GCD is Funny(hdu 5902)
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
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?
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.
4
1 2 3 4
4
2 2 2 2
5
5 6 2 3 4
2
1 2 3
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)的更多相关文章
- GCD and LCM HDU 4497 数论
GCD and LCM HDU 4497 数论 题意 给你三个数x,y,z的最大公约数G和最小公倍数L,问你三个数字一共有几种可能.注意123和321算两种情况. 解题思路 L代表LCM,G代表GCD ...
- HDU 5902 GCD is Funny 数学
GCD is Funny 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5902 Description Alex has invented a ne ...
- 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 ...
- 数学(GCD,计数原理)HDU 5656 CA Loves GCD
CA Loves GCD Accepts: 135 Submissions: 586 Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 2621 ...
- GCD and LCM HDU - 4497(质因数分解)
Problem Description Given two positive integers G and L, could you tell me how many solutions of (x, ...
- 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都可以写成 ...
- hdu 5902 Seam Carving
水题,直接上代码了 #include<cstdio> #include<cstring> #include<iostream> #include<cmath& ...
- acm数学(转)
这个东西先放在这吧.做过的以后会用#号标示出来 1.burnside定理,polya计数法 这个大家可以看brudildi的<组合数学>,那本书的这一章写的很详细也很容易理解.最好能 ...
- [转] POJ数学问题
转自:http://blog.sina.com.cn/s/blog_6635898a0100magq.html 1.burnside定理,polya计数法 这个大家可以看brudildi的<组合 ...
随机推荐
- MPI 学习笔记
目录 MPI学习笔记 MPI准备 概述 前置知识补充 环境部署 1.修改IP及主机名 2.关闭防火墙 3.实现免密码SSH登录 4.配置MPI运行环境 5.测试 程序的执行 编译语句 运行语句 MPI ...
- LeetCode替换空格
LeetCode 替换空格 题目描述 请实现一个函数,把字符串 s 中的每个空格替换成"%20". 实例 1: 输入:s = "We are happy." 输 ...
- nodejs-Express框架
JavaScript 标准参考教程(alpha) 草稿二:Node.js Express框架 GitHub TOP Express框架 来自<JavaScript 标准参考教程(alpha)&g ...
- 《C陷阱与缺陷》 第0章导读 第1章词法陷阱
1.= 与==的区别 赋值运算符= 的优先级要小于逻辑运算符== 也就是说,会进行先逻辑上的比较,然后再把比较结果进行赋值,很合理. getc库是什么??? 1.C语言中有单字符 = 也有多字符单元如 ...
- Nginx流量拷贝
1. 需求 将生产环境的流量拷贝到预上线环境或测试环境,这样做有很多好处,比如: 可以验证功能是否正常,以及服务的性能: 用真实有效的流量请求去验证,又不用造数据,不影响线上正常访问: 这跟灰度发布还 ...
- Can namespaces be nested in C++?
In C++, namespaces can be nested, and resolution of namespace variables is hierarchical. For example ...
- ssh 无法使用
ssh 无法运行造成无法远程连接 linux 原因: 我将 /var 目录权限修改成了 777,但 linux 系统出于安全起见,该目录的 7 权限只对 root 用户开放,所以linux 系统认为 ...
- JVM堆空间结构及常用的jvm内存分析命令和工具
jdk8之前的运行时数据区域 程序计数器 是一块较小的内存空间,它可以看做是当前线程所执行的字节码的行号指示器.每个线程都有一个独立的程序计数器,这类内存区域为"线程私有",此内存 ...
- Redis主从 部署和配置
目录 一.主从简介 主从介绍 主从原理 二.主从部署 环境介绍 主从配置 临时主从 三.主从测试 一.主从简介 主从介绍 Redis都是主节点.每个从节点只能有一个主节点,而主节点可以同时具有多个从节 ...
- aha
欢迎使用 MWeb MWeb 是专业的 Markdown 写作.记笔记.静态博客生成软件,目前已支持 Mac,iPad 和 iPhone.MWeb 有以下特色: 软件本身: 使用原生的 macOS 技 ...