2015浙江理工校赛G Jug Hard (数论orBFS)

本文探讨了倒水问题的高效求解方法,通过直接判断目标体积能否被两水壶容量的最大公约数整除来简化BFS算法,提供了一种更快速的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题解

两个杯子倒水问题…一看10万个样例就犹豫了一下要不要BFS做,其实直接判断一下d能不能除尽a和b的gcd就好了..当然BFS也能做,只是慢了一点

代码


#include<cstdio>
#include <queue>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <set>
#include <ctime>
#include <cmath>
#include <cctype>
using namespace std;
#define MAX 100000
#define LL long long
int cas=1,T;
int gcd(int a,int b)
{
    return b==0?a:gcd(b,a%b);
}
int main()
{
    scanf("%d",&T);
    while (T--)
    {
       int a,b,d;
       scanf("%d%d%d",&a,&b,&d);
       if (b==d || a == d)
       {
           printf("Yes\n");
           continue;
       }
       if (d%gcd(a,b))
           printf("No\n");
       else
           printf("Yes\n");
    }

    return 0;
}

题目

Jug Hard

Time Limit: 10 Sec Memory Limit: 128 MB

Description

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?

Input

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).

Output

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.

Sample Input

3
8 1 5
4 4 3
5 3 4

Sample Output

Yes
No
Yes

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值