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 翻译 有\( ...
随机推荐
- OpenGL编程(一)渲染一个指定颜色的背景窗口
上次已经搭好了OpenGL编程的环境.已经成功运行了第一个程序.可只是照搬书上的代码,并没弄懂其中的原理.这次通过一个小程序来解释使用GLUT库编写OpenGL程序的过程. 程序的入口 与其他程序一样 ...
- RSA不对称加密
package sinRsa; import java.io.ByteArrayOutputStream; import java.io.FileInputStream; import java.io ...
- NodeJS学习笔记 (26)命令行设计-repl
https://github.com/chyingp/nodejs-learning-guide
- cal---显示日历
cal命令用于显示当前日历,或者指定日期的日历. 语法 cal(选项)(参数) 选项 -l:显示单月输出: -3:显示临近三个月的日历: -s:将星期日作为月的第一天: -m:将星期一作为月的第一天: ...
- vue使用axios中 this 指向问题
1.解决办法 在vue中使用axios做网络请求的时候,会遇到this不指向vue,而为undefined,可以使用箭头函数"=>"来解决.如下: methods: { lo ...
- Unity Shader实现各种进度条
1.圆形进度条shader Shader "ProgressBar360" { Properties { _BGTex("Background Texture" ...
- ArcGIS api for javascript——加入两个动态地图
描述 这个示例表现如何加两个动态地图到一个地图.动态服务按用户缩放或平移服务器每次绘制的地图,ArcGISDynamicMapServiceLayer表示ArcGIS JavaScript API动态 ...
- 一个工作快八年的老IT人士这几年沉浮总结的职场经验教训
曾经我挺喜欢写博客,认为把心中的想法表达出来非常畅快.而且还能和网上非常多人沟通交流,如今我更喜欢把想法留在心中.博客非常久没更新了,刚才闲来无事.看看职场话题版块发现非常多人都挺迷茫的,所以我写一些 ...
- poj--2007--Scrambled Polygon(数学几何基础)
Scrambled Polygon Time Limit: 1000MS Memory Limit: 30000KB 64bit IO Format: %I64d & %I64u Su ...
- BZOJ 3236 莫队+树状数组
思路: 莫队+树状数组 (据说此题卡常数) yzy写了一天(偷笑) 复杂度有点儿爆炸 O(msqrt(n)logn) //By SiriusRen #include <cmath> #in ...