CSU1209-Three Jugs-GCD
来源:互联网 发布:招聘网站源代码 java 编辑:程序博客网 时间:2024/06/03 12:33
Y: Three Jugs
Description
We have three jugs A, B, C without any calibration, and an infinite supply of water. There are three types of actions that you can use:
(1) Fill a jug.
(2) Empty a jug.
(3) Pour from one jug to another.
Pouring from one jug to another stops when the first jug is empty or the second jug is full, whichever comes first. For example, if A has 5 gallons, B has 6 gallons and a capacity of 8, then pouring from A to B leaves B full and 3 gallons in A.
Now you need to calculate the minimum accurate gallons of water we can get by using the three jugs.
Input
There is an integer T (1 <= T <= 200) in the first line, means there are T test cases in total.
For each test case, there are three integers a, b, c (1 <= a, b, c <= 10^18) in a line, indicate the capacity (unit: gallon) of the three jugs.
Output
For each test case, you should print one integer in a line, indicates the minimum accurate gallons of water we can get by using the three jugs.
Sample Input
23 6 96 10 15
Sample Output
31
这个题就是找三个数的gcd,用的定理如下:
扩展欧几里德算法是用来在已知a, b求解一组x,y,使它们满足贝祖等式: ax+by = gcd(a, b) =d(解一定存在,根据数论中的相关定理)。扩展欧几里德常用在求解模线性方程及方程组中。
证明过程有需要的说下我再搞
#include <bits/stdc++.h>#define N 10100#define INF 0x3f3f3f3f#define LL long long#define mem(a,n) memset(a,n,sizeof(a))#define fread freopen("in.txt","r",stdin)#define fwrite freopen("out.txt","w",stdout)using namespace std;LL gcd(LL a,LL b){ if(a<b){ swap(a,b); } return (b==0?a:gcd(b,a%b));}int main(){ ios::sync_with_stdio(false); LL a,b,c,t; cin>>t; while(t--){ cin>>a>>b>>c; LL ans=gcd(a,b);// cout<<ans<<endl; ans=gcd(ans,c); cout<<ans<<endl; } return 0;}
- CSU1209-Three Jugs-GCD
- Three Jugs(辗转相除法求GCD)
- 1209: Three Jugs
- COJ 1209: Three Jugs
- CSU 1209 Three Jugs
- CSU - 1209 Three Jugs
- ACM:Y: Three Jugs
- CSU 1209 Three Jugs (数论)
- Jugs
- Jugs
- Jugs
- Jugs
- zoj1005 Jugs
- ZOJ1005 Jugs
- 1005 Jugs
- ZOJ1005 Jugs
- zoj1005-Jugs
- 571 - Jugs
- iOS
- C++11系列——函数对象(Function Object)
- Java设计模式之五种单例模式
- CSU1204-Rectangles-二分
- <NOIP> 1 . P1000 超级玛丽游戏
- CSU1209-Three Jugs-GCD
- Redis启动的三种方式
- Kotlin-22.Lambda表达式和函数
- TCP连接的建立和释放(三次握手和四次挥手)
- 【C#】面向对象之多态
- 数据库锁的基本概念
- Bootstrap学习--初识Bootstrap
- 关于驱动和设备的一点所思所想(未完待续,根据以后的学习再补充)
- Java精选笔记_面向对象(包、访问控制、内存机制、垃圾回收机制)