【ZSTU4216 2015年12月浙理工校赛 G】【数论 gcd】Jug Hard 两个杯子倒水能否倒腾到目标值

4216: Jug Hard

Time Limit: 10 Sec   Memory Limit: 128 MB
Submit: 1176   Solved: 182

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


#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<ctype.h>
#include<math.h>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre(){freopen("c://test//input.in","r",stdin);freopen("c://test//output.out","w",stdout);}
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1,class T2>inline void gmax(T1 &a,T2 b){if(b>a)a=b;}
template <class T1,class T2>inline void gmin(T1 &a,T2 b){if(b<a)a=b;}
const int N=0,M=0,Z=1e9+7,ms63=0x3f3f3f3f;
int casenum,casei;
int x,y,z;
int gcd(int x,int y)
{
	return y==0?x:gcd(y,x%y);
}
int main()
{
	scanf("%d",&casenum);
	for(casei=1;casei<=casenum;++casei)
	{
		scanf("%d%d%d",&x,&y,&z);
		puts(z%gcd(x,y)==0?"Yes":"No");
	}
	return 0;
}
/*
【题意】
很经典,就是我们有两个水杯,容积分别为x和y。
我们想要倒腾水,两个水杯倒来倒去,最后使得其中一个水杯,其中的水量恰好为z。
数据保证z<=max(x,y)
组数可达1e5
x,y可达1e7

【类型】
脑洞 数据gcd

【分析】
以前做过一个类似的题,数据很小,用的搜索
然而这道题的数据范围这么大,大概只能单组logn。
于是显然一个猜想——puts(z%gcd(x,y)==0?"Yes":"No");

这个猜想是成立的,题目很快就被秒杀了。
然而,为什么会满足这样一个猜想呢?

因为,我们发现,如果我们倒腾,
要不就是得到vol%x,
要不就是得到vol%y
更多地,我们可以得到(vol+x)%y,(vol+y)%x之类的。

哎呀我数学不好,但是我知道这是和gcd划勾勾的。噗~然后就AC啦

【时间复杂度&&优化】
O(Tlog(x+y))

*/


你可能感兴趣的:(数论,水题,数论-GCDLCM)