数论

Problem - D - Codeforces

大致题意

给你一个长度为\(n\)的数列,让你给数列中每一个数都加上一个任意数,使得这个时候数列中的平方数最多并输出平方数的数量

显然我们可以知道,我们总可以加上一个任意数使得存在一个数是平方数

再来看数据范围

he first line contains the number of test cases \(t\) (\(1≤t≤50\)).

The first line of each test case contains a single integer \(n\) (\(1≤n≤50\)) — the size of the set.

显然我们可以使用\(O(n^4)\)以上时间复杂度的做法

下面开始分析:

假设存在一个 \(x\) 使得存在两个数列中的数 \(a_i,a_j\)满足 \(a_i+x\) , \(a_j+x\)都为平方数(\(a_i\ge a_j\))

此时满足 \(a_i+x=A^2\) , \(a_j+x=B^2\)

两式相减得 \(a_i-a_j=A^2-B^2\)

\(p=a_i-a_j\)

则上式变为 \(p=A^2-B^2\)

\(p=(A+B)×(A-B)\)

\(\alpha\)\(p\) 的较大的因子\((A+B)\),$ \beta$ 是 \(p\) 的较小的因子\((A-B)\)

\(p=\alpha×\beta\)

\(A=\frac{\alpha+\beta}{2}\) , \(B=\frac{\alpha-\beta}{2}\)

\(\alpha+\beta\)为偶数 , \(\alpha-\beta\)也为偶数

\(x=B^2-a_j\)

由此便得到了一个 \(x\) 带入计算可得平方数的大小

因为(\(1≤n≤50\))由此可得一两千个 \(x\) 便可以一一带入计算得到最优解

key code

const int N=2e5+10;
int n,m,k,a[N],b[N],p[N];
int issqrt(int n){
	int res=sqrt(n);
	return res*res==n;
}
int check(int x){
	int res=0;
	up(1,n)res+=issqrt(a[o]+x);
	return res;
}
void solve(){
	//try it again.
	cin>>n;
	up(1,n)cin>>a[o];
	int ans=check(0);
	if(!ans)ans^=1;
	fup(i,1,n){
		fup(j,i+1,n){
			int d=(a[j]-a[i]);
			for(int x=1;x*x<=d;x++){
				if(d%x==0){
					int y=d/x;
					if((x+y)&1||(x-y)&1)continue;
					int B=(x+y)>>1;
					int A=(x-y)>>1;
					if(B*B>=a[j]){
						ans=max(ans,check(B*B-a[j]));
					}
				}
			}
		}
	}
	cout<<ans<<endl;
}
posted @ 2023-01-17 16:26  liangqianxing  阅读(62)  评论(0)    收藏  举报