A - Getting Difference


Time limit : 2sec / Memory limit : 256MB

Score : 300 points

Problem Statement

There is a box containing N balls. The i-th ball has the integer Ai written on it. Snuke can perform the following operation any number of times:

  • Take out two balls from the box. Then, return them to the box along with a new ball, on which the absolute difference of the integers written on the two balls is written.

Determine whether it is possible for Snuke to reach the state where the box contains a ball on which the integer K is written.

Constraints

  • 1≤N≤105
  • 1≤Ai≤109
  • 1≤K≤109
  • All input values are integers.

Input

Input is given from Standard Input in the following format:

N K
A1 A2 AN

Output

If it is possible for Snuke to reach the state where the box contains a ball on which the integer K is written, print POSSIBLE; if it is not possible, print IMPOSSIBLE.


Sample Input 1

3 7
9 3 4

Sample Output 1

POSSIBLE

First, take out the two balls 9 and 4, and return them back along with a new ball, abs(9−4)=5. Next, take out 3 and 5, and return them back along with abs(3−5)=2. Finally, take out 9 and 2, and return them back along with abs(9−2)=7. Now we have 7 in the box, and the answer is therefore POSSIBLE.


Sample Input 2

3 5
6 9 3

Sample Output 2

IMPOSSIBLE

No matter what we do, it is not possible to have 5 in the box. The answer is therefore IMPOSSIBLE.


Sample Input 3

Copy
4 11
11 3 7 15

Sample Output 3

Copy
POSSIBLE

The box already contains 11 before we do anything. The answer is therefore POSSIBLE.


Sample Input 4

5 12
10 2 8 6 4

Sample Output 4

IMPOSSIBLE
首先,k>max(a[0],a[1],a[2],.......)进行特判,一定不满足,之后,对序列两两之间取GCD,判断即可
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <iomanip>
#include <cmath>
#include <ctime>
#include <map>
#include <set>
using namespace std;
#define lowbit(x) (x&(-x))
#define max(x,y) (x>y?x:y)
#define min(x,y) (x<y?x:y)
#define MAX 100000000000000000
#define MOD 1000000007
#define pi acos(-1.0)
#define ei exp(1)
#define PI 3.141592653589793238462
#define INF 0x3f3f3f3f3f
#define mem(a) (memset(a,0,sizeof(a)))
typedef long long ll;
set<ll>s;
ll a[],n,k;
ll gcd(ll x,ll y)
{
return y==?x:gcd(y,x%y);
}
int main()
{
scanf("%lld%lld",&n,&k);
int ok=,maxn=-;
for(int i=;i<n;i++)
{
cin>>a[i];
if(a[i]==k)ok=;
maxn=max(maxn,a[i]);
}
sort(a,a+n);
if(ok) {puts("POSSIBLE");return ;}
if(k>maxn) {puts("IMPOSSIBLE");return ;}
for(int i=;i<n;i++)
{
s.insert(gcd(a[i],a[i-]));
}
for(set<ll>::iterator it=s.begin();it!=s.end();it++)
{
if(k%*it==)
{
puts("POSSIBLE");
return ;
}
}
puts("IMPOSSIBLE");
return ;
}

