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. Linux 软件安装位置选择指南

    Linux 软件安装   Linux 下安装软件不像 Windows 下安装这么简单,Windows 下会自动选择合适安装路径,而 Linux 下安装路径大部分完全由自己决定,我可以将软件安装到任意可 ...

  2. Hadoop入门 概念

    Hadoop是分布式系统基础架构,通常指Hadoop生态圈 主要解决 1.海量数据的存储 2.海量数据的分析计算 优势 高可靠性:Hadoop底层维护多个数据副本,即使Hadoop某个计算元素或存储出 ...

  3. JavaScript设计模式,单例模式!

    单例设计模式:保证一个类仅有一个实例,并且提供一个访问它的全局访问点.有些对象只需要一个,这时可用单例模式. 传统的单例模式 和new 创建对象的调用不一样 调用者要调用xxx.getInstance ...

  4. Linux:变量$#,$@,$0,$1,$2,$*,$$,$?

    写一个简单的脚本 vim var 脚本内容如下: #!/bin/sh echo "the number of parameters passed to the script: $#" ...

  5. 看看线程特有对象ThreadLocal

    作用:设计线程安全的一种技术. 在使用多线程的时候,如果多个线程要共享一个非线程安全的对象,常用的手段是借助锁来实现线程的安全.线程安全隐患的前提是多线程共享一个不安全的对象 ,那么有没有办法让线程之 ...

  6. python初探——pandas使用

    一.简介 pandas 是基于NumPy 的一种工具,该工具是为了解决数据分析任务而创建的.Pandas 纳入了大量库和一些标准的数据模型,提供了高效地操作大型数据集所需的工具.pandas提供了大量 ...

  7. Jenkins构建通知

    目录 一.简介 二.推送到gitlab 三.邮件通知 自带配置 Email Extension 四.钉钉通知 五.脚本钉钉通知 六.HTTP请求通知 一.简介 类似于监控报警,jenkins在配置持续 ...

  8. 转:Android控件属性

    Android功能强大,界面华丽,但是众多的布局属性就害苦了开发者,下面这篇文章结合了网上不少资料,花费本人一个下午搞出来的,希望对其他人有用. 第一类:属性值为true或false android: ...

  9. pf4j及pf4j-spring

    什么是PF4J 一个插件框架,用于实现插件的动态加载,支持的插件格式(zip.jar). 核心组件 Plugin:是所有插件类型的基类.每个插件都被加载到一个单独的类加载器中以避免冲突. Plugin ...

  10. [BUUCTF]PWN——ciscn_2019_n_3

    ciscn_2019_n_3 附件 步骤 例行检查,32位,开启了nx和canary保护 本地试运行一下,经典的堆题的菜单 3.32位ida载入 new(),申请了两个chunk,第一个chunk(1 ...