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的<组合 ...
随机推荐
- 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 ...
- Shell学习(一)——Shell简介
参考博客: [1]Shell简介
- ybatis中查询出多个以key,value的属性记录,封装成一个map返回的方法
可以采用值做映射,也可以不采用映射方式 <resultMap id="configMap" type="java.util.Map" > <r ...
- LocalDate计算两个日期相差天数
import org.apache.commons.lang.StringUtils; import java.time.LocalDate; import java.time.ZoneId; imp ...
- java上传图片或文件
转载至:http://www.xdx97.com/#/single?bid=8b351a73-922c-eadc-512e-9e248a3efde9 前端通过form表单用post方式提交文件,后台进 ...
- .net core Winform 添加DI和读取配置、添加log
首先新建配置类 public class CaptureOption { /// <summary> /// 是否自启 /// </summary> public bool A ...
- Linux内核启动流程(简介)
1. vmlinux.lds 首先分析 Linux 内核的连接脚本文件 arch/arm/kernel/vmlinux.lds,通过链接脚本可以找到 Linux 内核的第一行程序是从哪里执行的: 第 ...
- Mybatis中原生DAO实现和Mapper动态代理实现
Mybatis开发dao的方法通常用两种,一种是传统DAO的方法,另一种是基于mapper代理的方法. 一.传统DAO方式开发 1.sql语句映射文件编写 User.xml <?xml vers ...
- cron 获取下次运行时间(基于 C# + Quartz.NET)
代码 Quartz 的 cron 支持秒,导致一些 cron 库无法准确的获得下次执行时间,这里使用 Quartz.Net 自带的方法来获取下次执行时间. //引用 Quartz CronExpres ...
- 10 - Vue3 UI Framework - Tabs 组件
标签页是非常常用的组件,接下来我们来制作一个简单的 Tabs 组件 返回阅读列表点击 这里 需求分析 我们先做一个简单的需求分析 可以选择标签页排列的方向 选中的标签页应当有下划线高亮显示 切换选中时 ...