标签:
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 282 Accepted Submission(s): 58
1 #include<stdio.h> 2 #include<algorithm> 3 #include<iostream> 4 #include<string.h> 5 #include<queue> 6 #include<stack> 7 using namespace std; 8 int gcd(int n,int m); 9 int ans[10000]; 10 bool flag[1005]; 11 int main(void) 12 { 13 int T; 14 int n; 15 scanf("%d",&T); 16 while(T--) 17 { 18 scanf("%d",&n); 19 int i,j; 20 for(i = 0; i < n; i++) 21 { 22 scanf("%d",&ans[i]); 23 } 24 memset(flag,0,sizeof(flag)); 25 for(i = 0; i < n; i++) 26 { 27 for(j = i+1; j < n; j++) 28 { 29 flag[gcd(ans[i],ans[j])] = true; 30 } 31 } 32 int v = n; 33 v--; 34 int c = 1; 35 while(v >= 3&&c) 36 { 37 v--; 38 c = 0; 39 for(i = 1; i <= 1000; i++) 40 { 41 if(flag[i]) 42 { 43 for(j = 0; j < n; j++) 44 { 45 int x = gcd(ans[j],i); 46 if(!flag[x]) 47 { 48 flag[x] = true; 49 c = 1; 50 } 51 } 52 } 53 } 54 } 55 int s = 0; 56 for(i = 1; i <= 1000; i++) 57 { 58 if(flag[i]) 59 { 60 if(!s) 61 { 62 s=1; 63 printf("%d",i); 64 } 65 else 66 { 67 printf(" %d",i); 68 } 69 } 70 } 71 printf("\n"); 72 } 73 return 0; 74 } 75 int gcd(int n,int m) 76 { 77 if(m == 0) 78 return n; 79 else return gcd(m,n%m); 80 }
标签:
原文地址:http://www.cnblogs.com/zzuli2sjy/p/5905567.html