Many Perfect Squares - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
题意:
思路:
n,1~50:对n暴力
x,0~1e18:O(1)计算
完全平方数:p^2
Code:
#include <bits/stdc++.h>
#define int long long
#define max(a,b) (a>b?a:b)
#define min(a,b) (a<b?a:b)
using namespace std;
const int mxn=1e6+10;
const int mxe=1e6+10;
const int mod=998244353;
int N,ans=1;
int a[60];
bool check(int x){
int t=sqrt(x);
return t*t==x;
}
void solve(){
ans=1;
cin>>N;
for(int i=1;i<=N;i++) cin>>a[i];
//sort(a+1,a+1+N);
for(int i=1;i<=N;i++){
for(int j=i+1;j<=N;j++){
int u=a[j]-a[i];
for(int t=1;t*t<=u;t++){
if(u%t!=0||(u/t+t)%2==1) continue;
int tmp=(u/t-t)/2;
int x=tmp*tmp-a[i];
if(x<0) continue;
int cnt=0;
for(int l=1;l<=N;l++){
if(check(a[l]+x)) cnt++;
}
ans=max(ans,cnt);
}
}
}
cout<<ans<<'\n';
}
signed main(){
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int __=1;cin>>__;
while(__--)solve();return 0;
}