GCD is Funny

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 $n$
integers at a board and he performs the following moves repeatedly:

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
There are multiple test cases. The first line of input
contains an integer $T$ $(1 \le T \le 100)$, indicating the number of test
cases. For each test case:

The first line contains an integer $n$ $(3 \le
n \le 500)$ -- the number of integers written on the board. The next line
contains $n$ integers: $a_1, a_2, ..., a_n$ $(1 \le a_i \le 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
分析:大意:给你>=3个数,每次取3个数去掉,添上其中两个数的gcd的2遍,最后剩两个相同数是多少?
   仔细分析可知第一次取两个数gcd,去掉另一个数,以后就可以任取剩下n-3个数作gcd;
   所以就是要求2~n-1个数的gcd,模拟即可;
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <unordered_map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, ls[rt]
#define Rson mid+1, R, rs[rt]
#define sys system("pause")
#define intxt freopen("in.txt","r",stdin)
const int maxn=1e3+;
using namespace std;
int gcd(int p,int q){return q==?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
inline ll read()
{
ll x=;int f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n,m,k,t,a[maxn],ok[maxn];
queue<pii>p;
int main()
{
int i,j;
scanf("%d",&t);
while(t--)
{
memset(ok,,sizeof(ok));
while(!p.empty())p.pop();
scanf("%d",&n);
rep(i,,n)scanf("%d",&a[i]);
rep(i,,n)rep(j,i+,n)
{
k=gcd(a[i],a[j]);
if(!ok[k])ok[k]=,p.push(mp(k,));
}
while(!p.empty())
{
pii q=p.front();
p.pop();
if(q.se==n-)break;
rep(i,,n)
{
k=gcd(q.fi,a[i]);
if(!ok[k])ok[k]=,p.push(mp(k,q.se+));
}
}
bool flag=false;
rep(i,,)
{
if(ok[i])
{
if(flag)printf(" %d",i);
else printf("%d",i),flag=true;
}
}
printf("\n");
}
//system("Pause");
return ;
}

GCD is Funny的更多相关文章

  1. Objective-C三种定时器CADisplayLink / NSTimer / GCD的使用

    OC中的三种定时器:CADisplayLink.NSTimer.GCD 我们先来看看CADiskplayLink, 点进头文件里面看看, 用注释来说明下 @interface CADisplayLin ...

  2. iOS 多线程之GCD的使用

    在iOS开发中,遇到耗时操作,我们经常用到多线程技术.Grand Central Dispatch (GCD)是Apple开发的一个多核编程的解决方法,只需定义想要执行的任务,然后添加到适当的调度队列 ...

  3. 【swift】BlockOperation和GCD实用代码块

    //BlockOperation // // ViewController.swift import UIKit class ViewController: UIViewController { @I ...

  4. 修改版: 小伙,多线程(GCD)看我就够了,骗你没好处!

    多线程(英语:multithreading),是指从软件或者硬件上实现多个线程并发执行的技术.具有多线程能力的计算机因有硬件支持而能够在同一时间执行多于一个线程,进而提升整体处理性能.具有这种能力的系 ...

  5. GCD的相关函数使用

    GCD 是iOS多线程实现方案之一,非常常用 英文翻译过来就是伟大的中枢调度器,也有人戏称为是牛逼的中枢调度器 是苹果公司为多核的并行运算提出的解决方案 1.一次性函数 dispatch_once 顾 ...

  6. hdu1695 GCD(莫比乌斯反演)

    题意:求(1,b)区间和(1,d)区间里面gcd(x, y) = k的数的对数(1<=x<=b , 1<= y <= d). 知识点: 莫比乌斯反演/*12*/ 线性筛求莫比乌 ...

  7. hdu2588 GCD (欧拉函数)

    GCD 题意:输入N,M(2<=N<=1000000000, 1<=M<=N), 设1<=X<=N,求使gcd(X,N)>=M的X的个数.  (文末有题) 知 ...

  8. BZOJ 2820: YY的GCD [莫比乌斯反演]【学习笔记】

    2820: YY的GCD Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 1624  Solved: 853[Submit][Status][Discu ...

  9. BZOJ 2818: Gcd [欧拉函数 质数 线性筛]【学习笔记】

    2818: Gcd Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 4436  Solved: 1957[Submit][Status][Discuss ...

  10. GCD总结

    //用block只有两种:同步执行/异步执行(参数1:队列;参数二:任务) dispatch_async(dispatch_get_global_queue(0, 0),^{ });//异步在新的线程 ...

随机推荐

  1. servlet规范核心类图

    作为新手在写servlet时很多时候忘记类与类之间的关系,找到这张图就瞬间清晰了,这比看API要舒服很多.

  2. php缓存总结

    php缓存技术: 1.全页面静态化缓存;2.页面部分缓存;3.数据缓存;4.查询缓存;5.按内容变更进行缓;6.内存式缓存;7.apache缓存模块;8.php APC缓存扩展;9.Opcode缓存. ...

  3. hdu 1003,nefu 728 max sum

    Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a sub ...

  4. 高精度运算专题3-乘法运算(The multiplication operation)

    这个专题呢,我就来讲讲高精度的乘法,下面是三个计算乘法的函数,第一个函数是char类型的,要对字符串进行数字转换,而第二个是两个int类型的数组,不用转换成数字,第三个则更为优化,用a数组-b数组放回 ...

  5. 关于UIFont和计算字符串的高度和宽度

    转自:http://i.cnblogs.com/EditPosts.aspx?opt=1 1.创建方法:+ fontWithName:size:- fontWithSize:2.创建系统字体:+ sy ...

  6. [Mark] openvswitch megaflow

    Megaflow is an OpenVSwitch (OVS) feature than enables us to install flows for which some fields are ...

  7. 1.0 Python 学习网站

    w3cschool : http://www.runoob.com/python/python-tutorial.html cnblog Python 从入门到精通:  http://www.cnbl ...

  8. iOS学习笔记(02) - 关键字 __kindof

    1.__kindof:表示当前类或它的子类. 2.__kindof书写格式:放在类型前面,表示修饰这个类型. 3.__kindof优点:在调用的时候,很清楚的知道返回类型. 直接举一个例子来形容这个问 ...

  9. 《JavaScript高级程序设计》读书笔记 ---变量、作用域和内存问题小结

    JavaScript 变量可以用来保存两种类型的值:基本类型值和引用类型值.基本类型的值源自以下5种基本数据类型:Undefined.Null.Boolean.Number 和String.基本类型值 ...

  10. 新学的js精集

    逻辑运算符比较 逻辑与和逻辑或这两个逻辑运算符它不单单会输出布尔型数据 1.只要"||"前面为false,无论"||"后面是true还是false,结果都返回& ...