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 ...
随机推荐
- 002.MFC_对话框_静态文本_编辑框
一.建立 名为dialogAndCtl的MFC工程,并添加如图控件 1.将上方static text 控件 Caption属性设置为在文本框中如数文本,可以统计字符 2.edit control控件属 ...
- 第二阶段:2.商业需求分析及BRD:7.商业需求文档3
BRD模版 阐述需求来源以及调研分析情况 百度指数工具.定量的数据.发展趋势,是否与公司的战略冲突.环境政策:比如做内容的运营. 决策层看重的! 第二大块. 通过什么方式解决这个需求. 规划能力.类似 ...
- 使用element的upload组件实现一个完整的文件上传功能(上)
说到标题就有点心塞了,前段时间项目上需要实现一个文件上传的功能,然后就咔咔的去用了element的upload组件,不用不知道一用吓一跳哇. 在使用的过程中遇到了很多让意想不到的问题,后来也因为时间问 ...
- 通过公网连接阿里云redis,rinetd
目前云数据库 Redis 需要通过 ECS 的内网进行连接访问.如果您本地需要通过公网访问云数据库 Redis,可以在 ECS Linux 云服务器中安装 rinetd 进行转发实现. 1.在云服务器 ...
- IntelliJ IDEA的常用设置及快捷键
IntelliJ IDEA的常用设置及快捷键 基本设置 打开设置:ctrl+alt+s 修改主题.字体.字号 快捷键设置 创建项目和模块 标记源码文件 标记资源文件 设置jdk版本号 配置Tomcat ...
- 前端Tips#3 - 简写的 border-radius 100% 和 50% 是等效的
本文同步自 JSCON简时空 - 技术博客,点击阅读 视频讲解 视频地址 文字讲解 1.先讲结论 border-radius 这个 css 属性大家应该使用得非常娴熟,现实中用到的场景基本都是四个圆角 ...
- Go中的Package和Module分析
Package 所谓package(包)其实就是代码的一种组织管理方式,代码多了就需要放入文件,文件多了就需要归类放入文件夹,就好比我们在给电脑装软件时会进行归类安装,其实也是有意无意对电脑软件安装的 ...
- 【转】推荐给初级Java程序员的3本进阶书
ImportNew 注: 原作者在这篇文章中介绍3本不错的技术书籍.作者认为这些书籍对新手或者学生而言尤其有帮助.通过一些基础性的教程入门后,我们可以使用Java做基础性的编程.然而,当我们需要从初级 ...
- 【Tool】---ubuntu18.04配置oh-my-zsh工具
作为Linux忠实用户,应该没有人不知道bash shell工具了吧,其实除了bash还有许多其他的工具,zsh就是一款很好得选择,基于zsh shell得基础之上,oh-my-zsh工具更是超级利器 ...
- python认识及环境变量
什么是python? python是一种脚本语言,是高级语言.计算机只能识别机器语言,在机器语言上是汇编语言,再往上是高级语言.高级语言的基础是C语言. python语言较为简单,易入门. pytho ...