首页 > 代码库 > GCD is Funny(hdu 5902)
GCD is Funny(hdu 5902)
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 n−2 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?
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 n−2 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 (1≤T, indicating the number of test cases. For each test case:
The first line contains an integer n (3≤n≤500) -- the number of integers written on the board. The next line contains n integers: a1,a2,...,an (1≤ai≤1000) -- the numbers on the board.
The first line contains an integer n (3≤n≤500) -- the number of integers written on the board. The next line contains n integers: a1,a2,...,an (1≤ai≤1000) -- the numbers on the board.
Output
For each test case, output the numbers which can left on the board in increasing order.
Sample Input
341 2 3 442 2 2 255 6 2 3 4
Sample Output
1 221 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 else66 {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)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。