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 ...
随机推荐
- CRM 权限与分派不一样问题
问题描述: 1 userA用户 为 区域经理; 2 区域经理(角色) 为 分派给userA的安全角色; 3 区域经理(角色) 设置了对实体 客户 的读权限为 业务部门级; 按正常来说userA 应 ...
- ArcGIS Engine要素渲染和专题图制作(转)
摘要:Feature的常用的绘制方法包括:1.简单绘制:2.唯一值绘制/多字段唯一值绘制:3.点密度/多字段点密度绘制:4.数据分级绘制:5.质量图(饼图/直方图): 6.按比例尺渲染:7.比例符号渲 ...
- dom4j生成xml
package com.yunfengtech.solution.business; import java.io.FileOutputStream; import org.dom4j.Documen ...
- win7以管理员身份运行bat提示系统找不到指定的路径。
windows7“以管理员身份运行”bat提示“系统找不到指定的路径.” 使用批处理安装服务,直接双击运行没有权限,右键“以管理员身份运行”却提示“系统找不到指定的路径.”,反复查看路径是正确的. 打 ...
- Archlinux 添加无线网络
From: https://wiki.archlinux.org/index.php/Beginners%27_Guide_%28%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%8 ...
- shell 初学者 必读 ,强烈推荐新手读
背景: 很多人从C/C++转化而来,看了学习文档之后,踩入了很多坑 1 对变量赋值 不要有空格 a=123 # 正确 a = 123 # 错误 2 if语句 [] 要留有空格,变量最好加" ...
- loadrunner 功能详解(一) - Run-time Settings
1.General / Run Logic Number of Iterations:说明的是反复循环的次数. 常境的时间中,如果时间设为5分钟,而实际上程序的运行只需要1分钟,而在这项中,选择的是 ...
- Asp.net 怎样去除表单多行文本框滚动条
<textarea style="overflow:hidden;border-width:0px;">永远没有滚动条</textarea><text ...
- 纸上谈兵:哈希表(hash table)
作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! HASH 哈希表(hash table)是从一个集合A到另一个集合B的映射(map ...
- CPU阿甘
本系列文章全部摘选自"码农翻身"公众号,仅供个人学习和分享之用.文章会给出原文的链接地址,希望不会涉及到版权问题. 个人感言:真正的知识是深入浅出的,码农翻身" 公共号将 ...