GCD is Funny
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 479 Accepted Submission(s): 128
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
题意:
n个数每次选三个数删除,取其中两个数将gcd放回去两次,问最后剩的数可能是多少。
天真的以为两两gcd就是答案。
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <cmath>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define L(i) i<<1
#define R(i) i<<1|1
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-9
#define maxn 10010
#define MOD 1000000007
const int mod = 2520;
int gcd(int a, int b)
{
while(b)
{
int t = a % b;
a = b;
b = t;
}
return a;
}
int a[550];
int vis[1200];
int T,n;
int main()
{
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int i=1; i<=n; i++)
{
scanf("%d",&a[i]);
}
memset(vis,0,sizeof(vis));
int ans=0;
for(int i=1; i<=n; i++)
for(int j=i+1; j<=n; j++)
{
int f=gcd(a[i],a[j]);
if(!vis[f])
{
vis[f]=1;
ans++;
}
}
int len =1;
while(len < n-2)
{
bool flag=true;
len++;
for(int i=1; i<=1000; i++)
{
if(vis[i])
{
for(int j=1; j<=n; j++)
{
int f=gcd(a[j],i);
if(!vis[f])
{
vis[f]=1;
ans++;
flag=false;
}
}
}
}
if(flag) break;
}
for(int i=1; i<=1000; i++)
{
if(vis[i]&&ans>1)
{
printf("%d ",i);
ans--;
}
else if(vis[i] && ans==1)
{
printf("%d\n",i);
ans--;
}
if(ans==0) break;
}
}
return 0;
}