湖南中医药大学2017年集训队第四场选拔赛-Problem D: Jug Hard

Problem D: Jug Hard

Time Limit: 5 Sec Memory Limit: 128 MB
Submit: 192 Solved: 83
[Submit][Status][Web Board]
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

[分析]
这题曾经做过类似的。
数论。拓展欧几里得。(算法竞赛入门经典310-313)
设va为第一个桶的容量,vb为第二个桶的容量,v为所要求的容量。
所以存在x和y满足va*x+vb*y=v;
所有根据欧几里得得出va*x+vb*y=gcd(va,vb);
因为题目不要求算出x和y,所有不用完全的拓展欧几里得。
只要连立上两式子可得v=gcd(va,vb);
没完。
因为x和y存在多解。
所有最终得:
当v%gcd==0时,满足条件。
[代码]

#include<cstdio>

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

int main()
{
    int va, vb, v;
    int t;
    scanf("%d", &t);
    while (t--)
    {
        scanf("%d%d%d", &va, &vb,&v);
        if (v%gcd(va, vb) == 0)printf("Yes\n");
        else printf("No\n");
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值