GCD is FunnyTime Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 265 Accepted Submission(s): 52 |
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≤100)
, 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
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
Source
Recommend
提示
题意:
Alex发明了一个有趣的游戏. 一开始他在黑板上写了n个正整数, 然后他开始重复进行如下的操作:
1. 他选择黑板上三个数字a, b和c, 把他们从黑板上擦掉.
2. 他从这三个数a, b和c中选择了两个数, 并计算出他们的最大公约数, 记这个数为d (d 可以是gcd(a,b), gcd(a,c)或者gcd(b, c)).
3. 他在黑板上写下两次数字d.
显然, 在操作n-2次后, 黑板上只会留下两个相同的数字. Alex想要知道哪些数字可以最终留在黑板上.
2. 他从这三个数a, b和c中选择了两个数, 并计算出他们的最大公约数, 记这个数为d (d 可以是gcd(a,b), gcd(a,c)或者gcd(b, c)).
3. 他在黑板上写下两次数字d.
显然, 在操作n-2次后, 黑板上只会留下两个相同的数字. Alex想要知道哪些数字可以最终留在黑板上.
思路:
先两两求出最大公约数(最大公因数),之后还需要去用这些再接着与序列求出最大公约数(最大公因数),最后依次输出即可。
题解最终还是来了:
终于搞清楚了这个题的做法, 于是来补个题解. 再次对造成的rating变化以及做题感受道歉.
考虑这题操作的本质, 就是每次选择三个数, 然后丢掉一个数, 之后加回去两个相同的数. 本来一个直观的想法就是两两gcd, 但是这个反例很容易举, 比如6,10,15,30. 之后又一个想法就是求任意一个size≥2的子集的gcd, 但是这个反例也有一个反例, 比如6,10,15. 这个想法之所以错了就是因为我们每次要丢掉一个数. 那么是不是只要考虑2≤size<n的子集就好了呢? 对, 这样就好了.
考虑到, 最后留下来的数一定是某个子集的gcd. 我们只要在一开始丢掉了一个数, 考虑留下来两个数是x,x, 那么又选了另一个数y的话, 我们只要丢掉其中一个x就能获得了两个gcd(x,y), 也就是说接下来每次操作我们都有了一个额外的数用来丢弃, 且不会改变子集gcd的种类数.
接下来只要枚举答案g, 判断g是否可能是gcd就好了. 这个做法比较复杂, 我采用的是直接计算gcd是g子集个数(这个是一个经典的容斥, 这里不表), 为了避免使用大数, 取了若干个乘积超过2500的质数来取模. 如果模每个质数的方案数都是0, 那么g肯定不会存在在最后结果中.
唉, 这个做法真切和标题.
示例程序
Problem : 5902 ( GCD is Funny ) Judge Status : Accepted
RunId : 18347194 Language : GCC Code Len : 1574 B
Code Render Status : Rendered By HDOJ GCC Code Render Version 0.01 Beta
#include <stdio.h>
#include <string.h>
int gcd(int x,int y)
{
int t;
while(y!=0)
{
t=x%y;
x=y;
y=t;
}
return x;
}
int main()
{
int a[1000],num[1001],t,n,x,i,i1,i2,flag,len;
scanf("%d",&t);
for(i=1;t>=i;i++)
{
scanf("%d", &n);
for(i1=0;n>i1;i1++)
{
scanf("%d",&a[i1]);
}
memset(num,0,sizeof(num));
for(i1=0;n>i1;i1++)
{
for(i2=i1+1;n>i2;i2++)
{
num[gcd(a[i1],a[i2])]=1; //记录最大公约数
}
}
flag=1; //是否还能求出最大公约数
len=n-3; //n-3似乎是模拟题目的操作
while(flag==1&&len>=1)
{
flag=0;
len--;
for(i1=1;1000>=i1;i1++) //每个求出的最大公约数和数列接着再找最大公约数
{
if(num[i1]==1)
{
for(i2=0;n>i2;i2++)
{
x=gcd(a[i2],i1);
if(num[x]==0)
{
flag=1; //还能求出最大公约数,还必须是以前没有的
num[x]=1;
}
}
}
}
}
flag=0;
for(i1=1;1000>=i1;i1++) //数据输出
{
if(num[i1]==1)
{
if(flag==0)
{
printf("%d",i1);
flag=1;
}
else
{
printf(" %d",i1);
}
}
}
printf("\n");
}
return 0;
}