蓝桥杯小白赛第六期 4.你不干?有的是帕鲁干!

4.你不干?有的是帕鲁干!【算法赛】 - 蓝桥云课 (lanqiao.cn)

这是我的代码,求出样例了,但是过不了:

js 复制代码
#include<iostream>
#include<cmath>
#include<string>
using namespace std;
typedef long long LL;
int main()
{
	int n; cin >> n;
	string s;
	for (int i = 0; i < n; i++)
	{
		cin >> s;

		for (int i = 1; i < 100000000000000000; i ++)
		{

			LL temp =  pow(i + 2, 2) - pow(i, 2);
			if (s == to_string(temp))
			{
				cout << "Yes\n"; cout << i << " " << i + 2 << endl;
				break;
			}
			else
			{
				cout << "No\n";
				break;
			}
		}
	}

	return 0;
}

思想

一。 <math xmlns="http://www.w3.org/1998/Math/MathML"> b − a = 2 b-a=2 </math>b−a=2,因为两个连续奇数差为2

二。 <math xmlns="http://www.w3.org/1998/Math/MathML"> b = a + 2 b=a+2 </math>b=a+2

我们现在来求 <math xmlns="http://www.w3.org/1998/Math/MathML"> ( b + a ) (b+a) </math>(b+a)

<math xmlns="http://www.w3.org/1998/Math/MathML"> ( b + a ) = ( a + 2 + a ) = ( 2 a + 2 ) = 2 ( a + 1 ) (b+a)=(a+2+a)=(2a+2)=2(a+1) </math>(b+a)=(a+2+a)=(2a+2)=2(a+1)

因为a+1肯定是偶数(因为a是奇数),所以2(a+1)一定至少是4。

因此 <math xmlns="http://www.w3.org/1998/Math/MathML"> ( b + a ) (b+a) </math>(b+a)一定可以被4整除。 又因为 <math xmlns="http://www.w3.org/1998/Math/MathML"> ( b − a ) (b-a) </math>(b−a)是2所以 <math xmlns="http://www.w3.org/1998/Math/MathML"> ( b + a ) ( b − 2 (b+a)(b-2 </math>(b+a)(b−2)至少是8

所以 <math xmlns="http://www.w3.org/1998/Math/MathML"> b 2 − a 2 b^2-a^2 </math>b2−a2一定可以至少被8整除所以我们要求的数一定可以被8整除

code

js 复制代码
#include <bits/stdc++.h>
using namespace std;
#define int long long

signed main()
{
  int t;cin>>t;
  while(t--){
      int x;cin>>x;
      if(x%8==0&&x!=0){
          cout<<"Yes"<<"\n";
          cout<<2*(x/8)-1<<" "<<2*(x/8)+1<<"\n";
      }else{
          cout<<"No"<<"\n";
      }
  }
  return 0;
}
相关推荐
我叫汪枫24 分钟前
C语言深度入门系列:第十一篇 - 动态内存管理与数据结构:程序世界的高效算法大师
c语言·数据结构·算法
Li_76953228 分钟前
优选算法100 题 ——1 双指针
算法
77qqqiqi31 分钟前
算法——数学基础
算法
啊?啊?32 分钟前
7 排序算法通关指南:从 O (n²)(选择 / 冒泡)到 O (nlogn)(快排 / 归并)+ 计数排序
数据结构·算法·排序算法
张较瘦_35 分钟前
[论文阅读] 算法 | 抗量子+紧凑!SM3-OTS:基于国产哈希算法的一次签名新方案
论文阅读·算法·哈希算法
芒克芒克36 分钟前
LeetCode 面试经典 150 题:多数元素(摩尔投票法详解 + 多解法对比)
算法·leetcode·面试
wow_DG37 分钟前
【Vue2 ✨】Vue2 入门之旅 · 进阶篇(二):虚拟 DOM 与 Diff 算法
开发语言·javascript·vue.js·算法·前端框架
和光同尘 、Y_____38 分钟前
BRepMesh_IncrementalMesh 重构生效问题
c++·算法·图形渲染
sali-tec2 小时前
C# 基于halcon的视觉工作流-章32-线线测量
开发语言·人工智能·算法·计算机视觉·c#
lingran__2 小时前
速通ACM省铜第一天 赋源码(The Cunning Seller (hard version))
c++·算法