Obtain Two Zeroes/CodeForces 1260B
题目
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.
题目来源:CodeForces 1260B
题目链接
题意
给定两个数a,b。有两种操作:操作1 a:=a−x, b:=b−2x,操作2 a:=a−2x, b:=b−x。问通过两种操作后a,b能否变为0。
解法
设a通过操作1减的数为x1,b通过操作2减的数为x2,若能成功则a=x1+2x2,b=2x1+x2,相加得a+b=3*(x1+x2),所以a+b是3得倍数,注意若a>2b,或b>2a时也是不行的。所以当a+b%3>0 ,或a>2b,b>2a时是NO,其余情况是YES。
代码:
#include <cstring>
#include <stdio.h>
#include <math.h>
#include <algorithm>
#include <iostream>
using namespace std;
int n,a,b,l;
int main()
{
scanf("%d",&n);
for (int i=1;i<=n;i++)
{
scanf("%d%d",&a,&b);
if ((a+b)%3!=0 || a>2*b || b>2*a) printf("NO\n"); else printf("YES\n");
}
return 0;
}