HDU 5902 GCD is Funny 数学
2016-11-23 18:48
344 查看
GCD is Funny
题目连接:
http://acm.hdu.edu.cn/showproblem.php?pid=5902Description
Alex has invented a new game for fun. There are n integers at a board and he performs the following moves repeatedly:He chooses three numbers a, b and c written at the board and erases them.
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)).
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.Sample Input
34
1 2 3 4
4
2 2 2 2
5
5 6 2 3 4
Sample Output
1 22
1 2 3
Hint
题意
给你n个数,然后选出三个数出来,然后再从这三个数中选择两个数做GCD,然后再扔两个GCD回到原序列。一直重复N-2次,最后显然只会剩下两个相同的数,问你这个数是多少。
题解:
感觉好神啊……这道题是某次BC的出题事故= =
答案是所有size>=2的子集的gcd
模拟n-2次暴力去搞一搞就好了。
不用考虑新增加的数,因为没有意义,你总会从之前的数里面求GCD得到。
代码
#include<bits/stdc++.h> using namespace std; const int maxn = 1005; int n; int can[maxn]; int a[maxn]; int gcd(int a,int b){ return b==0?a:gcd(b,a%b); } void solve() { memset(can,0,sizeof(can)); scanf("%d",&n); 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++){ can[gcd(a[i],a[j])]=1; } } int num = n-3; bool flag = true; while(num>=1&&flag){ num--; flag = false; for(int i=1;i<=1000;i++){ if(can[i]){ for(int j=1;j<=n;j++){ int p = gcd(i,a[j]); if(!can[p]){ can[p]=1; flag = true; } } } } } int first = 0; for(int i=1;i<=1000;i++){ if(can[i]){ if(first==0)printf("%d",i),first=1; else printf(" %d",i); } } printf("\n"); } int main() { int t; scanf("%d",&t); while(t--)solve(); return 0; }
相关文章推荐
- Hdu-5902-GCD is Funny (gcd)
- HDU 5902:GCD is Funny (GCD)
- HDU 5902 GCD is Funny
- HDU - 5902 GCD is Funny 数论(GCD相关)
- HDU 5869 Different GCD Subarray Query (数学gcd+树状数组离线查询)
- HDU 5019 Revenge of GCD(数学)
- hdu-5651 xiaoxin juju needs help(数学+gcd约分求阶乘)
- 【hdu 2504】又见GCD(数学相关)
- HDU 4497 GCD and LCM (数学,质数分解)
- hdu 4497 GCD and LCM(组合数学)
- GCD is Funny(hdu 5902)
- 【gcd+数学证明】【HDU1722】 CAKE
- hdu 5902 - 数学
- HDU 1695 GCD (数论-整数和素数,欧拉函数,组合数学-容斥原理)
- 【gcd+数学证明】【HDU1722】 CAKE
- HDU-5902-GCD is Funny
- HDU-4497 GCD and LCM (数学)
- HDU 1695 GCD (数论-整数和素数,组合数学-容斥原理)
- HDU 5902 GCD is Funny(GCD)
- HDU 5019 Revenge of GCD(数学)