2219. A famous math puzzle
来源:互联网 发布:windows游戏模拟器 编辑:程序博客网 时间:2024/05/18 09:07
A1*C1+A2*C2+A3*C3+…+An*Cn=W.
如果Ai是正数,我们就用第i个壶从水源中取Ai次水;如果Ai为负数,我们就把第i个壶倒空Ai次,这样最后必会剩下W升水Maybe you knew this famous puzzle when you were a child.
You have two jugs, A and B, and an infinite supply of water. There are three types of actions that you can use: (1) you can fill a jug, (2) you can empty a jug, and (3) you can pour from one jug to the other. Pouring from one jug to the other stops when the first jug is empty or the second jug is full, whichever comes first.
For example, if A has 5 gallons and B has 6 gallons and a capacity of 8, then pouring from A to B leaves B full and 3 gallons in A. As to a given cubage W, a solution is a sequence of steps that leaves exactly W gallons in one of the jugs.
For example, when A=5, B=6 and W=4, we can take the following steps to achieve the goal.
Today, we will generalize the puzzle. You are given N jugs, and asked to decide whether a goal W can be achieved.
Input
Input to your program consists of multiple test cases. Every test case starts with a line consists of two integers N and W. Then N lines follow, each line consisits of a single positive integer that is the capacity of the ith jug (1 ≤ N ≤ 100).
Input is terminated with N=0 and W=0.
Output
For each test case, if the goal can be achieved, you should output YES in a single line. Otherwise output NO in a single line.
Sample Input
2 4562 1065392 12483 91035140 0
Sample Output
YESNONOYES
#include<iostream>#include<cstring>using namespace std;int gcd(int a,int b){if(b==0)return a;elsereturn gcd(b,a%b);}int main(){int n,w,a[100];while(cin>>n>>w&&n){int max=0;for(int i=0;i<n;i++){cin>>a[i];if(a[i]>max)max=a[i];}if(max<w){cout<<"NO"<<endl;continue;}if(n==1){if(a[0]==w)cout<<"YES"<<endl;elsecout<<"NO"<<endl;continue;}else{int temp=gcd(a[n-1],a[n-2]);for(int i=n-3;i>=0;i--){temp=gcd(temp,a[i]);}if(w%temp==0)cout<<"YES"<<endl;elsecout<<"NO"<<endl;}}return 0;}
- 2219. A famous math puzzle
- hdu4255 A Famous Grid
- hdu4249 A Famous Equation
- HDU4255-A Famous Grid
- A Famous ICPC Team
- A Famous Music Composer
- HDU4252:A Famous City
- A famous music composer
- A Famous Music Composer
- A Famous Music Composer
- A Famous Music Composer
- HDU4252 A Famous City
- HDU4255 A Famous Grid
- A Famous Music Compose
- A Famous Music Composer
- HDU4252:A Famous City
- A Famous Music Composer
- A Famous Music Composer
- Hibernate HQL语句使用总结
- vim复制,粘贴,删除,撤销,替换,光标移动等用法
- nmap 常用命令
- 启动项目报错
- linux fork()函数详解
- 2219. A famous math puzzle
- SQL合并统一数据(相同的在一行显示)
- Visual SVN Server启动失败0x8007042a错误
- printk && demsg
- 勿入陷阱 从业者告诉你真实的P2P第三方支付托管
- 求二叉树中相差最大的两个节点间的绝对值
- oracle Lpad()函数和Rpad()函数的用法
- 7款开源Java反编译工具
- 怎么才能快速获得积分?