题目链接:
hdu:http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=153598
bc(中文):http://bestcoder.hdu.edu.cn/contests/contest_chineseproblem.php?cid=577&pid=1002
题解:
1、对于每个输入,枚举它的因子,并统计,结果存在mmp数组中,最后再倒过来扫一遍mmp数组,其中第一个mmp[i]>=2的,就是答案。
时间复杂度:O(n*sqrt(n) )
这个跑了1404ms
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 using namespace std; 5 6 const int maxn=1e5+10; 7 typedef long long LL; 8 9 int n; 10 int mmp[maxn]; 11 12 void init(){ 13 memset(mmp,0,sizeof(mmp)); 14 } 15 16 int main() { 17 int tc,kase=0; 18 scanf("%d",&tc); 19 while(tc--) { 20 scanf("%d",&n); 21 init(); 22 for(int i=0;i<n;i++){ 23 int x; 24 scanf("%d",&x); 25 for(int div=1;div*div<=x;div++){ 26 if(x%div==0){ 27 mmp[div]++; 28 if(x/div!=div) mmp[x/div]++; 29 } 30 } 31 } 32 int ans=1; 33 for(int i=maxn-1;i>=0;i--){ 34 if(mmp[i]>=2){ 35 ans=i; break; 36 } 37 } 38 printf("Case #%d: %d\n",++kase,ans); 39 } 40 return 0; 41 }