AtCoder Grand Contest 018 A
A - Getting Difference
Time limit時間制限 : 2sec / Memory limitメモリ制限 : 256MB
配点 : 300 点
問題文
箱に N 個のボールが入っていて、i 番目のボールには整数 Ai が書かれています。 すぬけ君は、次の操作を好きな回数だけ行うことができます。
- 箱から二つのボールを取り出し、その二つのボールに書かれている数の差の絶対値を書いた新しいボールと一緒に箱に戻す。
すぬけ君が、整数 K の書かれたボールが箱の中に入っている状態にできるかどうか判定してください。
制約
- 1≤N≤105
- 1≤Ai≤109
- 1≤K≤109
- 入力はすべて整数である。
入力
入力は以下の形式で標準入力から与えられる。
N K
A1 A2 … AN
出力
すぬけ君が、整数 K がかかれたボールが箱の中に入っている状態にできる場合には POSSIBLE
、 できない場合には IMPOSSIBLE
と出力せよ。
入力例 1
3 7
9 3 4
出力例 1
POSSIBLE
まず、9 と書かれたボールと 4 と書かれたボールを取り出し、abs(9−4)=5 なので、5 と書かれた新しいボールと一緒に箱に戻します。 次に、3 と書かれたボールと 5 と書かれたボールを取り出し、abs(3−5)=2 なので、2 と書かれた新しいボールと一緒に箱に戻します。 最後に、9 と書かれたボールと 2 と書かれたボールを取り出し、abs(9−2)=7 なので、7 と書かれた新しいボールと一緒に箱に戻します。 7 と書かれたボールを箱に入れることができたので、この例の答えは POSSIBLE
になります。
入力例 2
3 5
6 9 3
出力例 2
IMPOSSIBLE
どれだけ操作を行っても、5 の書かれたボールを箱の中に入れることはできません。 よってこの例の答えは、IMPOSSIBLE
になります。
入力例 3
4 11
11 3 7 15
出力例 3
POSSIBLE
操作を行うまでもなく、箱の中には 11 の書かれたボールが入っています。 よってこの例の答えは、POSSIBLE
になります。
入力例 4
5 12
10 2 8 6 4
出力例 4
IMPOSSIBLE
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
不知天高地厚的去尝试了一波Atcoder 果不其然 只写出来一道题 还是大爷提醒一波才会的 所以我们就来讲讲这第一题吧
一句话题意就是 给你n个数以及一个k 我们可以从n个数中拿出两个数 相减得到一个新的数(也就是多了这一个新的数,新的数也可以参与操作) 问能否得到k
分析一波易得 我们只考虑两个数 那个根据更相减损术 我们可以利用相减得到一波gcd(也就是最大公约数)
那么两个数 x y 都是gcd的倍数 他们无论怎么相减都会是gcd的倍数
把这个结论推广到n'个数 那么答案就是n个数的gcd啦 2333
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define LL long long
using namespace std;
const int M=1e6+;
int read(){
int ans=,f=,c=getchar();
while(c<''||c>''){if(c=='-') f=-; c=getchar();}
while(c>=''&&c<=''){ans=ans*+(c-''); c=getchar();}
return ans*f;
}
int n,k,num[M],ans,mx;
bool f;
int gcd(int x,int y){
while(y){
int p=x%y;
x=y;
y=p;
}
return x;
}
int main()
{
n=read(); k=read();
for(int i=;i<=n;i++){
num[i]=read();
mx=max(mx,num[i]);
if(num[i]==k) f=;
}
if(f){printf("POSSIBLE\n"); return ;}
if(mx<k){printf("IMPOSSIBLE\n"); return ;}
ans=gcd(num[],num[]);
for(int i=;i<=n;i++) ans=gcd(ans,num[i]);
if(k%ans==) printf("POSSIBLE\n");
else printf("IMPOSSIBLE\n");
return ;
}
AtCoder Grand Contest 018 A的更多相关文章
- 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
假设我们一开始选取所有的运动项目,然后每一轮将当前选择人数最多的运动项目从我们当前的项目集合中删除,尝试更新答案.容易发现只有这样答案才可能变优,如果不动当前选取人数最多的项目,答案就不可能变优. 我 ...
- 【GCD】AtCoder Grand Contest 018 A - Getting Difference
从大到小排序,相邻两项作差,求gcd,如果K是gcd的倍数并且K<=max{a(i)},必然有解,否则无解. 可以自己手画画证明. #include<cstdio> #include ...
- AtCoder Grand Contest 018 A - Getting Difference
A - Getting Difference Time limit : 2sec / Memory limit : 256MB Score : 300 points Problem Statement ...
- 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 翻译 有\( ...
随机推荐
- JavaSE 第二次学习随笔(String的坑 + ==)
String 类是一个final类, 其内部是使用的 private final char value[]; 来存储内容, 其既可以当作一个基本类型来使用也可以当作一个类来使用;final 类(Str ...
- C++ 基础 初始化列表
当一个类组合了其他类,或者使用了 const 成员,就要用 初始化列表. Class A {...}; Class B {...}; Class C { private: A a; B b; int ...
- 开放定址法——线性探测(Linear Probing)
之前我们所采用的那种方法,也被称之为封闭定址法.每个桶单元里存的都是那些与这个桶地址比如K相冲突的词条.也就是说每个词条应该属于哪个桶所对应的列表,都是在事先已经注定的.经过一个确定的哈希函数,这些绿 ...
- PHP.19-验证码生成
生成验证码 思路:先定义验证码函数getCode() //绘制验证码 $num = 4; //字符长度 getCode($num, 2); 1.创建画布,分配颜色 imagecreatetruecol ...
- ClassNotFountException 与 NoClassDefineError
一 知识准备 NoClassDefFoundError:正如它们的名字所说明的是一个错误 (Error),而ClassNotFoundException是一个异常.正如上一章节所说Exception和 ...
- ansible-1
ansible与salt对比: 相同: 都是为了同时在多台机器上执行相同的命令 都是python开发 不同: agent(saltstack需要安装.ansible不需要) 配置(salt配置麻烦,a ...
- java练习题——字符串
一.动手动脑之String.equals()方法: 判断s1和s2的内容相同s1.equals(s2). 判断s1和s2的地址相同s1 == s2. 二.整理String类的Length().char ...
- 如何将多个Eclipse项目导入IntelliJ IDEA
技术交流群:233513714 IntelliJ IDEA 与Eclipse在新建项目上的叫法略有不同,区别见下图. 当我们使用idea后再次使用eclipse时就会有很多不适,下面介绍一个多项目的导 ...
- Idea中maven依赖图查看
技术交流群: 233513714 使用Intellij idea,想看看它的maven依赖图,根据eclipse的经验,不是很容易能找到Intellij idea对应的功能.在打开的pom.xml文件 ...
- centos使用--ssh登陆
1 安装openssh-server yum install openssh-server 2 登录 在配置好ssh后就可以只使用SSH密钥登录而不允许通过密码登录了,方法如下: 修改ssh配置: v ...