Pick two numbers ai,aj(i≠j)ai,aj(i≠j) from a sequence to maximize the value of their greatest common divisor.
Input
Multiple test cases. In the first line there is an integer TT , indicating the number of test cases. For each test cases, the first line contains an integer nn , the size of the sequence. Next line contains nn numbers, from a1a1 to anan . 1≤T≤100,2≤n≤105,1≤ai≤1051≤T≤100,2≤n≤105,1≤ai≤105 . The case for n≥104n≥104 is no more than 1010 .
Output
For each test case, output one line. The output format is Case #xx : ansans , xx is the case number, starting from 11 , ansans is the maximum value of greatest common divisor.
Sample Input
2 4 1 2 3 4 3 3 6 9
Sample Output
Case #1: 2 Case #2: 3
#include<bits/stdc++.h>
using namespace std;
int maxn[100002];
int main(){
int t,n;int k;int m=0;int cas=1;
cin>>t;
while(t--){
cin>>n;
memset(maxn,0,sizeof(maxn));m=0;
for(int i=1;i<=n;i++){
scanf("%d",&k);
m=max(m,k);
int ss=sqrt(k+0.5);
for(int i=1;i<=ss;i++){
if(k%i==0) {
maxn[i]++;
maxn[k/i]++;
}
}
if(ss*ss==k) maxn[ss]--;
}
for(int i=m;i>=1;i--){
if(maxn[i]>=2){
cout<<"Case #"<<cas++<<": ";
cout<<i<<endl;
break;
}
}
}
return 0;
}
这道题我随便看了一下,首先暴力是会超时的,再是二分是不合理的,然后把所有的列出来感觉不是很行,就看了答案。
首先这个复杂度一般1e4,意思是可能可以吗,感觉表述的有点迷。
N根号N,还是可以的,考虑的是数量级。 把所有的因子列出来,找出最大的出现至少两次的因子即可。
所以看一道题起码要做个一小时或者半小时的,若是没思路才看,或者看一道,再做一道,总要有几道真是完全自己做的
虽然水平不是很够,做题总是错,但是这个效率应该是挺高的。
小心重根多计算了,还有,下标开始的数字要小心,太不走心了。