L: Obtain Two Zeroes

题目来源

题目

You are given two integers a and b. You may perform any number of operations on them (possibly zero).During each operation you should choose any positive integer x and set a:=a−x, b:=b−2x or a:=a−2x, b:=b−x. Note that you may choose different values of x in different operations.Is it possible to make a and b equal to 0 simultaneously?Your program should answer t independent test cases.InputThe first line contains one integer t (1≤t≤1001≤t≤100) — the number of test cases.Then the test cases follow, each test case is represented by one line containing two integers a and b for this test case (0≤a,b≤1090≤a,b≤1e9).OutputFor each test case print the answer to it — YES if it is possible to make a and b equal to 0 simultaneously, and NO otherwise.You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer).

Example 
Input  
3
6 9
1 1
1 2
Output  
YES
NO
YES

NoteIn the first test case of the example two operations can be used to make both a and b equal to zero:
choose x=4 and set a:=a−x, b:=b−2x. Then a=6−4=2,b=9−8
choose x=1 and set a:=a−2x, b:=b−x. Then a=2−2=0, b=1−1=0

题意

给出两个整数a和b。您可以对它们执行任意数量的操作(可能为零)。在每次操作中,您应该选择任意正整数x并设置a:= a-x,b:= b-2x或a:= a-2x,b:= b−x
请注意,您可能会在不同的运算中选择x的不同值,是否可能同时使a和b等于0?您的程序应回答t个独立的测试用例输入第一行包含一个整数t(1≤t≤1001≤t ≤100)—测试用例的数量。然后,每个测试用例都由一行包含该测试用例的两个整数a和b(0≤a,b≤1e9)表示。输出对于每个测试用例,请打印答案—如果可以同时使a和b等于0,则为是,否则为NO。您可以在任何情况下打印每个字母(例如,字符串yEs,是,是和是都会被视为肯定答案)。

输入项
3
6 9
1 1
1 2
输出量是没有是注意在示例的第一个测试用例中,可以使用两个操作使a和b都等于零: 选择x = 4并设置a:= a-x,b:= b-2x。那么a = 6-4 = 2,b = 9-8 选择x = 1并设置a:= a-2x,b:= bx。那么a = 2-2 = 0,b = 1-1 = 0

解法

通过分析不难发现这压根不是道二分题,而是一道数学题
取x=1(就算取x>1, 也可以看成x个1,不过多操作几次而已)
假设第一个操作执行n次,第二个执行m次, 相加即可知(a+b)%3=0
而当a大于b的二倍时, 执行哪个操作都无法让b=0时a=0, 同理b>a。。。。

代码

#include <iostream>
using namespace std;

int main(){
	int T;
	cin >> T;
	while(T--){
		int a, b;
		cin >> a >> b;
		if((a+b) %3!=0 || b>2*a || a>2*b){
			cout << "NO" << endl;
		}
		else cout << "YES" << endl;
	}
	
	return 0;
}
发布了14 篇原创文章 · 获赞 0 · 访问量 151

猜你喜欢

转载自blog.csdn.net/loaf_/article/details/103960461
two