hdu 5902 GCD is Funny

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≤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.

Output

For each test case, output the numbers which can left on the board in increasing order.

题意:n个数每次选三个数删除,取其中两个数将gcd放回去两次,问最后剩的数可能是多少。

由于一开始取3个数要删掉一个,所以只要求n-1个的gcd。

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
int a[1010] , dp[1010];
int gcd(int a , int b) {
    while(b)
    {
        int t = a % b;
        a = b;
        b = t;
    }
    return a;
}
int main()
{
    int t;
    scanf("%d" , &t);
    while(t--) {
        int n;
        scanf("%d" , &n);
        memset(dp , 0 , sizeof(dp));
        for(int i = 1 ; i <= n ; i++) {
            scanf("%d" , &a[i]);
        }
        for(int i = 1 ; i <= n ; i++) {
            for(int j = i + 1 ; j <= n ; j++) {
                dp[gcd(a[i] , a[j])] = 1;
            }
        }
        int flag = 1;
        for(int i = 1 ; ; i++) {
            if(flag == 0 || i >= n - 2)
                break;
            flag = 0;
            for(int j = 1 ; j <= 1000 ; j++) {
                for(int l = 1 ; l <= n ; l++) {
                    int gg = gcd(a[l] , j);
                    if(dp[j] && !dp[gg]) {
                        flag = 1;
                        dp[gg] = 1;
                    }
                }
            }
        }
        int temp = 0;
        for(int i = 1 ; i <= 1000 ; i++) {
            if(!temp) {
                if(dp[i]) {
                    printf("%d" , i);
                    temp = 1;
                }
            }
            else {
                if(dp[i])
                    printf(" %d" , i);
            }
        }
        printf("\n");
    }
    return 0;
}
时间: 2024-12-22 14:10:17

hdu 5902 GCD is Funny的相关文章

HDU 5902 GCD is Funny DP

http://acm.hdu.edu.cn/showproblem.php?pid=5902 一眼看上去,以为是枚举两个数,然后去重gcd即可. 但是不是 6 6 10 15这样的数据 应该是1 2 3 5 6 1是从6 6 15得到gcd = 3,然后3和10搭配得到的. 那么就是其它的gcd能够作为新的数字,去枚举其他数字,得到其他gcd.其实这个应该很容易想到的,但是比赛的时候却认为只用枚举两个就够了,可能是因为1001,觉得应该是水题吧.其实是自己蔡.. 那么考虑dp. dp[i][va

HDU 1695 GCD 欧拉函数+容斥原理+质因数分解

链接:http://acm.hdu.edu.cn/showproblem.php?pid=1695 题意:在[a,b]中的x,在[c,d]中的y,求x与y的最大公约数为k的组合有多少.(a=1, a <= b <= 100000, c=1, c <= d <= 100000, 0 <= k <= 100000) 思路:因为x与y的最大公约数为k,所以xx=x/k与yy=y/k一定互质.要从a/k和b/k之中选择互质的数,枚举1~b/k,当选择的yy小于等于a/k时,可以

HDU 1695 GCD (数论-整数和素数,组合数学-容斥原理)

GCD Problem Description Given 5 integers: a, b, c, d, k, you're to find x in a...b, y in c...d that GCD(x, y) = k. GCD(x, y) means the greatest common divisor of x and y. Since the number of choices may be very large, you're only required to output t

HDU 5726 GCD 区间GCD=k的个数

GCD Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2742    Accepted Submission(s): 980 Problem Description Give you a sequence of N(N≤100,000) integers : a1,...,an(0<ai≤1000,000,000). There ar

hdu 4497 GCD and LCM(排列组合)

题目:hdu 4497 GCD and LCM 题目大意:给出三个数的最大公约数,和最小公倍数,问这三个数的排列组合关系. 解题思路:最小公倍数/最大公约数 ==  三个数不同部分的乘积.这样来考虑的话,三个数都要有最大公约数的部分,其余的部分就是由LCM / GCD 里面的因子构成.这里面的因子可能会有 2 2 3 这样的情况, 不同的因子之间是不会相互干扰的,但是相同的会出现问题,因为,不能同时将相同的因子都放在三个位置上,这样最大公约数就的要乘上这个因子.然后对于单种因子来考虑的话,每种因

HDU 2588 GCD (欧拉函数)

GCD Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1013    Accepted Submission(s): 457 Problem Description The greatest common divisor GCD(a,b) of two positive integers a and b,sometimes writt

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 pe

HDU 2588 GCD

传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2588 解题思路: 本来的思路是枚举大于等于M的数S,若S|N,那么KS(KS<N)GCD(KS,N)>=M, 这样有重复的. 而这样有重复的: 若GCD(x,N)>=M. 设y=N/C,那么y的素因子有phi[i],phi[i+1]......., 那么GCD(x*phi[i],N)>=M, 而根据算术基本定理可知上述方法没有重复的. 那么ans=所有大于M的N的因子X的N/X的欧拉函数

hdu 4497 GCD and LCM 数论 素数分解

GCD and LCM Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Submission(s): 1339    Accepted Submission(s): 607 Problem Description Given two positive integers G and L, could you tell me how many solutions of