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. MPI 学习笔记

    目录 MPI学习笔记 MPI准备 概述 前置知识补充 环境部署 1.修改IP及主机名 2.关闭防火墙 3.实现免密码SSH登录 4.配置MPI运行环境 5.测试 程序的执行 编译语句 运行语句 MPI ...

  2. LeetCode替换空格

    LeetCode 替换空格 题目描述 请实现一个函数,把字符串 s 中的每个空格替换成"%20". 实例 1: 输入:s = "We are happy." 输 ...

  3. nodejs-Express框架

    JavaScript 标准参考教程(alpha) 草稿二:Node.js Express框架 GitHub TOP Express框架 来自<JavaScript 标准参考教程(alpha)&g ...

  4. 《C陷阱与缺陷》 第0章导读 第1章词法陷阱

    1.= 与==的区别 赋值运算符= 的优先级要小于逻辑运算符== 也就是说,会进行先逻辑上的比较,然后再把比较结果进行赋值,很合理. getc库是什么??? 1.C语言中有单字符 = 也有多字符单元如 ...

  5. Nginx流量拷贝

    1. 需求 将生产环境的流量拷贝到预上线环境或测试环境,这样做有很多好处,比如: 可以验证功能是否正常,以及服务的性能: 用真实有效的流量请求去验证,又不用造数据,不影响线上正常访问: 这跟灰度发布还 ...

  6. Can namespaces be nested in C++?

    In C++, namespaces can be nested, and resolution of namespace variables is hierarchical. For example ...

  7. ssh 无法使用

    ssh 无法运行造成无法远程连接 linux 原因: 我将 /var  目录权限修改成了 777,但 linux 系统出于安全起见,该目录的 7 权限只对 root 用户开放,所以linux 系统认为 ...

  8. JVM堆空间结构及常用的jvm内存分析命令和工具

    jdk8之前的运行时数据区域 程序计数器 是一块较小的内存空间,它可以看做是当前线程所执行的字节码的行号指示器.每个线程都有一个独立的程序计数器,这类内存区域为"线程私有",此内存 ...

  9. Redis主从 部署和配置

    目录 一.主从简介 主从介绍 主从原理 二.主从部署 环境介绍 主从配置 临时主从 三.主从测试 一.主从简介 主从介绍 Redis都是主节点.每个从节点只能有一个主节点,而主节点可以同时具有多个从节 ...

  10. aha

    欢迎使用 MWeb MWeb 是专业的 Markdown 写作.记笔记.静态博客生成软件,目前已支持 Mac,iPad 和 iPhone.MWeb 有以下特色: 软件本身: 使用原生的 macOS 技 ...