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+7;
int read(){int ans=0,f=1,c=getchar();while(c<'0'||c>'9'){if(c=='-') f=-1; c=getchar();}while(c>='0'&&c<='9'){ans=ans*10+(c-'0'); 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=1;i<=n;i++){num[i]=read();mx=max(mx,num[i]);if(num[i]==k) f=1;}if(f){printf("POSSIBLE\n"); return 0;}if(mx<k){printf("IMPOSSIBLE\n"); return 0;}ans=gcd(num[1],num[2]);for(int i=3;i<=n;i++) ans=gcd(ans,num[i]);if(k%ans==0) printf("POSSIBLE\n");else printf("IMPOSSIBLE\n");return 0;
}

View Code

转载于:https://www.cnblogs.com/lyzuikeai/p/7226403.html

AtCoder Grand Contest 018 A相关推荐

  1. 【每日亿题#12】AtCoder Grand Contest 021 (A ~ F)全部题解

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 文章目录 AtCoder Grand Contest 021 题解 A. Digit Sum 2 B. ...

  2. AtCoder Grand Contest 008: Contiguous Repainting(思维)

    Contiguous Repainting 时间限制: 2 Sec  内存限制: 256 MB 提交: 69  解决: 22 [提交][状态][讨论版][命题人:admin] 题目描述 There a ...

  3. AtCoder Grand Contest 017

    AtCoder Grand Contest 017 A - Biscuits 有\(n\)个数,问有多少个集合的数的和模\(2\)余\(P\). 随便\(dp\)一下就好了. #include< ...

  4. AtCoder题解——AtCoder Grand Contest 048——A - atcoder < S

    题目相关 题目链接 AtCoder Grand Contest 048 A 题,https://atcoder.jp/contests/agc048/tasks/agc048_a. Problem S ...

  5. AtCoder题解 —— AtCoder Grand Contest 050 —— B - Three Coins —— 动态规划

    题目相关 题目链接 AtCoder Grand Contest 050 B 题,https://atcoder.jp/contests/agc050/tasks/agc050_b. Problem S ...

  6. Atcoder Grand Contest 010 B - Boxes 差分

    B - Boxes 题目连接: http://agc010.contest.atcoder.jp/tasks/agc010_b Description There are N boxes arrang ...

  7. AtCoder Grand Contest 010 D - Decrementing

    题目传送门:https://agc010.contest.atcoder.jp/tasks/agc010_d 题目大意: 有\(n\)个数\(A_i\),它们的\(gcd\)是1,A.B两人轮流操作, ...

  8. Atcoder Grand Contest 012 B - Splatter Painting解题报告

    题目:http://agc012.contest.atcoder.jp/tasks/agc012_b 有一个n点m边的图,(不一定联通) 还有q个操作:每次将一个点v及其周围距离<=d的点涂成颜 ...

  9. [atcoder]AtCoder Grand Contest 027题解

    [题目链接] https://agc027.contest.atcoder.jp/ A [题解] 题意: 是把xxx个糖果分给nnn个人,一个人如果恰好分到aia_{i}ai​个糖果就会高兴.求最多使 ...

  10. UPC个人训练赛第十五场(AtCoder Grand Contest 031)

    传送门: [1]:AtCoder [2]:UPC比赛场 [3]:UPC补题场 参考资料 [1]:https://www.cnblogs.com/QLU-ACM/p/11191644.html B.Re ...

最新文章

  1. Centos7安装Nginx详细步骤
  2. 分享一款jquery的日期插件
  3. 一篇对伪共享、缓存行填充和CPU缓存讲的很透彻的文章
  4. springboot集成shiro实现注册、登录、退出功能
  5. hdu 3078(LCA+排序)
  6. EF(Entity FrameWork)实体框架
  7. C语言课后习题(66)
  8. 【ACM】 1231 最大连续子序列
  9. 13.nginx 源码目录及配置
  10. xtu DP Training C.炮兵阵地
  11. 第一章 ZeroMQ基础
  12. 液晶VGH、 VGL电路解析
  13. matlab如何给页眉加图片,word页眉页脚修改(Word header footer modification).doc
  14. 系统调用ptrace和进程跟踪
  15. webstorm绝对路径引入echarts_Webstorm+Webpack+echarts构建个性化定制的数据可视化图表两个echarts详细教程(柱状图,南丁格尔图)...
  16. python朋友圈图片_教你如何用Python处理图片九宫格,炫酷朋友圈
  17. signature=d601b7b6eb512df6319aad970c9aaeab,Excise Tax Return Serial Number 97-17 971101 971115
  18. OpenJ_Bailian - 3164 奇偶排序
  19. 小学生计算机校本课程教材,二年级校本课程教材
  20. jQuery子页面刷新父页面--局部刷新+整体刷新

热门文章

  1. Android中的控件
  2. 嵌入式系统调试仿真工具
  3. 推荐系统Recommendation System:综述
  4. 6个座位办公室最佳位置_6个办公室座位的最佳位置 六个最好的办公室座位位置...
  5. 黑苹果 无法运行xcode_Hackintosh (黑苹果) 折腾
  6. Java 多线程(三)优先级、休眠(未完待续...)
  7. JavaWeb — 获取复选框的值时,要注意不是得到显示的值,而是得到value值
  8. Android 实现圆角头像(使用第三方开源库)
  9. 蔡工RK系列Android驱动开发入门视频课程
  10. vue.js 源代码学习笔记 ----- instance inject