题目描述
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;
}