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 翻译 有\( ...
随机推荐
- 超简单开发自己的php框架一点都不难
(转)https://blog.csdn.net/qq_33862644/article/details/79344331 写框架的极简思路: 接收,打印参数想怎么弄.如 获取配置文件的方法,根据传过 ...
- 学习python第十四天,模块
Python 模块(Module),是一个 Python 文件,以 .py 结尾,包含了 Python 对象定义和Python语句. 模块让你能够有逻辑地组织你的 Python 代码段. 把相关的代码 ...
- [Luogu1341]无序字母对(欧拉回路)
按题意给定字符串建无向图,找欧拉回路 按照定义,当没有奇数度点或者只有2个奇数度点时才有欧拉回路 Code #include <cstdio> #include <algorithm ...
- urllib使用二
urlopen方法返回一个html 对html使用info()方法返回HTTPMessage对象实例 import urllib def print_list(lists): for i in lis ...
- IAR FOR STM8S 错误 An error occurred while retrieving GDI features: gdi-error [40201]解决方法
今早使用IAR调试编译调试一个工程,发现IAR竟然出现如下错误信息 An error occurred ]: Can't access configuration database 在网上查看了一下, ...
- python ranndom模块及生成验证码
python的random模块用于生成随机数,下面介绍一下random模块的常用方法: 取随机小数: 数学计算 random.random() 用于生成一个0-1的随机浮点数 0<=n<1 ...
- Android开发——View滑动的三种实现方式
0. 前言 Android开发中,我们常常需要View滑动实现一些绚丽的效果来优化用户体验.一般View的滑动可以用三种方式实现. 转载请注明出处:http://blog.csdn.net/seu ...
- 5-1 练习css 总结
1. 边框 border:3px dotted; border: 2px solid yellow; 背景颜色 background-color: red; 外攘 margin:20px 0 20px ...
- 单例解决存储微信Token信息保留两小时
采用单例模式可以存储初始化数据,比如第一次对/api/test接口进行了访问,传入的信息为“123”,则在两个小时之内返回的信息依然是“123”,无论传入的参数是什么,如果有效时间过了两个小时,比如传 ...
- mongo创建数据库和用户
1.linux安装mongo conf文件配置: 配置文件: dbpath=/home/data/mongodb/mongodb logpath=/home/data/logs/mongodb.log ...