"蔚来杯"2022牛客暑期多校训练营6
J Number Game
题目大意
给整数A,B,C,X,有如下两种操作:
1.B->A-B
2.C->B-C
问能否在进行了一定次数上述操作后将C变为X。
思路
设操作一使用了 a a a 次,操作二使用了 b b b次。
若 a = b a=b a=b 或 a > b a>b a>b ,则容易得出,C的集合为 k ∗ ( A − 2 B ) + C k*(A-2B)+C k∗(A−2B)+C
若 a < b a<b a<b,相当于多进行了一次操作2,C的集合为 k ∗ ( A − 2 B ) + B − C k*(A-2B)+B-C k∗(A−2B)+B−C
因此,题目可以理解为,当 X − C X-C X−C或 X − B + C X-B+C X−B+C能被 A − 2 B A-2B A−2B 整除时输出YES,否则输出NO。
注意特判 A = 2 B A=2B A=2B 的情况。
代码
#include <iostream>
#include <climits>
#include <string.h>
#include <string>
#include <stdio.h>
#include <queue>
#include <vector>
#include <map>
#include <set>
#include <list>
#include <stack>
#include <algorithm>
#include <math.h>
#include <cctype>
#include <stdlib.h>
#include <unordered_map>
#define INTINF 0x3f3f3f3f
#define LLINF 0x3f3f3f3f3f3f3f3f
#define N 2000010
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
int t;
int main()
{
cin>>t;
while(t--)
{
int a,b,c,x;
cin>>a>>b>>c>>x;
int x1=x-c;
int x2=x-b+c;
int y=a-b-b;
int flag=0;
if(y!=0)
{
if(x1==0||x2==0) flag=1;
}
else
{
if(x1%y==0||x2%y==0) flag=1;
}
if(flag) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
system("pause");
return 0;
}
/*
b->a-b
c->b-c
1. b-(b-c)=c
2. a-(a-b)=b
*/