BestCoder Round #87 1001
GCD is Funny
Alex has invented a new game for fun. There are nnn integers at a board and he performs the following moves repeatedly:
- He chooses three numbers aaa, bbb and ccc written at the board and erases them.
- He chooses two numbers from the triple aaa, bbb and ccc and calculates their greatest common divisor, getting the number ddd (ddd maybe gcd(a,b)\gcd(a,b)gcd(a,b), gcd(a,c)\gcd(a,c)gcd(a,c) or gcd(b,c)\gcd(b, c)gcd(b,c)).
- He writes the number ddd to the board two times.
It can be seen that after performing the move n−2n-2n−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?
There are multiple test cases. The first line of input contains an integer TTT (1≤T≤100)(1 \le T \le 100)(1≤T≤100), indicating the number of test cases. For each test case:
The first line contains an integer nnn (3≤n≤500)(3 \le n \le 500)(3≤n≤500) -- the number of integers written on the board. The next line contains nnn integers: a1,a2,...,ana_1, a_2, ..., a_na1,a2,...,an (1≤ai≤1000)(1 \le a_i \le 1000)(1≤ai≤1000) -- the numbers on the board.
For each test case, output the numbers which can left on the board in increasing order.
3
4
1 2 3 4
4
2 2 2 2
5
5 6 2 3 4
1 2
2
1 2 3
题意很简单 就是找n中任意两个数的最大最大公约数,我最开始选择用数组去存,结果超时了 ,导致wa了三遍,
后来选择用map去存
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<map>
#include<algorithm>
typedef long long ll;
using namespace std;
int test[1006];
int test2[1006];
int GCD(int a,int b)
{
if(a < b)
int temp = a, a = b, b = temp;
if(b == 0) return a;
return GCD(b,a%b);
}
int main()
{
int t;
int i,j;
int n;
scanf("%d",&t);
map<int,int>test2;
while(t--)
{
scanf("%d",&n);
for(i=1;i<=n;i++)
scanf("%d",&test[i]);
int tt;
for(i=1;i<n;i++)
{
for(j=i+1;j<=n;j++)
{
tt=GCD(test[i],test[j]);
test2[tt]=1;
}
}
map<int,int>::iterator it;
int flag=1;
for(it=test2.begin();it!=test2.end();it++)
{
if(flag==1)
printf("%d",it->first);
else
printf(" %d",it->first);
flag=0;
}
test2.clear();
printf("\n");
} }
BestCoder Round #87 1001的更多相关文章
- 贪心 BestCoder Round #39 1001 Delete
题目传送门 /* 贪心水题:找出出现次数>1的次数和res,如果要减去的比res小,那么总的不同的数字tot不会少: 否则再在tot里减去多余的即为答案 用set容器也可以做,思路一样 */ # ...
- HDU 5904 - LCIS (BestCoder Round #87)
HDU 5904 - LCIS [ DP ] BestCoder Round #87 题意: 给定两个序列,求它们的最长公共递增子序列的长度, 并且这个子序列的值是连续的 分析: 状态转移方程式 ...
- 暴力 BestCoder Round #41 1001 ZCC loves straight flush
题目传送门 /* m数组记录出现的花色和数值,按照数值每5个搜索,看看有几个已满足,剩下 5 - cnt需要替换 ╰· */ #include <cstdio> #include < ...
- 暴力 BestCoder Round #46 1001 YJC tricks time
题目传送门 /* 暴力:模拟枚举每一个时间的度数 详细解释:http://blog.csdn.net/enjoying_science/article/details/46759085 期末考结束第一 ...
- 字符串处理 BestCoder Round #43 1001 pog loves szh I
题目传送门 /* 字符串处理:是一道水题,但是WA了3次,要注意是没有加'\0'的字符串不要用%s输出,否则在多组测试时输出多余的字符 */ #include <cstdio> #incl ...
- BestCoder Round #75 1001 - King's Cake
Problem Description It is the king's birthday before the military parade . The ministers prepared a ...
- BestCoder Round #92 1001 Skip the Class —— 字典树 or map容器
题目链接:http://bestcoder.hdu.edu.cn/contests/contest_showproblem.php?cid=748&pid=1001 题解: 1.trie树 关 ...
- BestCoder Round #87 1003 LCIS[序列DP]
LCIS Accepts: 109 Submissions: 775 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65 ...
- BestCoder Round #87 1002 Square Distance[DP 打印方案]
Square Distance Accepts: 73 Submissions: 598 Time Limit: 4000/2000 MS (Java/Others) Memory Limit ...
随机推荐
- [IIS]IIS扫盲(八)
iis - IIS之FTP服务器 一.建立你的FTP站点 第一个FTP站点(即“默认FTP站点”)的设置方法和更多FTP站点的建立方法请参照前文Web服务器中相关操作执行.需要注意的是,如果你要用一 ...
- 玩转单元测试之Testing Spring MVC Controllers
玩转单元测试之 Testing Spring MVC Controllers 转载注明出处:http://www.cnblogs.com/wade-xu/p/4311657.html The Spri ...
- centos 用dvd创建yum 仓库
环境:CentOS 6.0 默认的yum是以网络来安装的,在没有网络或者网速不佳的情况下,通过yum来安装软件是意见非常痛苦的事情.其实对于CentOS DVD来说,里面提供的软件就足以满足我们的需要 ...
- jQuery最佳编程实践
加载jQuery 1.坚持使用CDN来加载jQuery,这种别人服务器免费帮你托管文件的便宜干嘛不占呢.点击查看使用CDN的好处,点此查看一些主流的jQuery CDN地址. <script t ...
- 二十六:Struts2 和 spring整合
二十六:Struts2 和 spring整合 将项目名称为day29_02_struts2Spring下的scr目录下的Struts.xml文件拷贝到新项目的scr目录下 在新项目的WebRoot-- ...
- java实验2实验报告(20135131)
一.实验内容 1. 初步掌握单元测试和TDD 2. 理解并掌握面向对象三要素:封装.继承.多态 3. 初步掌握UML建模 4. 熟悉S.O.L.I.D原则 5. 了解设计模式 二.实验要求 1.没有L ...
- RSA加密前端JS加密,后端asp.net解密,报异常
RSA加密前端JS加密,后端asp.net解密,报异常 参考引用:http://www.ohdave.com/rsa/的JS加密库 前端JS加密代码: function GetChangeStr() ...
- session基础
1.每个页面都必须开启session_start()后才能在每个页面里面使用session. 2.session_start()初始化session,第一次访问会生成一个唯一会话ID保存在客户端(是基 ...
- VC++ MFC中如何将应用程序的配置信息保存到注册表中(二)
在上一篇中介绍了几个写入注册表数据和读取注册表数据的接口,并介绍了使用方法. 这一片教你如何使得你的应用程序在下次打开时保持上一次关闭前的状态. 在上一篇添加的代码的基础上,要添加WM_CLOSE消息 ...
- framebuffer应用编程实践
framebuffer的使用主要包括4个部分: (1):首先需要打开设备文件 /dev/fb0. (2):获取设备的信息.包括可变信息和不可变信息,分别使用两个结构体来进行封装,这两个结构体在 < ...