HDU 5902 GCD is Funny 数学
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:
- 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
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 数学的更多相关文章
- 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 ...
- 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 ...
- HDU 5726 GCD 区间GCD=k的个数
GCD Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submis ...
- HDU 5726 GCD (2016多校、二分、ST表处理区间GCD、数学)
题目链接 题意 : 给出一个有 N 个数字的整数数列.给出 Q 个问询.每次问询给出一个区间.用 ( L.R ) 表示.要你统计这个整数数列所有的子区间中有多少个和 GCD( L ~ R ) 相等.输 ...
- 数学--数论--HDU 4675 GCD of Sequence(莫比乌斯反演+卢卡斯定理求组合数+乘法逆元+快速幂取模)
先放知识点: 莫比乌斯反演 卢卡斯定理求组合数 乘法逆元 快速幂取模 GCD of Sequence Alice is playing a game with Bob. Alice shows N i ...
- 数学--数论--HDU 5223 - GCD
Describtion In mathematics, the greatest common divisor (gcd) of two or more integers, when at least ...
- 数学--数论--HDU 5382 GCD?LCM?(详细推导,不懂打我)
Describtion First we define: (1) lcm(a,b), the least common multiple of two integers a and b, is the ...
- GCD is Funny(hdu 5902)
GCD is Funny Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tota ...
- HDU 4497 GCD and LCM (数学,质数分解)
题意:给定G,L,分别是三个数最大公因数和最小公倍数,问你能找出多少对. 析:数学题,当时就想错了,就没找出规律,思路是这样的. 首先G和L有公因数,就是G,所以就可以用L除以G,然后只要找从1-(n ...
随机推荐
- python基础语法(1)
一.基本概念 1. python中数有四种类型:整数.长整数.浮点数和复数. 整数, 如 1 长整数 是比较大的整数 浮点数 如 1.23.3E-2 复数 如 1 + 2j. 1.1 + 2.2j 2 ...
- angularjs+jasmine单元测试入门
使用cordova.angularjs.ionic开发hybrid App有一段时间了.为了做单元测试,之前一直是把要测的某一部分产品代码复制到另一个单独的工程中来写测试代码,测好了以后再复制回去.弊 ...
- 项目启动异常java.lang.OutOfMemoryError: PermGen space
java.lang.OutOfMemoryError: PermGen space 解决办法: Eclipse-->window-->Tomcat -->JVM setting - ...
- 中国大学MOOC-陈越、何钦铭-数据结构-2016秋期中考试
判断题: 1-1 算法分析的两个主要方面是时间复杂度和空间复杂度的分析. (2分) 1-2 将N个数据按照从小到大顺序组织存放在一个单向链表中.如果采用二分查找,那么查找的平均时间复杂度是O(logN ...
- 使用NHibernate(7)-- 一对一 && 一对多 && 多对多
1, 一对一. 对于数据量比较大的时候,考虑查询的性能,肯能会把一个对象的属性分到两个表中存放:比如用户和用户资料,经常使用的一般是Id和用户名,用户资料(学校,籍贯等)是不经常被查询的,所以就会分成 ...
- SSH Secure Shell Client的windows客户端样式设置
SSH Secure Shell Client下载:http://pan.baidu.com/s/1dF2lDdf 其他工具(putty-0.67)下载:http://pan.baidu.com/s/ ...
- SpringMVC关于json、xml自动转换的原理研究[附带源码分析]
目录 前言 现象 源码分析 实例讲解 关于配置 总结 参考资料 前言 SpringMVC是目前主流的Web MVC框架之一. 如果有同学对它不熟悉,那么请参考它的入门blog:http://www.c ...
- [芯片] 4、接口技术·实验四·串行接口8251A
目录 一.实验目的和要求... 2 二.实验原理与背景... 3 三.实验具体的内容... 3 四.实验的代码说明... 4 五.实验结果的分析... 6 附录资料 一.实验目的和要求 学会8251芯 ...
- ASP.NET CheckBoxList Operations with jQuery
本文描述了如何通过jQuery来对ASP.NET CheckBoxList控件进行一些基本操作,如通过value/text/index check/uncheck CheckBoxList,最小/最大 ...
- 汇编语言hello world
DOS下: ;栈段 stack segment stack db dup(?) stack ends ;数据段 data segment szHello db 'Hello,world',0dh,0a ...