Obtain Two Zeroes/CodeForces 1260B

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值