GCD is Funny

来源:互联网 发布:css书籍推荐知乎 编辑:程序博客网 时间:2024/06/18 16:00
Problem Description

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

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

It can be seen that after performing the move n-2n2 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 TT (1 \le T \le 100)(1T100), indicating the number of test cases. For each test case:

The first line contains an integer nn (3 \le n \le 500)(3n500) -- the number of integers written on the board. The next line contains nn integers: a_1, a_2, ..., a_na1,a2,...,an (1 \le a_i \le 10^6)(1ai106) -- 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





/*
求最大的共约数;
*/
#include<iostream>
#include<math.h>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=510;
int gcd(int a,int b)//最大公约数;
{
    if(b==0)
        return a;
    return gcd(b,a%b);
}
bool b[1000010];
int main()
{
    int t;
    cin>>t;
    int n,a[101];
    while(t--)
    {
        memset(b,0,sizeof(b));
        cin>>n;
        for(int i=1;i<=n;i++)
        {
            cin>>a[i];
            for(int j=1;j<i;j++)
            {
                int d=gcd(a[i],a[j]);
                b[d]=1;
            }
        }
        int flag=0;
        for(int i=1;i<=n;i++)
        {
            if(b[i])//公约数存在;
            {
                if(!flag)
                {
                    flag=1;
                    printf("%d",i);
                }
                else
                    printf(" %d",i);
            }
        }
        cout<<endl;
    }
}

0 0