题意简述
给你两个桶,初始各有$x$ , $y$ 升水,每次可以从一个木桶向另一个木桶倒水,直到一个桶装满或另一个桶无剩余。判断桶内是否能达到要求水量 $y$。
题目解决
满足题意的有且仅有以下两种情况。
- $x$ , $y$ 的最大公因数必须整除 $z$ 即 $\gcd(x,y)|z$。
- $x$ , $y$ 中必须至少有一个大于等于 $x \ge z | y \ge z$。
证明应该是显然的。
题目实现
用拓展欧几里得算法,实现还是非常简单的。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| #include<bits/stdc++.h> using namespace std; int T; int exgcd(int a,int b,int &x,int &y){ if(b==0){x=1,y=0;return a;} int d=exgcd(b,a%b,y,x); y-=(a/b)*x; return d; } int main() { cin>>T; while(T--){ int a,b,c; cin>>a>>b>>c; int x,y; if(a+b<c){ cout<<"NO"<<endl; continue; } if(c==0){ cout<<"YES"<<endl; continue; } if(a==0||b==0){ if(b==c||a==c)cout<<"YES"<<endl; else cout<<"NO"<<endl; continue; } int d=exgcd(a,b,x,y); if(c%d==0)cout<<"YES"<<endl; else cout<<"NO"<<endl; } return 0; }
|