2219. A famous math puzzle

¡题目描述:
有n个壶,和无穷多的水.每次我们只能:
1.把其中一个壶灌满.
2.把其中一个壶倒空.
3.把一个壶中的水倒入另一个壶中,直到一个壶为空或者另一个壶已经满了为止.
给定一个体积W,问能否经过若干次倒水后使得最终有一个壶中只剩下W升水?如果能,输出”YES”,否则输出”NO”

要使n个水壶能倒出w体积的水,则必须符合以下两个条件:
1.至少有一个水壶的容量大于或等于w.
2.假设p是n个水壶容量的最大公约数,那么w必须p的倍数.

为什么是最大公约数的倍数的解释(刚开始真的不懂,后来明白了):
假设n个水壶的容量分别为C1,C2,C3…..Cn.
必要性:不管执行三种操作的那一种,壶中所含的水一定是P的整数倍.
充分性:由欧几里德算法扩展可知,必然存在整数A1,A2,A3…..An,使得

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.

 AB
initial00
fill A50
pour A B05
fill A55
pour A B46
empty B40

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 4
5
6
2 10
65
39
2 12
4
8
3 9
10
35
14
0 0

Sample Output

YES
NO
NO
YES

#include<iostream>
#include<cstring>
using namespace std;
int gcd(int a,int b)
{
	if(b==0)
		return a;
	else
		return 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;
			else
				cout<<"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;
			else
				cout<<"NO"<<endl;
		}
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用中提到了一个类似的错误信息,说明在从指定的maven仓库下载构件时发生了错误。该错误可能是由于证书验证失败导致的。引用中也提到了一个类似的问题,指出在传输metadata时出现了问题。为了解决这个问题,可以尝试以下几种方法: 1. 检查网络连接: 确保你的网络连接正常,并且能够正常访问maven仓库。有时候网络问题可能会导致传输失败。 2. 检查仓库地址: 确保你正在使用正确的maven仓库地址。有时候使用错误的地址或者被防火墙阻止的地址可能导致下载失败。 3. 配置信任的证书: 如果错误信息中提到了证书验证失败,可能是因为你的maven配置没有信任该证书。你可以尝试将证书添加到你的maven信任列表中,以解决验证问题。 4. 清除本地仓库: 有时候本地仓库中的一些损坏的文件可能会导致传输失败。你可以尝试清除本地仓库中的相关构件,并重新下载。 5. 更新maven设置: 确保你正在使用最新的maven版本,并检查你的maven设置是否正确配置。有时候旧版本的maven可能会导致一些传输问题。 希望以上方法能够帮助你解决传输构件的问题。如果问题仍然存在,请提供更多的错误信息,以便我们能够提供更具体的帮助。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [报错-新建maven项目Could not transfer artifact......](https://blog.csdn.net/u014692224/article/details/112758493)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值