GCD is Funny
GCD is Funny
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
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?
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.
on the board in increasing order.
4
1 2 3 4
4
2 2 2 2
5
5 6 2 3 4
2
1 2 3
#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的更多相关文章
- Objective-C三种定时器CADisplayLink / NSTimer / GCD的使用
OC中的三种定时器:CADisplayLink.NSTimer.GCD 我们先来看看CADiskplayLink, 点进头文件里面看看, 用注释来说明下 @interface CADisplayLin ...
- iOS 多线程之GCD的使用
在iOS开发中,遇到耗时操作,我们经常用到多线程技术.Grand Central Dispatch (GCD)是Apple开发的一个多核编程的解决方法,只需定义想要执行的任务,然后添加到适当的调度队列 ...
- 【swift】BlockOperation和GCD实用代码块
//BlockOperation // // ViewController.swift import UIKit class ViewController: UIViewController { @I ...
- 修改版: 小伙,多线程(GCD)看我就够了,骗你没好处!
多线程(英语:multithreading),是指从软件或者硬件上实现多个线程并发执行的技术.具有多线程能力的计算机因有硬件支持而能够在同一时间执行多于一个线程,进而提升整体处理性能.具有这种能力的系 ...
- GCD的相关函数使用
GCD 是iOS多线程实现方案之一,非常常用 英文翻译过来就是伟大的中枢调度器,也有人戏称为是牛逼的中枢调度器 是苹果公司为多核的并行运算提出的解决方案 1.一次性函数 dispatch_once 顾 ...
- hdu1695 GCD(莫比乌斯反演)
题意:求(1,b)区间和(1,d)区间里面gcd(x, y) = k的数的对数(1<=x<=b , 1<= y <= d). 知识点: 莫比乌斯反演/*12*/ 线性筛求莫比乌 ...
- hdu2588 GCD (欧拉函数)
GCD 题意:输入N,M(2<=N<=1000000000, 1<=M<=N), 设1<=X<=N,求使gcd(X,N)>=M的X的个数. (文末有题) 知 ...
- BZOJ 2820: YY的GCD [莫比乌斯反演]【学习笔记】
2820: YY的GCD Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 1624 Solved: 853[Submit][Status][Discu ...
- BZOJ 2818: Gcd [欧拉函数 质数 线性筛]【学习笔记】
2818: Gcd Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 4436 Solved: 1957[Submit][Status][Discuss ...
- GCD总结
//用block只有两种:同步执行/异步执行(参数1:队列;参数二:任务) dispatch_async(dispatch_get_global_queue(0, 0),^{ });//异步在新的线程 ...
随机推荐
- Ubuntu彻底删除mysql
删除 mysql sudo apt-get autoremove --purge mysql-server-5.0sudo apt-get remove mysql-serversudo apt-ge ...
- POJ 1789 Truck History(Prim+邻接矩阵)
( ̄▽ ̄)" #include<iostream> #include<cstdio> #include<cstring> #include<algo ...
- hdu_5788_Level Up(树状数组+主席树)
题目链接:hdu_5788_Level Up 题意: 有一棵树,n个节点,每个节点有个能力值A[i],mid[i],mid的值为第i节点的子树的中位数(包括本身),现在让你将其中的一个节点的A值改为1 ...
- hdu_2476_String painter(区间DP)
题目链接:hdu_2476_String painter 题意: 有a,b两字符串,现在你有一个刷子,每次可以任选一个区间,使这个区间变成你想要的字符,现在让你将a变成b,问最少刷多少次 题解: 考虑 ...
- Struts文件上传
首先要加入上传文件需要的jar文件 commons-fileupload-1.2.1.jar commomons-io-1.3.2.jar不同版本的strutsjar文件的版本也可能不同,一般在配置s ...
- 放弃阿里云主机,选择高性价比Vultr VPS免备案
阿里云主机ECS推广多年后,质量有所改善,但我依然强烈不推荐阿里云主机.考虑性价比带宽速度等因素后,我推荐的vps品牌有vultr和digitalocean,还有大名鼎鼎的linode,是中国用户的最 ...
- 给当前的URL添加/更新新的参数
/** * 给当前的URL添加/更新新的参数 * @param a 参数 * @param b 值 * @returns {string} 新的参数 * @constructor */ functio ...
- $each $position $sort $slice
$push 向数组中添加元素 $each 循环数据(循环添加数据到数组) $sort 对数组进行排序(1:升序:-1:降序.) $slice 对整个collection表进行数据裁减,用的时候一定要当 ...
- 第二次冲刺spring会议(第三次会议)
[例会时间]2014/5/6 21:15 [例会地点]9#446 [例会形式]轮流发言 [例会主持]马翔 [例会记录]兰梦 小组成员:兰梦 ,马翔,李金吉,赵天,胡佳奇
- 【卷一】正则一 之re.split
有时候,用re.split()匹配字符串会比findall,search, match等 正则表达式对象方法方便简洁很多! 参考: <Python核心编程(3rd)>—p23 如果给定分隔 ...