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?
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.
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
int a[1010] , dp[1010];
int gcd(int a , int b) {
while(b)
{
int t = a % b;
a = b;
b = t;
}
return a;
}
int main()
{
int t;
scanf("%d" , &t);
while(t--) {
int n;
scanf("%d" , &n);
memset(dp , 0 , sizeof(dp));
for(int i = 1 ; i <= n ; i++) {
scanf("%d" , &a[i]);
}
for(int i = 1 ; i <= n ; i++) {
for(int j = i + 1 ; j <= n ; j++) {
dp[gcd(a[i] , a[j])] = 1;
}
}
int flag = 1;
for(int i = 1 ; ; i++) {
if(flag == 0 || i >= n - 2)
break;
flag = 0;
for(int j = 1 ; j <= 1000 ; j++) {
for(int l = 1 ; l <= n ; l++) {
int gg = gcd(a[l] , j);
if(dp[j] && !dp[gg]) {
flag = 1;
dp[gg] = 1;
}
}
}
}
int temp = 0;
for(int i = 1 ; i <= 1000 ; i++) {
if(!temp) {
if(dp[i]) {
printf("%d" , i);
temp = 1;
}
}
else {
if(dp[i])
printf(" %d" , i);
}
}
printf("\n");
}
return 0;
}
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 5726 GCD 区间GCD=k的个数
GCD Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submis ...
- GCD is Funny(hdu 5902)
GCD is Funny Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tota ...
- 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 ...
- HDU 4675 GCD of Sequence(容斥)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4675 题意:给出n,m,K,一个长度为n的数列A(1<=A[i]<=m).对于d(1< ...
- HDU 5726 GCD (RMQ + 二分)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5726 给你n个数,q个询问,每个询问问你有多少对l r的gcd(a[l] , ... , a[r]) ...
- HDU 5726 GCD(DP)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5726 [题目大意] 给出数列An,对于询问的区间[L,R],求出区间内数的GCD值,并且求出GCD ...
随机推荐
- 关于STM32GPIO按键上下拉配置的认识
说真的,后知后觉这个问题还是有点值得研究的,一开始学习我用的板子在按键模块电路中GPIO输入脚是有外部上下拉电阻的,如下图所示:当KEY1接V3.3,在其后为它接一个下拉电阻,可以保证按下按键输入高电 ...
- 读JDK源码集合部分
以前读过一遍JDK源码的集合部分,读完了一段时间后忘了,直到有一次面试简历上还写着读过JDK集合部分的源码,但面试官让我说说,感觉记得不是很清楚了,回答的也模模糊糊的,哎,老了记性越来越差了,所以再回 ...
- MyBatis 核心配置综述之 ParameterHandler
目录 ParameterHandler 简介 ParameterHandler 创建 ParameterHandler 中的参数从何而来 ParameterHandler 解析 MyBatis 四大核 ...
- Canvas动画(PC端 移动端)
Canvas动画(PC端 移动端) 一,介绍与需求 1.1,介绍 canvas是HTML5中新增一个HTML5标签与操作canvas的javascript API,它可以实现在网页中完成动态的2D与3 ...
- 每个程序员都可以「懂」一点 Linux
提到 Linux,作为程序员来说一定都不陌生.但如果说到「懂」Linux,可能就没有那么多人有把握了.到底用 Linux 离懂 Linux 有多远?如果决定学习 Linux,应该怎么开始?要学到什么程 ...
- JavaWeb——使用会话维持状态2
在这次的例子里面,将完成一类似购物车的功能,在客户访问网站的时候,会选中自己将要购买的商品,而购物车将始终维持着商品的状态,会话将联系起选择第一个商品(第一个请求),选择其他商品(其他请求)以及付款等 ...
- 通过注解实现通用导出Excel
Javaweb开发中数据的导入导出很常见,每次我们都需要写很多代码,所以我就在想能不能写一些通用的方法,之前已经在网上 整理过一些通用的方法,最近在网上看到一位牛人封装的更加简介,自己拿过来整理了一下 ...
- iText实现pdf导出
/** * AsianTest.java */ import java.io.FileOutputStream; import java.io.IOException; import com.lowa ...
- .Net Core2.1 秒杀项目一步步实现CI/CD(Centos7.2)系列一:k8s高可用集群搭建总结以及部署API到k8s
前言:本系列博客又更新了,是博主研究很长时间,亲自动手实践过后的心得,k8s集群是购买了5台阿里云服务器部署的,这个集群差不多搞了一周时间,关于k8s的知识点,我也是刚入门,这方面的知识建议参考博客园 ...
- 基于Spring Boot自建分布式基础应用
目前刚入职了一家公司,要求替换当前系统(单体应用)以满足每日十万单量和一定系统用户负载以及保证开发质量和效率.由我来设计一套基础架构和建设基础开发测试运维环境,github地址. 出于本公司开发现状及 ...