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),^{ });//异步在新的线程 ...
随机推荐
- Node.js:服务器与数据流
1.Node 常被用来构建服务器,下面代码就是创建了一个服务器. var http = require('http'); var server = http.createServer(); serve ...
- PHP数据类型转换(字符转数字,数字转字符)
PHP的数据类型转换属于强制转换,允许转换的PHP数据类型有: (int).(integer):转换成整形 (float).(double).(real):转换成浮点型 (string):转换成字符串 ...
- 更改自身web项目的图标(默认为tomcat的小喵咪)
在页面<head>标签中加入 <link rel="shortcut icon" href="img/11.png" type="i ...
- json格式数据 ,将数据库中查询的结果转换为json(方式2)
controller: /*** * 返回所有版本的信息,json的形式返回到前台 * @return */ @RequestMapping(value="/getAllVersion&qu ...
- hdu_5881_Tea(xjb猜)
题目链接:hdu_5881_Tea 题意: 有一壶水, 体积在 L 和 R 之间, 有两个杯子, 你要把水倒到两个杯子里面, 使得杯子水体积几乎相同(体积的差值小于等于1), 并且使得壶里剩下水体积不 ...
- 2、hibernate七步走完成增删改查
一.hibernate框架介绍如下 1.框架=模板 2.Hibernate是对象模型与关系数据库模型之间的桥梁 3.hibernate持久化概念 什么是ORM ORM是对象关系映射,是一种数据持久化操 ...
- Django的使用
Django使用介绍 1.MTV Django中的MTV分别表示models.templates和views. models文件主要定义数据库的连接. templates文件可以放一些html的模版. ...
- kettle连接mysql
kettle连接mysql时出现问题
- Mysql innodb 间隙锁
前段时间系统老是出现insert死锁,很是纠结.经过排查发现是间隙锁!间隙锁是innodb中行锁的一种, 但是这种锁锁住的却不止一行数据,他锁住的是多行,是一个数据范围.间隙锁的主要作用是为了防止出现 ...
- JS全选
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...