HDU-5902-GCD is Funny解题笔记
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
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
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
题意:Alex发明了一个有趣的游戏. 一开始他在黑板上写了n个正整数, 然后他开始重复进行如下的操作: 1. 他选择黑板上三个数字a, b和c, 把他们从黑板上擦掉.
2. 他从这三个数a, b和c中选择了两个数, 并计算出他们的最大公约数, 记这个数为d (d 可以是gcd(a,b), gcd(a,c)或者gcd(b,c)).
3. 他在黑板上写下两次数字d. 显然, 在操作n−2次后, 黑板上只会留下两个相同的数字. Alex想要知道哪些数字可以最终留在黑板上.
解题思路:原数列中的数两两GCD的结果必可保留,产生的新数与原数列还能继续两两GCD产生新数,如此再进行n-3次操作,若没有新数生成则跳出。
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <set>
#include <vector>
#include <cctype>
#include <iomanip>
#include <sstream>
#include <climits>
#include <queue>
#include <stack>
using namespace std;
typedef long long ll;
#define INF 0X3f3f3f3f
const ll MAXN = 1e3 + ;
const ll MOD = 1e9 + ;
int GCD(int a, int b)
{
return b == ? a : GCD(b, a % b);
}
int num[MAXN];
int ans[MAXN];
int main()
{
ios::sync_with_stdio();
int t;
cin >> t;
while (t--)
{
memset(ans, , sizeof(ans));
int n;
cin >> n;
for (int i = ; i < n; i++)
cin >> num[i];
for (int i = ; i < n - ; i++)
for (int j = i + ; j < n; j++)
ans[GCD(num[i], num[j])] = ;
int cnt = n;
bool run = true;
while (cnt-- >= && run)
{
run = false;
for (int i = ; i <= ; i++)
if (ans[i])
for (int j = ; j < n; j++)
{
if (!ans[GCD(num[j], i)])
{
ans[GCD(num[j], i)] = ;
run = true;
}
}
}
bool flag = false;
for (int i = ; i <= ; i++)
{
if (ans[i])
{
if (!flag)
{
flag = true;
cout << i;
}
else
cout << ' ' << i;
}
}
cout << endl;
}
return ;
}
HDU-5902-GCD is Funny解题笔记的更多相关文章
- 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 ...
- HDU 5726 GCD 区间GCD=k的个数
GCD Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submis ...
- 《剑指offer》解题笔记
<剑指offer>解题笔记 <剑指offer>共50题,这两周使用C++花时间做了一遍,谨在此把一些非常巧妙的方法.写代码遇到的难点.易犯错的细节等做一个简单的标注,但不会太过 ...
- GCD is Funny(hdu 5902)
GCD is Funny Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tota ...
- HDU 2588 GCD 【Euler + 暴力技巧】
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=2588 GCD Time Limit: 2000/1000 MS (Java/Others) Mem ...
- HDU 3791二叉搜索树解题(解题报告)
1.题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=3791 2.参考解题 http://blog.csdn.net/u013447865/articl ...
- HDU 1695 GCD (欧拉函数+容斥原理)
GCD Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- HDU 1695 GCD 容斥
GCD 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=1695 Description Given 5 integers: a, b, c, d, k ...
- hdu 4497 GCD and LCM 数学
GCD and LCM Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4 ...
随机推荐
- Ubuntu常用命令大全 以及 PHP+MySQL代码部署在Linux(Ubuntu)上注意事项
PHP+MySQL代码部署在Linux(Ubuntu)上注意事项 https://cloud.tencent.com/developer/article/1024187 Ubuntu常用命令大全 ht ...
- torch or numpy
黄色:重点 粉色:不懂 Torch 自称为神经网络界的 Numpy, 因为他能将 torch 产生的 tensor 放在 GPU 中加速运算 (前提是你有合适的 GPU), 就像 Numpy 会把 a ...
- 0009 CSS基础选择器( 标签、类、id、通配符)
typora-copy-images-to: media 第01阶段.前端基础.CSS基础选择器 CSS选择器(重点) 学习目标: 理解 能说出选择器的作用 id选择器和类选择器的区别 应用 能够使用 ...
- 使用poi读写excel、向excel追加数据等,包括.xls和.xlsx文档
1.使用maven引入jar包 <dependency> <groupId>org.apache.poi</groupId> <artifactId>p ...
- Python学习(二)语言基础
一.变量与类型 在程序设计中,变量是一种存储数据的载体 整型:Python中可以处理任意大小的整数 浮点型:浮点数也就是小数 字符串型:字符串是以单引号或双引号括起来的任意文本 布尔型:布尔值只有Tr ...
- 洛谷$P4099\ [HEOI2013]\ SAO\ dp$
正解:树形$dp$ 解题报告: 传送门$QwQ$. 考虑设$f_i$表示点$i$的子树内的拓扑序排列方案数有多少个. 发现这样不好合并儿子节点和父亲节点.于是加一维,设$f_{i,j}$表示点$i$的 ...
- Spring 资源注入
Spring开发中经常需要调用各种资源,包含普通文件.网址.配置文件.系统环境变量等,我们可以使用Spring表达式语言(Spring-EL)实现资源的注入. Spring主要使用@Value注解实现 ...
- Java和JavaScript之间的区别
1.简介 通过优锐课核心java学习笔记中,我们可以看到,Java和JavaScript之间的区别.我们将在本文中比较Java语言和JavaScript语言.JavaScript由Netscape开发 ...
- 最全面的css布局
1.定位 定位的概念就是它允许你定义一个元素相对于其他正常元素的位置,它应该出现在哪里,这里的其他元素可以是父元素,另一个元素甚至是浏览器窗口本身.还有就是浮动了,其实浮动并不完全算是定位,它的特性非 ...
- 【转】早该知道的7个JavaScript技巧
我写JAVAScript代码已经很久了,都记不起是什么年代开始的了.对于JavaScript这种语言近几年所取得的成就,我感到非常的兴奋:我很幸运也是这些成就的获益者.我写了不少的文章,章节,还有一本 ...