2682. Die Hard 4
Constraints
Time Limit: 1 secs, Memory Limit: 256 MB
Description
In Die Hard with a Vengeance, Simon's strange game of cat and mouse is played. In the park, a time bomb is about to go off. Detective John McClane and his sidekick Zeus must make exactly four gallons from five and three gallon jugs in five minutes. It’s no so easy for them, but finally they solve it in time:
(1) Fill the five gallon jug. Three gallon jug is empty.
(2) Empty three gallons from the five gallon jug into the three gallon jug.
(3) There remains two gallons in the five gallon jug. Empty the three gallon jug.
(4) Pour the two gallons into the three gallon jug.
(5) Fill the five gallon jug and pour one gallon from it into the three gallon jug - filling the three gallon jug.
(6) Four gallons remain in the five gallon jug.
In Die Hard 4, terrorists start another attack. This time, John faces a similar but more difficult problem. Given n jugs of A1, A2, …, An gallons respectively, no weighing scale, to obtain exactly W gallons. Because terrorists no time to prepare all n jugs, so they just ask whether it is possible.
Input
The input consists of multiple datasets. The end of the input is indicated by a line containing a zero.
The format of each dataset is as follows.
n
A1 A2 … An
W
Here, all data items are positive integers. n is the number of jugs (n<=100). A1, A2, …, An (Ai<10^9) are the volume of the jugs. W is the volume they wanted.
Output
For each dataset, output the result of the problem in a separate line: “YES” when the problem is solvable, “NO” otherwise.
Sample Input
2 3 5 4 2 3 6 4 0
Sample Output
YES NO
Problem Source
系列热身赛5@2011年上半学期算法考试和4+2选拔赛
// Problem#: 2682
// Submission#: 3556152
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include<cstdio>
int main(){while(1){int N,a,b,t;scanf("%d",&N);if(!N--)break;scanf("%d",&a);while(N--){scanf("%d",&b);while(b){t=a%b;a=b;b=t;}}scanf("%d",&b);printf("%s\n",b%a?"NO":"YES");}return 0;}