hdu 5902 GCD is Funny
来源:互联网 发布:ios手游模拟器 mac 编辑:程序博客网 时间:2024/06/16 16:41
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 numbersa , b and c written at the board and erases them.
2. He chooses two numbers from the triplea , 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 numberd to the board two times.
It can be seen that after performing the moven−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
2. He chooses two numbers from the triple
3. He writes the number
It can be seen that after performing the move
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 integern (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
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刚开始看是1001 所以,直接两两gcd()跑了一遍。
结果就阵亡了。。。。。。。。。。。。。。。。
晚上想了想,6 6 10 15 这个例子我出的结果显然不对。
然后我保留两两gcd的结果,继续gcd 。
反正晚上一直wa。跑到700+ms
今天上午再写,才发现,6 10 15 我多跑一边, 多出了个1
然后记录一下剩余个数,别多跑,就过了。
#include <bits/stdc++.h>using namespace std;const int maxn=1110;int gcd(int x,int y){ if(x%y==0) return y; else return gcd(y,x%y);}int num[maxn],a[maxn];int main(){ int T,n; cin>>T; while(T--) { cin>>n; memset(num,0,sizeof(num)); 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++) num[gcd(a[i],a[j])]=1; int flag=1,cnt=n-1; while(flag&&cnt>=3) { cnt--; flag=0; for(int i=1;i<=1000;i++) if(num[i]) for(int j=1;j<=n;j++) { int t=gcd(i,a[j]); if(num[t]==0) { num[t]=1; flag=1; } } } int t=0; for(int i=1;i<=1000;i++) if(num[i]) { if(t) printf(" %d",i); else{ printf("%d",i); t=1; } } printf("\n"); }}
0 0
- hdu 5902 GCD is Funny
- HDU 5902 GCD is Funny
- HDU-5902-GCD is Funny
- HDU 5902:GCD is Funny (GCD)
- HDU 5902 GCD is Funny(GCD)
- Hdu-5902-GCD is Funny (gcd)
- hdu 5902 GCD is Funny(BestCoder #87 1001)
- GCD is Funny
- hdu5902 GCD is Funny
- hdu5902 GCD is Funny
- hdu5902 GCD is Funny
- GCD is Funny (gcd + 想法)
- hdu5902 GCD is Funny(错) && hdu5904 LCIS(最长公共连续上升子序列)
- Mecanum wheels is funny
- hdu5902GCD is Funny+数论
- HDU 6050 Funny Function
- hdu 6050 Funny Function
- hdu 6050 funny function
- CXF整合Spring整合實現WebService服務
- Android调用系统相机和相册-坑篇
- ListView分页显示
- Add Digits
- DVD
- hdu 5902 GCD is Funny
- 【浅墨Unity3D Shader编程】之一 夏威夷篇:游戏场景的创建 & 第一个Shader的书写
- C语言实现99乘法表
- 微信应用号(小程序)开发IDE配置
- 第一篇博客
- 巴蜀 1738 曹冲养猪
- BZOJ 1084 [SCOI2005]最大子矩阵
- HttpServlet cannot be resolved to a type 解决办法
- 创业者眼中的小程序:某个岗位工资会翻番