GCD is Funny

Accepts: 524
Submissions: 1147
Time Limit: 4000/2000 MS (Java/Others)
Memory Limit: 65536/65536 K (Java/Others)
Problem Description

Alex has invented a new game for fun. There are nnn integers at a board and he performs the following moves repeatedly:

  1. He chooses three numbers aaa, bbb and ccc written at the board and erases them.
  2. 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)).
  3. 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?

Input

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_na​1​​,a​2​​,...,a​n​​ (1≤ai≤1000)(1 \le a_i \le 1000)(1≤a​i​​≤1000) -- the numbers on the board.

Output

For each test case, output the numbers which can left on the board in increasing order.

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
题意很简单 就是找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的更多相关文章

  1. 贪心 BestCoder Round #39 1001 Delete

    题目传送门 /* 贪心水题:找出出现次数>1的次数和res,如果要减去的比res小,那么总的不同的数字tot不会少: 否则再在tot里减去多余的即为答案 用set容器也可以做,思路一样 */ # ...

  2. HDU 5904 - LCIS (BestCoder Round #87)

    HDU 5904 - LCIS [ DP ]    BestCoder Round #87 题意: 给定两个序列,求它们的最长公共递增子序列的长度, 并且这个子序列的值是连续的 分析: 状态转移方程式 ...

  3. 暴力 BestCoder Round #41 1001 ZCC loves straight flush

    题目传送门 /* m数组记录出现的花色和数值,按照数值每5个搜索,看看有几个已满足,剩下 5 - cnt需要替换 ╰· */ #include <cstdio> #include < ...

  4. 暴力 BestCoder Round #46 1001 YJC tricks time

    题目传送门 /* 暴力:模拟枚举每一个时间的度数 详细解释:http://blog.csdn.net/enjoying_science/article/details/46759085 期末考结束第一 ...

  5. 字符串处理 BestCoder Round #43 1001 pog loves szh I

    题目传送门 /* 字符串处理:是一道水题,但是WA了3次,要注意是没有加'\0'的字符串不要用%s输出,否则在多组测试时输出多余的字符 */ #include <cstdio> #incl ...

  6. BestCoder Round #75 1001 - King's Cake

    Problem Description It is the king's birthday before the military parade . The ministers prepared a ...

  7. BestCoder Round #92 1001 Skip the Class —— 字典树 or map容器

    题目链接:http://bestcoder.hdu.edu.cn/contests/contest_showproblem.php?cid=748&pid=1001 题解: 1.trie树 关 ...

  8. BestCoder Round #87 1003 LCIS[序列DP]

    LCIS  Accepts: 109  Submissions: 775  Time Limit: 4000/2000 MS (Java/Others)  Memory Limit: 65536/65 ...

  9. BestCoder Round #87 1002 Square Distance[DP 打印方案]

    Square Distance  Accepts: 73  Submissions: 598  Time Limit: 4000/2000 MS (Java/Others)  Memory Limit ...

随机推荐

  1. Python基础篇【第1篇】: Python基础

    Python 简介 Python 是一个高层次的结合了解释性.编译性.互动性和面向对象的脚本语言. Python 的设计具有很强的可读性,相比其他语言经常使用英文关键字,其他语言的一些标点符号,它具有 ...

  2. XE6移动开发环境搭建之IOS篇(7):在Mac OSX 10.8中安装Xcode4.6.3(有图有真相)

    网上能找到的关于Delphi XE系列的移动开发环境的相关文章甚少,本文尽量以详细的图文内容.傻瓜式的表达来告诉你想要的答案. 原创作品,请尊重作者劳动成果,转载请注明出处!!! 在安装Xcode前, ...

  3. 查看/修改 Linux 时间和时区

    查看/修改Linux时区和时间 一.时区      1. 查看当前时区                 date -R      2. 修改设置时区          方法(1)            ...

  4. lintcode-【简单题】快乐数

    题目: 写一个算法来判断一个数是不是"快乐数". 一个数是不是快乐是这么定义的:对于一个正整数,每一次将该数替换为他每个位置上的数字的平方和,然后重复这个过程直到这个数变为1,或是 ...

  5. C++学习基础九——继承

    1.不同于Java中通过extends实现继承,C++是通过:实现的. 2.C++中同样包含public,private,protected三个关键字: public关键字表示在任意其他类中可调用该成 ...

  6. zedboard如何从PL端控制DDR读写(四)

    PS-PL之间的AXI 接口分为三种:• 通用 AXI(General Purpose AXI) — 一条 32 位数据总线,适合 PL 和 PS 之间的中低速通信.接口是透传的不带缓冲.总共有四个通 ...

  7. pgadmin中的备份功能消失的原因和找回方法

    pgadmin在正常情况下,在左侧的[对象浏览器]中选中数据库.模式或者表时,右键会出现[备份]和[恢复]的选项(除早期不支持该功能的版本外). 如: 当然,有时候有人会发现,当你想要用这些备份还原功 ...

  8. linux编程中printf显示不加换行的缓冲问题

    最近在编写linux网络编程时,总是遇到这样的事,程序逻辑没错误,但是程序运行到某个地方就停在那里了,后来才发现在prinrf()中加入换行能正常运行了,如“ printf("123&quo ...

  9. C++ JsonCpp 使用(含源码下载)

    C++ JsonCpp 使用(含源码下载) 前言 JSON是一个轻量级的数据定义格式,比起XML易学易用,而扩展功能不比XML差多少,用之进行数据交换是一个很好的选择JSON的全称为:JavaScri ...

  10. IE 下JS和CSS 阻塞后面内容总结

    总结: 1.  CSS 都是可以并行下载的. 2.  IE6 和 IE7   JS 不能并行下载,CSS 和 JS 阻塞后面内容下载. 3.  IE8   JS 还是会阻塞图片下载 开始改变加载模式, ...