ZCMU OJ 1896: Jug Hard

文章讲述了如何利用两个无刻度水壶和一个水龙头,在给定两个水壶的容量以及目标水量的情况下,判断是否能通过转移水达到目标体积。关键在于使用最大公约数gcd来确定能否实现。
摘要由CSDN通过智能技术生成

ZCMU OJ 1896: Jug Hard

题目描述

You have two empty jugs and tap that may be used to fill a jug. When filling a jug from the tap, you can only fill it completely (i.e., you cannot partially fill it to a desired level, since there are no volume measurements on the jug).

You may empty either jug at any point.

You may transfer water between the jugs: if transferring water from a larger jug to a smaller jug, the smaller jug will be full and there will be water left behind in the larger jug.

Given the volumes of the two jugs, is it possible to have one jug with some specific volume of water?

输入
The first line contains T, the number of test cases (1 ≤ T 100 000). Each test case is composed of three integers: a b d where a and b (1 ≤ a, b ≤ 10 000 000) are the volumes of the two jugs, and d is the desired volume of water to be generated. You can assume that d ≤ max(a,b).
输出
For each of the T test cases, output either Yes or No, depending on whether the specific volume of water can be placed in one of the two jugs.
样例输入
3
8 1 5
4 4 3
5 3 4
样例输出
Yes
No
Yes

提示:
如果c%gcd(a,b)==0则可以成功。
gcd(x,y)为x,y的最大公约数。

代码:

#include<bits/stdc++.h>
using namespace std;

int gcd(int a,int b) {
    return b>0 ? gcd(b,a%b):a;
}

int main()
{
	int t;
	cin>>t;
	for(int i=0;i<t;i++)
	{
		int a,b,c;
		cin>>a>>b>>c; 
		if(c%gcd(a,b)==0)
		{
			cout<<"Yes"<<endl;
		}else
		{
			cout<<"No"<<endl;
		}
	}
	return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值