Description–
牛牛最近对三元一次方程非常感兴趣。众所周知,三元一次方程至少需要三个方程组成一个方程组,才有可能得出一组解。
牛牛现在想要知道对于方程 a x + b y + c z = d ax+by+cz=d ax+by+cz=d 中有没有至少存在一组 x , y , z x,y,z x,y,z 的解,且 x , y , z x,y,z x,y,z 都为整数,使得方程式成立。
Input–
第一行输入一个正整数 T T T,表示测试点中测试样例的组数。
接下来 T T T 行,每行四个整数 a , b , c , d a,b,c,d a,b,c,d 表示方程 a x + b y + c z = d ax+by+cz=d ax+by+cz=d 中的 a , b , c , d a,b,c,d a,b,c,d
Output–
如果至少存在一组{x,y,z}能够满足方程式等式成立,且x,y,z均为整数,请输出"YES",否则请输出"NO"。
Sample Input–
2
3 1 2 0
2 8 8 3
Sample Output–
YES
NO
说明–
-
1
∗
3
+
(
−
1
)
∗
1
+
(
−
1
)
∗
2
=
0
1*3+(-1)*1+(-1)*2=0
1∗3+(−1)∗1+(−1)∗2=0
得到一组 x , y , z x,y,z x,y,z 的解为{ 1 , − 1 , − 1 1,-1,-1 1,−1,−1 }为整数使得等式成立,所以输出 " Y E S " "YES" "YES" 。
不存在x,y,z为整数使得方程 2 x + 8 y + 8 z = 3 2x+8y+8z=3 2x+8y+8z=3 成立,所以输出 " N O " "NO" "NO"。
对于
10
%
10\%
10% 的测试数据,保证
T
=
1
,
−
10
≤
a
,
b
,
c
,
d
≤
10
T=1,-10 \leq a,b,c,d \leq10
T=1,−10≤a,b,c,d≤10
对于
30
%
30\%
30% 的测试数据,保证
−
100
≤
a
,
b
,
c
,
d
≤
100
-100 \leq a,b,c,d\leq 100
−100≤a,b,c,d≤100
对于
100
%
100\%
100% 的测试数据,保证
−
1
0
18
≤
a
,
b
,
c
,
d
≤
1
0
18
,
1
≤
T
≤
100
-10^{18}\leq a,b,c,d\leq 10^{18} ,1 \leq T \leq 100
−1018≤a,b,c,d≤1018,1≤T≤100
解题思路–
就是算 g c d ( a , b , c ) gcd(a, b, c) gcd(a,b,c) 能不能被 d d d 整除,可就 y e s yes yes
代码–
#include <iostream>
#include <cstdio>
using namespace std;
int t;
long long a, b, c, d;
long long gcd(long long x, long long y)
{
if (!y) return x;
return gcd(y, x % y);
}
void work()
{
scanf("%lld%lld%lld%lld", &a, &b, &c, &d);
if (a == 0)
{
if (b == 0)
{
if (c == 0)
{
if (d == 0) printf("YES");
else printf("NO");
}
else if (d == 0 || d % c == 0) printf("YES");
else printf("NO");
}
else if (c == 0)
{
if (d == 0 || d % b == 0) printf("YES");
else printf("NO");
}
else if (d == 0 || d % gcd(b, c) == 0) printf("YES");
else printf("NO");
}
else if (d == 0 || d % gcd(gcd(a, b), c) == 0)printf("YES");
else printf("NO");
printf("\n");
}
int main()
{
scanf("%d", &t);
for (int i = 1; i <= t; ++i)
work();
return 0;
}