Try Again

AtCoder Grand Contest 018 A - Getting Difference

A - Getting Difference


Time limit : 2sec / Memory limit : 256MB

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

  • 1N105
  • 1Ai109
  • 1K109
  • 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(94)=5. Next, take out 3 and 5, and return them back along with abs(35)=2. Finally, take out 9 and 2, and return them back along with abs(92)=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

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

5 12
10 2 8 6 4

Sample Output 4

IMPOSSIBLE
首先,k>max(a[0],a[1],a[2],.......)进行特判,一定不满足,之后,对序列两两之间取GCD,判断即可
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <iomanip>
#include <cmath>
#include <ctime>
#include <map>
#include <set>
using namespace std;
#define lowbit(x) (x&(-x))
#define max(x,y) (x>y?x:y)
#define min(x,y) (x<y?x:y)
#define MAX 100000000000000000
#define MOD 1000000007
#define pi acos(-1.0)
#define ei exp(1)
#define PI 3.141592653589793238462
#define INF 0x3f3f3f3f3f
#define mem(a) (memset(a,0,sizeof(a)))
typedef long long ll;
set<ll>s;
ll a[100006],n,k;
ll gcd(ll x,ll y)
{
    return y==0?x:gcd(y,x%y);
}
int main()
{
    scanf("%lld%lld",&n,&k);
    int ok=0,maxn=-1;
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
        if(a[i]==k)ok=1;
        maxn=max(maxn,a[i]);
    }
    sort(a,a+n);
    if(ok) {puts("POSSIBLE");return 0;}
    if(k>maxn) {puts("IMPOSSIBLE");return 0;}
    for(int i=1;i<n;i++)
    {
        s.insert(gcd(a[i],a[i-1]));
    }
    for(set<ll>::iterator it=s.begin();it!=s.end();it++)
    {
        if(k%*it==0)
        {
            puts("POSSIBLE");
            return 0;
        }
    }
    puts("IMPOSSIBLE");
    return 0;
}

 


posted @ 2017-07-24 09:10  十年换你一句好久不见  阅读(240)  评论(0编辑  收藏  举报