首页 > 其他 > 详细

GCD is Funny(hdu 5902)

时间:2016-09-25 11:55:16      阅读:344      评论:0      收藏:0      [点我收藏+]

GCD is Funny

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 282    Accepted Submission(s): 58


Problem Description
Alex has invented a new game for fun. There are n integers at a board and he performs the following moves repeatedly:

1. He chooses three numbers a, b and c written at the board and erases them.
2. He chooses two numbers from the triple a, b and c and calculates their greatest common divisor, getting the number d (d maybe gcd(a,b), gcd(a,c) or gcd(b,c)).
3. He writes the number d to the board two times.

It can be seen that after performing the move n2 times, there will be only two numbers with the same value left on the board. Alex wants to know which numbers can left on the board possibly. Can you help him?
 

 

Input
There are multiple test cases. The first line of input contains an integer T (1T100), indicating the number of test cases. For each test case:

The first line contains an integer n (3n500) -- the number of integers written on the board. The next line contains n integers: a1,a2,...,an (1ai1000) -- the numbers on the board.
 

 

Output
For each test case, output the numbers which can left on the board in increasing order.
 

 

Sample Input
3 4 1 2 3 4 4 2 2 2 2 5 5 6 2 3 4
 

 

Sample Output
1 2 2 1 2 3
思路:因为最多执行n-2次,a<=1000,所以每次选两个数先做下gcd,然后新生成的数必定可以留下,那么还没完,需要继续将新生成的数,与原来的数gcd,这个不需要考虑这个新的数会和已经抹掉的数gcd得出新的值,因为如果这个新的数是由另外两个数gcd得到,那么这个数在和他们gcd时就为自己本身,不会增加新的。
 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 }

 

GCD is Funny(hdu 5902)

原文:http://www.cnblogs.com/zzuli2sjy/p/5905567.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!