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个球放在一起,每个球上都标有一些数字,问你每次从这些球中拿出两个,然后按照球上的序号做差(取绝对值),并把这个差值写到一个新球上,最后把它和那N个球放在一起,最后问可不可以得到一个标号为K 的球。

思路:求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");                    }



原创粉丝点击