AtCoder Grand Contest 018 A - Getting Difference的更多相关文章

  1. 【GCD】AtCoder Grand Contest 018 A - Getting Difference

    从大到小排序,相邻两项作差,求gcd,如果K是gcd的倍数并且K<=max{a(i)},必然有解,否则无解. 可以自己手画画证明. #include<cstdio> #include ...

  2. AtCoder Grand Contest 018 A

    A - Getting Difference Time limit時間制限 : 2sec / Memory limitメモリ制限 : 256MB 配点 : 300 点 問題文 箱に N 個のボールが入 ...

  3. AtCoder Grand Contest 018 D - Tree and Hamilton Path

    题目传送门:https://agc018.contest.atcoder.jp/tasks/agc018_d 题目大意: 给定一棵\(N\)个点的带权树,求最长哈密顿路径(不重不漏经过每个点一次,两点 ...

  4. AtCoder Grand Contest 018 E Sightseeing Plan

    题意: 给定三个矩形,选定三个点,答案加上第一个点出发经过第二个点在第三个点结束的方案数,只能往右或往下走. 折腾了我半个多下午的题. 设三个矩形为$A,B,C$一个思路是枚举$B$的那个点$s(x, ...

  5. 【贪心】【堆】AtCoder Grand Contest 018 C - Coins

    只有两维的时候,我们显然要按照Ai-Bi排序,然后贪心选取. 现在,也将人按照Ai-Bi从小到大排序,一定存在一个整数K,左侧的K个人中,一定有Y个人取银币,K-Y个人取铜币: 右侧的X+Y+Z-K个 ...

  6. 【贪心】AtCoder Grand Contest 018 B - Sports Festival

    假设我们一开始选取所有的运动项目,然后每一轮将当前选择人数最多的运动项目从我们当前的项目集合中删除,尝试更新答案.容易发现只有这样答案才可能变优,如果不动当前选取人数最多的项目,答案就不可能变优. 我 ...

  7. AtCoder Grand Contest 018题解

    传送门 \(A\) 根据裴蜀定理显然要\(k|\gcd(a_1,...,a_n)\),顺便注意不能造出大于\(\max(a_1,...,a_n)\)的数 int n,g,k,x,mx; int mai ...

  8. AtCoder Grand Contest 012

    AtCoder Grand Contest 012 A - AtCoder Group Contest 翻译 有\(3n\)个人,每一个人有一个强大值(看我的假翻译),每三个人可以分成一组,一组的强大 ...

  9. AtCoder Grand Contest 011

    AtCoder Grand Contest 011 upd:这篇咕了好久,前面几题是三周以前写的... AtCoder Grand Contest 011 A - Airport Bus 翻译 有\( ...

随机推荐

  1. pandas学习系列(一):时间序列

    最近参加了天池的一个机场航空人流量预测大赛,需要用时间序列来预测,因此开始使用python的pandas库 发现pandas库功能的确很强大,因此在这记录我的pandas学习之路. # -*- cod ...

  2. 一篇文章助你理解Python3中字符串编码问题

    前几天给大家介绍了unicode编码和utf-8编码的理论知识,以及Python2中字符串编码问题,没来得及上车的小伙伴们可以戳这篇文章:浅谈unicode编码和utf-8编码的关系和一篇文章助你理解 ...

  3. bzoj5085: 最大 暴力 二分

    Code: #include<cstdio> #include<algorithm> #include<cstring> #include<iostream& ...

  4. lhgDialog使用--loading提示(不自动关闭)

    使用lhgDialog时,发现有一个$.dialog.tips()方法可以实现loading样式的提示,但是存在默认关闭时间.方法如下图所示, 为了实现不自动关闭的方法,查看了相应的源码后,实现不关闭 ...

  5. CF 986A Fair(多源BFS)

    题目描述 一些公司将在Byteland举办商品交易会(or博览会?).在Byteland有 nnn 个城市,城市间有 mmm 条双向道路.当然,城镇之间两两连通. Byteland生产的货物有 kkk ...

  6. 紫书 习题 10-12 UVa 557(概率计算)

    开始的时候我没有考虑1/2的概率,直接一波组合数,然后WA 后来去看题解发现我们可以反过来想,求最后两个人不一样的情况 这个时候肯定会抛到最后的 所以每一种可能就是(0.5)^(n - 2),然后一共 ...

  7. 题解 CF327C 【Magic Five】

    这道题带坑,假如没有发现肯定会爆. 首先先搜索一遍0和5,储存在数组a里面. 那么应当有2 ^ a1 +2 ^ a2 +...+ 2 ^ an. 然而这道题没那么简单,数串还可以重复k次. 因此,需要 ...

  8. Java基础学习总结(18)——网络编程

    一.网络基础概念 首先理清一个概念:网络编程 != 网站编程,网络编程现在一般称为TCP/IP编程. 二.网络通信协议及接口 三.通信协议分层思想 四.参考模型 五.IP协议 每个人的电脑都有一个独一 ...

  9. zookeeper 性能测试

    zookeeper压力测试:性能对比(3个节点,5个节点,7个节点 创建节点.删除节点.设置节点数据.读取节点数据性能及并发性能) 测试结果如下: 五次测试三节点结果: 创建100W节点用时:15.0 ...

  10. 使用ThoughtWorks.QRCode生成二维码

    新建Windows应用程序,加入引用ThoughtWorks.QRCode.dll,编写代码生成二维码. using System; using System.Drawing; using Syste ...