AtCoder Grand Contest 018 A - Getting Difference
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
4 11
11 3 7 15
Sample Output 3
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的更多相关文章
- 【GCD】AtCoder Grand Contest 018 A - Getting Difference
从大到小排序,相邻两项作差,求gcd,如果K是gcd的倍数并且K<=max{a(i)},必然有解,否则无解. 可以自己手画画证明. #include<cstdio> #include ...
- AtCoder Grand Contest 018 A
A - Getting Difference Time limit時間制限 : 2sec / Memory limitメモリ制限 : 256MB 配点 : 300 点 問題文 箱に N 個のボールが入 ...
- AtCoder Grand Contest 018 D - Tree and Hamilton Path
题目传送门:https://agc018.contest.atcoder.jp/tasks/agc018_d 题目大意: 给定一棵\(N\)个点的带权树,求最长哈密顿路径(不重不漏经过每个点一次,两点 ...
- AtCoder Grand Contest 018 E Sightseeing Plan
题意: 给定三个矩形,选定三个点,答案加上第一个点出发经过第二个点在第三个点结束的方案数,只能往右或往下走. 折腾了我半个多下午的题. 设三个矩形为$A,B,C$一个思路是枚举$B$的那个点$s(x, ...
- 【贪心】【堆】AtCoder Grand Contest 018 C - Coins
只有两维的时候,我们显然要按照Ai-Bi排序,然后贪心选取. 现在,也将人按照Ai-Bi从小到大排序,一定存在一个整数K,左侧的K个人中,一定有Y个人取银币,K-Y个人取铜币: 右侧的X+Y+Z-K个 ...
- 【贪心】AtCoder Grand Contest 018 B - Sports Festival
假设我们一开始选取所有的运动项目,然后每一轮将当前选择人数最多的运动项目从我们当前的项目集合中删除,尝试更新答案.容易发现只有这样答案才可能变优,如果不动当前选取人数最多的项目,答案就不可能变优. 我 ...
- AtCoder Grand Contest 018题解
传送门 \(A\) 根据裴蜀定理显然要\(k|\gcd(a_1,...,a_n)\),顺便注意不能造出大于\(\max(a_1,...,a_n)\)的数 int n,g,k,x,mx; int mai ...
- AtCoder Grand Contest 012
AtCoder Grand Contest 012 A - AtCoder Group Contest 翻译 有\(3n\)个人,每一个人有一个强大值(看我的假翻译),每三个人可以分成一组,一组的强大 ...
- AtCoder Grand Contest 011
AtCoder Grand Contest 011 upd:这篇咕了好久,前面几题是三周以前写的... AtCoder Grand Contest 011 A - Airport Bus 翻译 有\( ...
随机推荐
- win环境操作mysql
第一步:登录数据库 第二步:查看数据库 第三步:进入数据库 第四步:创建表 第五步:删除数据库 第六步:进入数据库查看表状态
- AWK的介绍学习
第一节.awk的工作流程和基本用法 1.awk介绍 awk是一种报表生成器,就是对文件进行格式化处理的,这里的格式化不是文件系统的格式化,而是对文件内容进行各种"排版",进而格式化 ...
- 如何解决本地仓库和远程仓库的冲突(Conflict)
Background: 我有一个github仓库管理我的代码,我将这个仓库的代码clone到我的工作电脑和私人电脑本地方便我上班和在家时都可以对我的代码进行更新. 一天,我在家修改过代码之后并未提交, ...
- MySQL auttoReconnect
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from ...
- Python 调用snmp自定义OID实现监控
http://kkkkkk.blog.51cto.com/468162/1172726 http://blog.csdn.net/mirahs/article/details/49619729
- 洛谷 P2695 骑士的工作
P2695 骑士的工作 题目背景 你作为一个村的村长,保卫村庄是理所当然的了.今天,村庄里来了一只恶龙,他有n个头,恶龙到处杀人放火.你着急了.不过天无绝人之路,现在来了一个骑士团.里面有m位成员(往 ...
- hadoop(八) - sqoop安装与使用
一. sqoop安装: 安装在一台节点上就能够了. 1. 使用winscp上传sqoop 2. 安装和配置 加入sqoop到环境变量 将数据库连接驱动mysql-connector-5.1.8.jar ...
- elasticsearch搜索类型简单介绍
简单搜索 GET请求很easy--你能轻松获取你想要的文档.让我们来进一步尝试一些东西.比方简单的搜索! 我们尝试一个最简单的搜索所有员工的请求: GET /megacorp/employee/_se ...
- 7-07. PAT排名汇总(25) (结构体 ZJU_PAT)
题目链接:http://www.patest.cn/contests/ds/7-07 编程能力測试(Programming Ability Test,简称PAT)是浙江大学计算机科学与技术学院主办的专 ...
- java实现折半查找
package althorgrim;/** * 1.必须采用顺序存储结果 * 2.关键字必须有序 * @author hanrk-2734 * */public class TestBinarySe ...