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. (转)Java程序员应该了解的10个面向对象设计原则

    面向对象设计原则是OOPS(Object-Oriented Programming System,面向对象的程序设计系统)编程的核心,但大多数Java程序员追逐像Singleton.Decorator ...

  2. input编辑框编辑状态切换

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  3. 字符串匹配—KMP 扩展KMP Manacher

    kuangbin字符串专题传送门--http://acm.hust.edu.cn/vjudge/contest/view.action?cid=70325#overview 算法模板: KMP: ; ...

  4. SSL单向认证和双向认证原理

    注:本文为个人学习摘录,原文地址:http://edison0663.iteye.com/blog/996526 为了便于更好的认识和理解 SSL 协议,这里着重介绍 SSL 协议的握手协议.SSL ...

  5. ubuntu 14.04 32位库

    如果是ubuntu 14.04,则请先执行: 方法1: sudo gedit /etc/apt/sources.list 然后在最后添加上: deb http://archive.ubuntu.com ...

  6. vultr和digitalocean vps使用感受

    中国用户购买海外vps看中的是欧美vps性价比高,不必备案,隐私保护好.但是,由于涉及到vps代理推广,国内涌现大量vps推广站点,他们纯粹发布一些vps促销信息去吸引潜在买家,甚至根本没有使用过所推 ...

  7. 【简单学习shell】iptables命令实用

    构造设备离线iptables命令iptables -I INPUT -p all -s 10.71.115.159 -j DROP 断链iptables -I INPUT -p all -s 10.7 ...

  8. 关于ios 程序加载百度地图lib,出现链接错误:找不到符号 (null): _OBJC_CLASS_$_BMKMapManager的解决办法

    报告的错误信息 ld: warning: ignoring file /Users/5012/Documents/sphuang/IOS_project/baidu_map/ShareLocation ...

  9. mysql5.7.16安装

    系统:centOS6.5 mysql: 5.7.16 wget http://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.16-linux-glib ...

  10. HADOOP与ORACLE关联

    安装Oracle和Oracle大数据连接器/OLH,尝试把HDFS中的数据文件装载到Oracle中的表 http://f.dataguru.cn/thread-460110-1-1.html 文档讲述 ...