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. JavaScript忍者秘籍——函数(下)

    概要:本篇博客主要介绍函数的一些类型以及常见示例 1.匿名函数 使用匿名函数的常见示例: window.onload = function(){ assert(true,'power!'); }; / ...

  2. ssh: scp命令

    scp 复制命令 Eg. -r /tmp/q1 root@[::]/home 1.命令格式: scp [参数] [原路径] [目标路径] 2.命令功能: scp是 secure copy的缩写, sc ...

  3. java 视频中截图

    package com.sun.test; import java.io.File; import java.util.List; public class CreatePh { //public s ...

  4. POJ 1042 Gone Fishing#贪心

    (- ̄▽ ̄)-* #include<iostream> #include<cstdio> #include<cstring> using namespace std ...

  5. 如何理解CSS中的浮动 :其实他就像乘坐扶梯一样

    只要你用过自动扶梯,你就能很快的理解CSS中的浮动(Float). 你肯定遇到过这样的情况:       做好了,你想用CSS浮动来调整元素间的位置关系. 在写完代码之后,你发现浮动元素没出现在你设想 ...

  6. MVC3+EF4.1学习系列(七)-----EF并发的处理

    看这篇文章之前 推荐园子里的 这个文章已经有介绍了 而且写的很好~~ 可以先看下他的 再看我的 并发 1.悲观并发 简单的说 就是一个用户访问一条数据时 则把这个数据变为只读属性  把该数据变为独占 ...

  7. NOIP2002-普及组复赛-第二题-级数求和

    题目描述 Description 已知:Sn= 1+1/2+1/3+…+1/n.显然对于任意一个整数K,当n足够大的时候,Sn大于K. 现给出一个整数K(1<=k<=15),要求计算出一个 ...

  8. 关于MTK平台SIM-ME Lock的配置方案

    针对一些运营商的锁网需求,MTK平台已经对其有很好的支持.绝大多数的海外需求可以通过直接配置相关文件来完成.这里简单描述一下配置方法,不做原理分析. 相关数据结构分析: Modem中与SML锁网配置相 ...

  9. css3动画 9步

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  10. JPA 系列教程17-继承-独立表-TABLE_PER_CLASS

    PerTable策略 每个具体的类一个表的策略 举例 这种映射策略每个类都会映射成一个单独的表,类的所有属性,包括继承的属性都会映射成表的列. 这种映射策略的缺点是:对多态关系的支持有限,当查询涉及到 ...