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. Qt 5.7 版本+2013VS环境配置

    原本是使用MSVC5.5.1的版本,碍于习惯,之前一直使用creator,后面才转向VS,因为它的调试实在是太棒了,小程序还是creator调试(比如抽出大程序中的小模块之类的) 不知道是版本问题还是 ...

  2. js时间 字符串相互转化

    js的时间和字符串的转化的讲解是有很多文章的,基本的都是一致的原理.不过曾经碰到过一个比较坑爹的需求,看到网上很少有相关的总结,所以自己简单的记录一下,给后来的同学们点思路. 当时的需求是这样子的,某 ...

  3. Mysql --分区表(2)

    分区类型 RANGE分区 range分区的表是利用取值范围将数据分成分区,区间要连续并且不能互相重叠,使用values less than操作符进行分区定义 LIST分区 LIST分区是建立离散的值列 ...

  4. MySQL设置字符集CHARACTER SET

    本文地址:http://www.cnblogs.com/yhLinux/p/4036506.html 在 my.cnf 配置文件中设置相关选项,改变为相应的character set. 设置数据库编码 ...

  5. JS JQuery初始化

    (function($) {})(jQuery); 这种写法,申明一个匿名函数并立即调用 $(document).ready(function(){}); 文档全部加载完再执行 等同于$(functi ...

  6. Spring Mobile 1.1.0.RC1 和 1.0.2 发布

    Spring Mobile 1.1.0.RC1 发布了,该版本包含: 支持 Firefox OS 设备的检测 修复了使用 LiteDeviceDelegatingViewResolver 处理重定向和 ...

  7. [汇编] 将字符串里的一个'&'字符换成空格

    ; multi-segment executable file template. data segment ; add your data here! pkey db "press any ...

  8. [WinAPI] API 5 [遍历驱动器并获取驱动器属性]

    (1) GetLogicalDrives.获取主机中所有的逻辑驱动器,以BitMap的形式返回.◇返回值GetLogicalDrive函数返回一个DWORD类型的值,第一位表示所对应的驱动器是否存在. ...

  9. [游戏模版9] Win32 半透明 图像处理

    >_<:Previous part we talk about how to map a transparent picture, and this time we will solve ...

  10. Spring-MVC接收request参数和向页面传值总结

    接收请求参数值,三种方式: 1使用HttpServletRequest获取 2使用@RequestParam注解 3使用自动封装机制封装成bean对象 向页面传值 1直接使用HttpServletRe ...