[找规律]Number Game 2022牛客多校第6场 J

题目描述

There are three integers A,BA, BA,B and CCC written on the blackboard.

You can perform the following two operations as many times as you like:

1. Change BBB to A−BA-BA−B.

2. Change CCC to B−CB-CB−C.

Please note that each time you don't need to perform all two operations. You can choose one type of operation to perform.

You are given an integer xxx. Answer whether you can change CCC into xxx using these operations.

You need to answer TTT queries independently.

输入描述:

The first line contains a positive integer T(1≤T≤105)T(1\leq T\leq 10 ^ 5)T(1≤T≤105).

Each of the next TTT lines contains four integers A,B,C,x(−108≤A,B,C,x≤108)A, B, C, x(-10 ^ 8 \leq A, B, C, x \leq 10 ^ 8)A,B,C,x(−108≤A,B,C,x≤108).

输出描述:

For each test case, output "Yes" if CCC can become xxx, and "No" otherwise (without quotes).

示例1

输入

3 
2 4 3 1 
2 4 3 2 
4 2 2 0

输出

Yes
No
Yes

说明

Please note that A,B,C,xA, B, C, xA,B,C,x could be negative.

备注:

Please note that A,B,C,xA, B, C, xA,B,C,x could be negative.

题意: 给出四个整数a,b,c和x,每次操作可以让b = a-b或者c = b-c,问在若干次操作后能否让c等于x。

分析: b的值只能是b或者是a-b,而c的值可以多写几个找找规律,第一轮c的值可以为b-c或a-b-c,第二轮c的值可以为a-2*b+c或-a+2*b+c,第三轮c的值可以为-a+3*b-c或2*a-3*b-c,第四轮c的值可以为2*a-4*b+c或-2*a+4*b+c,挨着看可能不好找规律,但是隔一个再看就能找到规律了,其实就是b-c+k*(2*b-a)或a-2*b+c+k*(a-2*b)或a-b-c+k*(a-2*b)或-a+2*b+c+k*(2*b-a),其中k为任意自然数,所以就是让x和这四个数做差,看看差值是否为相应的倍数即可。

具体代码如下:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <string>
#define int long long
using namespace std;

signed main()
{
	int T;
	cin >> T;
	while(T--){
		int a, b, c, x;
		scanf("%lld%lld%lld%lld", &a, &b, &c, &x);
		int n1 = b-c, n2 = -a+2*b+c, n3 = a-b-c, n4 = a-2*b+c;
		if(2*b-a == 0){
			if(x-n1 == 0 || x-n2 == 0 || x-n3 == 0 || x-n4 == 0) puts("Yes");
			else puts("No");
			continue;
		}
		if((x-n1) % (2*b-a) == 0){
			puts("Yes");
			continue;
		}
		if((x-n2) % (2*b-a) == 0){
			puts("Yes");
			continue;
		}
		if((x-n3) % (a-2*b) == 0){
			puts("Yes");
			continue;
		}
		if((x-n4) % (a-2*b) == 0){
			puts("Yes");
			continue;
		}
		puts("No");
	}
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值