AtCoder Grand Contest 018 --------- Getting Difference
来源:互联网 发布:nginx配置stream 编辑:程序博客网 时间:2024/06/14 21:00
Problem Statement
There is a box containing N balls. Thei-th ball has the integerAi 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 integerK 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 KA1 A2 … AN
Output
If it is possible for Snuke to reach the state where the box contains a ball on which the integerK is written, printPOSSIBLE; if it is not possible, print IMPOSSIBLE.
Sample Input 1
3 79 3 4
Sample Output 1
POSSIBLE
First, take out the two balls 9 and4, and return them back along with a new ball,abs(9−4)=5.Next, take out 3 and5, and return them back along withabs(3−5)=2.Finally, take out 9 and2, and return them back along withabs(9−2)=7.Now we have 7 in the box, and the answer is thereforePOSSIBLE.
Sample Input 2
3 56 9 3
Sample Output 2
IMPOSSIBLE
No matter what we do, it is not possible to have 5 in the box. The answer is thereforeIMPOSSIBLE.
Sample Input 3
4 1111 3 7 15
Sample Output 3
POSSIBLE
The box already contains 11 before we do anything. The answer is thereforePOSSIBLE.
Sample Input 4
5 1210 2 8 6 4
Sample Output 4
IMPOSSIBLE
思路:求N个球的最大公因数,然后让目标球号与它求模,如果整除了,就可以,反之则不行;
代码如下:
#include<cstdio> #include<algorithm> #include<iostream> using namespace std; int gcd(int x,int y){ return x ? gcd(y%x,x) : y; } int main() { int n,a,Max,k; int i; cin>>n>>a>>k; int tmp=k; Max=k; int ans=0; for(i=1;i<n;i++) { int t; cin>>t; tmp=gcd(tmp,t); Max=max(Max,t); } //cout<<a<<endl; if(n == 1) { if(a == tmp) ans = 1; } else { if(a%tmp == 0 && a <= Max)//如何目标a可以被N个球的最大公因数整除,就表示成立,并且N个球中最大的序号应大于目标球 ans = 1; } if(ans == 1) puts("POSSIBLE"); else puts("IMPOSSIBLE"); }
- AtCoder Grand Contest 018 --------- Getting Difference
- AtCoder Grand Contest 018 A Getting Difference(比较水。。。)
- AtCoder Grand Contest 018
- AtCoder Grand Contest 018
- AtCoder Grand Contest 018 A
- atcoder grand Contest 018 A
- AtCoder Grand Contest 018 A
- AtCoder Grand Contest 018 E
- AtCoder Grand Contest 018 F
- 【AtCoder】 Grand Contest 018 C
- AtCoder Grand Contest 018--D题
- AtCoder Grand Contest 018 做题记录
- AtCoder Grand Contest 010
- AtCoder Grand Contest 011
- Atcoder Grand Contest 019
- AtCoder Grand Contest 010
- AtCoder Grand Contest 008
- AtCoder GC 018A: Getting Difference 题解
- java float乘法不正确的解决办法
- 表达式计算
- http请求的referer属性
- 【成长经历】----陪女朋友拔智齿
- HDU-2546(01背包)
- AtCoder Grand Contest 018 --------- Getting Difference
- Scala 类
- Html5新增常见标签
- 将"eh"更正为"Excel Home"
- 51nod_1179_计算N个数之间两两之间GCD的最大值
- ADB个人用法
- android图片大小的要求质量压缩图片
- python format 用法详解
- 昂贵的聘礼 POJ