GCD is Funny

题目连接:

http://acm.hdu.edu.cn/showproblem.php?pid=5902

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.

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

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 数学的更多相关文章

  1. hdu 4497 GCD and LCM 数学

    GCD and LCM Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4 ...

  2. hdu 5902 GCD is Funny

    Problem Description Alex has invented a new game for fun. There are n integers at a board and he per ...

  3. HDU 5726 GCD 区间GCD=k的个数

    GCD Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submis ...

  4. HDU 5726 GCD (2016多校、二分、ST表处理区间GCD、数学)

    题目链接 题意 : 给出一个有 N 个数字的整数数列.给出 Q 个问询.每次问询给出一个区间.用 ( L.R ) 表示.要你统计这个整数数列所有的子区间中有多少个和 GCD( L ~ R ) 相等.输 ...

  5. 数学--数论--HDU 4675 GCD of Sequence(莫比乌斯反演+卢卡斯定理求组合数+乘法逆元+快速幂取模)

    先放知识点: 莫比乌斯反演 卢卡斯定理求组合数 乘法逆元 快速幂取模 GCD of Sequence Alice is playing a game with Bob. Alice shows N i ...

  6. 数学--数论--HDU 5223 - GCD

    Describtion In mathematics, the greatest common divisor (gcd) of two or more integers, when at least ...

  7. 数学--数论--HDU 5382 GCD?LCM?(详细推导,不懂打我)

    Describtion First we define: (1) lcm(a,b), the least common multiple of two integers a and b, is the ...

  8. GCD is Funny(hdu 5902)

    GCD is Funny Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tota ...

  9. HDU 4497 GCD and LCM (数学,质数分解)

    题意:给定G,L,分别是三个数最大公因数和最小公倍数,问你能找出多少对. 析:数学题,当时就想错了,就没找出规律,思路是这样的. 首先G和L有公因数,就是G,所以就可以用L除以G,然后只要找从1-(n ...

随机推荐

  1. Unity3D 中 用quaternion 来对一个坐标点进行旋转的初步体会

    在unity3d中,用四元数来表示旋转,四元数英文名叫quaternion . 比如 transform.rotation 就是一个四元数,其由四个部分组成 Quaternion = (xi + yj ...

  2. BFS 或 同余模定理(poj 1426)

    题目:Find The Multiple 题意:求给出的数的倍数,该倍数是只由 1与 0构成的10进制数. 思路:nonzero multiple  非零倍数  啊. 英语弱到爆炸,理解不了题意... ...

  3. Tomcat7.0安装配置详细

    说明:Tomcat服务器上一个符合J2EE标准的Web服务器,在tomcat中无法运行EJB程序,如果要运行可以选择能够运行EJB程序的容器WebLogic,WebSphere,Jboss等:Tomc ...

  4. 利用NVelocity 模版生成文本文件

    namespace Common { public class Tools { public string Process(string content, int startIndex, int le ...

  5. IOS封装一个微信聊天的输入工具

    1.实现微信的输入工具 实现了大部分功能,各模块实现的很清晰,有利于更好的二次开发(适合自己的需求),我自己总结出来的, 可以更快的让你实现输入工具,不需要扩展的也可以很方便的使用这个输入工具. 1) ...

  6. [原] XAF 如何非常容易禁止清除一个下拉字段的值?

  7. 利用Formdata实现form提交文件上传不跳转页面

    作者:幻月九十链接:https://www.zhihu.com/question/19631256/answer/119911045来源:知乎著作权归作者所有,转载请联系作者获得授权. $('form ...

  8. Centos6.7下安装配置VPN

    在Vultr上买了台VPS准备做VPN,不贵5刀,位置是日本东京的.ping值在100-200之间,还好算说的过去. Vultr地址 系统选择的Centos6 的版本是6.7 在网上查了查linux下 ...

  9. 体验CoreCLR的stack unwinding特性在Linux/Mac上的初步实现

    有了stack unwinding特性,才能在.NET程序中获取调用堆栈(call stack)信息,才能在异常时显示调用堆栈信息.这个特性之前只在Windows上有实现,Linux/Mac上的实现最 ...

  10. 编译生成.NET Core Framework遇到的问题

    前两天在Windows Server 2012上编译生成.NET Core Framework的代码库corefx,遭遇了几个问题,在这篇博文中记录一下. 编译生成操作方法是在命令行(Develope ...