Problem 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.
(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.
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
//题解: 求N个数的最大公约数。
//标程:
#include<stdio.h> int gcd(int a,int b) { return b==0?a:gcd(b,a%b); } int main() { int n,p[110],i,m; while(scanf("%d",&n),n) { for(i=0;i<n;i++) scanf("%d",p+i); scanf("%d",&m); int Common_divisor=gcd(p[0],p[1]); for(i=2;i<n;i++) Common_divisor=gcd(p[i],Common_divisor); if(m%Common_divisor==0) printf("YES\n"); else printf("NO\n"); } return 0; }