GCD is Funny

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 282    Accepted Submission(s): 58

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 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
思路:因为最多执行n-2次,a<=1000,所以每次选两个数先做下gcd,然后新生成的数必定可以留下,那么还没完,需要继续将新生成的数,与原来的数gcd,这个不需要考虑这个新的数会和已经抹掉的数gcd得出新的值,因为如果这个新的数是由另外两个数gcd得到,那么这个数在和他们gcd时就为自己本身,不会增加新的。
 1 #include<stdio.h>
2 #include<algorithm>
3 #include<iostream>
4 #include<string.h>
5 #include<queue>
6 #include<stack>
7 using namespace std;
8 int gcd(int n,int m);
9 int ans[10000];
10 bool flag[1005];
11 int main(void)
12 {
13 int T;
14 int n;
15 scanf("%d",&T);
16 while(T--)
17 {
18 scanf("%d",&n);
19 int i,j;
20 for(i = 0; i < n; i++)
21 {
22 scanf("%d",&ans[i]);
23 }
24 memset(flag,0,sizeof(flag));
25 for(i = 0; i < n; i++)
26 {
27 for(j = i+1; j < n; j++)
28 {
29 flag[gcd(ans[i],ans[j])] = true;
30 }
31 }
32 int v = n;
33 v--;
34 int c = 1;
35 while(v >= 3&&c)
36 {
37 v--;
38 c = 0;
39 for(i = 1; i <= 1000; i++)
40 {
41 if(flag[i])
42 {
43 for(j = 0; j < n; j++)
44 {
45 int x = gcd(ans[j],i);
46 if(!flag[x])
47 {
48 flag[x] = true;
49 c = 1;
50 }
51 }
52 }
53 }
54 }
55 int s = 0;
56 for(i = 1; i <= 1000; i++)
57 {
58 if(flag[i])
59 {
60 if(!s)
61 {
62 s=1;
63 printf("%d",i);
64 }
65 else
66 {
67 printf(" %d",i);
68 }
69 }
70 }
71 printf("\n");
72 }
73 return 0;
74 }
75 int gcd(int n,int m)
76 {
77 if(m == 0)
78 return n;
79 else return gcd(m,n%m);
80 }

GCD is Funny(hdu 5902)的更多相关文章

  1. GCD and LCM HDU 4497 数论

    GCD and LCM HDU 4497 数论 题意 给你三个数x,y,z的最大公约数G和最小公倍数L,问你三个数字一共有几种可能.注意123和321算两种情况. 解题思路 L代表LCM,G代表GCD ...

  2. HDU 5902 GCD is Funny 数学

    GCD is Funny 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5902 Description Alex has invented a ne ...

  3. 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 ...

  4. 数学(GCD,计数原理)HDU 5656 CA Loves GCD

    CA Loves GCD Accepts: 135 Submissions: 586 Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 2621 ...

  5. GCD and LCM HDU - 4497(质因数分解)

    Problem Description Given two positive integers G and L, could you tell me how many solutions of (x, ...

  6. GCD and LCM HDU - 4497

    题目链接:https://vjudge.net/problem/HDU-4497 题意:求有多少组(x,y,z)满足gcd(x,y,z)=a,lcm(x,y,z)=b. 思路:对于x,y,z都可以写成 ...

  7. hdu 5902 Seam Carving

    水题,直接上代码了 #include<cstdio> #include<cstring> #include<iostream> #include<cmath& ...

  8. acm数学(转)

    这个东西先放在这吧.做过的以后会用#号标示出来 1.burnside定理,polya计数法    这个大家可以看brudildi的<组合数学>,那本书的这一章写的很详细也很容易理解.最好能 ...

  9. [转] POJ数学问题

    转自:http://blog.sina.com.cn/s/blog_6635898a0100magq.html 1.burnside定理,polya计数法 这个大家可以看brudildi的<组合 ...

随机推荐

  1. A Child's History of England.49

    But he was shipwrecked in the Adriatic Sea, and was fain [happy, willing] to pass through Germany, u ...

  2. Shell学习(一)——Shell简介

    参考博客: [1]Shell简介

  3. ybatis中查询出多个以key,value的属性记录,封装成一个map返回的方法

    可以采用值做映射,也可以不采用映射方式 <resultMap id="configMap" type="java.util.Map" > <r ...

  4. LocalDate计算两个日期相差天数

    import org.apache.commons.lang.StringUtils; import java.time.LocalDate; import java.time.ZoneId; imp ...

  5. java上传图片或文件

    转载至:http://www.xdx97.com/#/single?bid=8b351a73-922c-eadc-512e-9e248a3efde9 前端通过form表单用post方式提交文件,后台进 ...

  6. .net core Winform 添加DI和读取配置、添加log

    首先新建配置类 public class CaptureOption { /// <summary> /// 是否自启 /// </summary> public bool A ...

  7. Linux内核启动流程(简介)

    1. vmlinux.lds 首先分析 Linux 内核的连接脚本文件 arch/arm/kernel/vmlinux.lds,通过链接脚本可以找到 Linux 内核的第一行程序是从哪里执行的: 第 ...

  8. Mybatis中原生DAO实现和Mapper动态代理实现

    Mybatis开发dao的方法通常用两种,一种是传统DAO的方法,另一种是基于mapper代理的方法. 一.传统DAO方式开发 1.sql语句映射文件编写 User.xml <?xml vers ...

  9. cron 获取下次运行时间(基于 C# + Quartz.NET)

    代码 Quartz 的 cron 支持秒,导致一些 cron 库无法准确的获得下次执行时间,这里使用 Quartz.Net 自带的方法来获取下次执行时间. //引用 Quartz CronExpres ...

  10. 10 - Vue3 UI Framework - Tabs 组件

    标签页是非常常用的组件,接下来我们来制作一个简单的 Tabs 组件 返回阅读列表点击 这里 需求分析 我们先做一个简单的需求分析 可以选择标签页排列的方向 选中的标签页应当有下划线高亮显示 切换选中时 ...