GCD is Funny

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了GCD is Funny相关的知识,希望对你有一定的参考价值。

GCD is Funny

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

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?
 

 

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

The first line contains an integer $n$ $(3 \le n \le 500)$ -- the number of integers written on the board. The next line contains $n$ integers: $a_1, a_2, ..., a_n$ $(1 \le a_i \le 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
分析:大意:给你>=3个数,每次取3个数去掉,添上其中两个数的gcd的2遍,最后剩两个相同数是多少?
   仔细分析可知第一次取两个数gcd,去掉另一个数,以后就可以任取剩下n-3个数作gcd;
   所以就是要求2~n-1个数的gcd,模拟即可;
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <unordered_map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, ls[rt]
#define Rson mid+1, R, rs[rt]
#define sys system("pause")
#define intxt freopen("in.txt","r",stdin)
const int maxn=1e3+10;
using namespace std;
int gcd(int p,int q){return q==0?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
inline ll read()
{
    ll x=0;int f=1;char ch=getchar();
    while(ch<0||ch>9){if(ch==-)f=-1;ch=getchar();}
    while(ch>=0&&ch<=9){x=x*10+ch-0;ch=getchar();}
    return x*f;
}
int n,m,k,t,a[maxn],ok[maxn];
queue<pii>p;
int main()
{
    int i,j;
    scanf("%d",&t);
    while(t--)
    {
        memset(ok,0,sizeof(ok));
        while(!p.empty())p.pop();
        scanf("%d",&n);
        rep(i,1,n)scanf("%d",&a[i]);
        rep(i,1,n)rep(j,i+1,n)
        {
            k=gcd(a[i],a[j]);
            if(!ok[k])ok[k]=1,p.push(mp(k,2));
        }
        while(!p.empty())
        {
            pii q=p.front();
            p.pop();
            if(q.se==n-1)break;
            rep(i,1,n)
            {
                k=gcd(q.fi,a[i]);
                if(!ok[k])ok[k]=1,p.push(mp(k,q.se+1));
            }
        }
        bool flag=false;
        rep(i,1,1000)
        {
            if(ok[i])
            {
                if(flag)printf(" %d",i);
                else printf("%d",i),flag=true;
            }
        }
        printf("\n");
    }
    //system("Pause");
    return 0;
}

以上是关于GCD is Funny的主要内容,如果未能解决你的问题,请参考以下文章

GCD is Funny(hdu 5902)

HDU 5902 GCD is Funny DP

[HDU5902]GCD is Funny(xjb搞)

Life Is A Funny Proposition After All

HDU5902 GCD is FunnyGCD

add application window with unknown token XXX Unable to add window;is your activity is running?(代码片段