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. Eclipse安装配置以及java项目和类的创建

    1.Eclipse的安装: 双击此应用程序 进入安装界面 选择下一步 更改路径将此默认路径改为 确定之后下一步更改jre的安装路径 在之前安装的java文件夹下新建一个jre文件夹 将jre安装在里边 ...

  2. SQL多行转多列

    --★转换结果如上图 1.首先创建表: CREATE TABLE [成绩表]( ,) NOT NULL, )NULL, , )NULL, , )NULL, , )NULL ) ON [PRIMARY] ...

  3. JS初级-作用域

    作用域:域:空间.范围.区域--作用:读.写    script        全局变量.全局函数        自上而下        函数        由里到外        {}    浏览器 ...

  4. LeetCode:Permutations, Permutations II(求全排列)

    Permutations Given a collection of numbers, return all possible permutations. For example, [1,2,3] h ...

  5. 正确理解DTO、值对象和POCO

    今天推荐的文章比较技术化也比较简单,但是对于一些初学者而言,可能也是容易搞混的概念:就是如何理解DTO.值对象和POCO之间的区别. 所谓DTO就是数据传输对象(Data Transfer Objec ...

  6. VM - Bridge Adapter

    如何让外部可以连到本地的虚拟机. 1. 网络模式 - Bridged Adapter 2. 确保本机插上网线 3. 如果虚拟机是 Windows 8.1, 需要开启如下选项.

  7. [Leetcode][JAVA] Populating Next Right Pointers in Each Node II

    Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tre ...

  8. java基础-反射之Class.forName

    Class.forName 查阅javaAPI public static Class<?> forName(String className) throws ClassNotFoundE ...

  9. PHPCMS联动菜单的调用函数get_linkage方法详解

    v9联动菜单调用方法[注意此为内容页调用方法 {get_linkage($areaid,1,' >> ',1)} 显示效果: 湖北省 >> 武汉市 >> 汉阳区 [ ...

  10. Oracle EBS的BIP报表中显示特殊字体

    http://oracleseeker.com/2009/08/25/font_mapping_setup_for_special_character_print_in_oracle_ebs_bip/ ...