AtCoder Grand Contest 018 A

来源:互联网 发布:微信玩色子作弊软件 编辑:程序博客网 时间:2024/06/07 06:00

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
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

数学题:
我们设G为a1,a2,a3….an的最大公约数,M为a1,a2,a3….an的最大值
如果x和y的最大公约数是g,那么|x-y|的最大公约数也是g,如果x
和y都小于m则|x-y|也小于m,那么如果k%G==0且k<=M,则输出可能

#include <iostream>#include <stdio.h>#include <cmath>#include <algorithm>using namespace std;int main(){    int n,k;    int g,Max=0,p;    scanf("%d %d",&n,&k);    for(int i=0;i<n;i++){        scanf("%d",&p);        if(i==0){            g=p;        } else {            g=__gcd(g,p);        }        Max=max(Max,p);    }    if(k%g==0&&k<=Max){        puts("POSSIBLE");    } else {        puts("IMPOSSIBLE");    }    return 0;}
原创粉丝点击