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;
}


思想
一。 b−a=2,因为两个连续奇数差为2
二。 b=a+2
我们现在来求 (b+a)
(b+a)=(a+2+a)=(2a+2)=2(a+1)
因为a+1肯定是偶数(因为a是奇数),所以2(a+1)一定至少是4。
因此 (b+a)一定可以被4整除。 又因为 (b−a)是2 ,所以 (b+a)(b−2)至少是8
所以 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;
}