湖南中医药大学2017年集训队第四场选拔赛-Problem D: Jug Hard
来源:互联网 发布:万能扫描软件下载 编辑:程序博客网 时间:2024/05/01 06:31
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;}
- 湖南中医药大学2017年集训队第四场选拔赛-Problem D: Jug Hard
- 湖南中医药大学2017年集训队第四场选拔赛-Problem F: Virtual Friends
- 湖南中医药大学2017年集训队第四场选拔赛-Problem B: Power Eggs
- 湖南中医药大学2017年集训队第二场选拔赛
- 湖南中医药大学2017年集训队第五场选拔赛
- 2017年集训队第四场选拔赛Trainsorting
- Problem D: Jug Hard
- 2017年集训队第四场选拔赛 -string Game
- 中南大学2016年校队选拔赛第一场Problem D
- 中南大学2016年校队选拔赛第一场Problem E
- 中南大学2016年校队选拔赛第一场Problem A
- 中南大学2016年校队选拔赛第二场Problem F
- 中南大学2016年校队选拔赛第二场Problem A
- 2017南工软件ACM集训队选拔赛第一场
- 第四场选拔赛
- 湖南工业大学个人选拔赛第一场 题解
- 2016年湖南中医药大学考博英语考.试答.案《466068743+考過付》
- 2017年浙江中医药大学大学生程序设计竞赛-D:CC的神奇背包(思维)
- jdk环境变量的配置
- 教你操作 linux grep 指令,搜索内容或是汇出
- Spring官网下载jar包
- eclipse下切换svn用户 (换用户登录)
- 方块倒计时
- 湖南中医药大学2017年集训队第四场选拔赛-Problem D: Jug Hard
- 解决跨域问题
- Edraw Max(亿图图示)教程:如何自定义组织结构图展示的信息
- 燕麦云何洋开讲丨移动办公、数据上云,企业和信息安全的战争才刚开始
- [NOIP2016]天天爱跑步
- linux 模块编译错误
- Swift Extension的使用场景、iOS10字体的适配
- QT中使用Qcombox和QCompleter实现可编辑的combox下拉提示,并设置样式
- JS对象字符串数组排序