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

Copy
3 7
9 3 4

出力例 1

Copy
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

Copy
3 5
6 9 3

出力例 2

Copy
IMPOSSIBLE

どれだけ操作を行っても、5 の書かれたボールを箱の中に入れることはできません。 よってこの例の答えは、IMPOSSIBLE になります。


入力例 3

Copy
4 11
11 3 7 15

出力例 3

Copy
POSSIBLE

操作を行うまでもなく、箱の中には 11 の書かれたボールが入っています。 よってこの例の答えは、POSSIBLE になります。


入力例 4

Copy
5 12
10 2 8 6 4

出力例 4

Copy
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

Copy
3 7
9 3 4

Sample Output 1

Copy
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

Copy
3 5
6 9 3

Sample Output 2

Copy
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

Copy
5 12
10 2 8 6 4

Sample Output 4

Copy
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 ;
}

最新文章

  1. Linux简介及常用命令使用3--vi编辑器
  2. plupload简易应用 多图片上传显示预览以及删除
  3. 迷宫问题求解之“A*搜索”(二)
  4. poj: 2159
  5. (笔记)angular material radio用法
  6. Java多线程---------同步与死锁:synchronized;等待与唤醒:wait、notify、notifyAll;生命周期
  7. Java基础知识强化之集合框架笔记20:数据结构之 栈 和 队列
  8. XML的四种解析方式
  9. eclipse安装svn插件,在输入url后,一直卡在in progress界面不懂。
  10. Linux 查看文件
  11. QT 初试 MainWindow简易窗体
  12. Oracle问题之ORA-01609、ORA-00362
  13. Java接口的幂等性设计
  14. 利用media query让背景图适应不同分辨率的设备
  15. arm-linux 裸机下 VNC 的实现
  16. Debugging Java Native Memory Leaks
  17. JDBC创建表实例
  18. form enctype:&quot;multipart/form-data&quot;,method:&quot;post&quot; 提交表单,后台获取不到数据
  19. volatile的语义与实现
  20. django的用户认证组件

热门文章

  1. java实现 zip解压缩
  2. Laravel操作上传文件的方法
  3. PHP 输出控制
  4. 笔记-git-.gitignore
  5. Hibernate---实体类注释简介
  6. pdo事务
  7. hadoop进阶
  8. MySQL添加和删除字段
  9. Erlang OTP设计原则Gen_Fsm行为[转]
  10. 程序员最值得听的歌曲TOP10