小珂的苦恼
时间限制:
1000 ms | 内存限制:
1000 KB
难度:
2
描述
小珂是一名初中生,她现在很苦恼,因为老师布置了一个让她苦恼的作业,你能不能帮助她呢?题目信息如下。
已知二元一次方程 a*x+b*y=n, 判断这个二元一次方程有没有整数解,x,y为未知数,其中a,b,n都为整数且不等于零,同时满足0<a,b,n<2^16-1。
2 2 4 2 3 9 7
Yes No
第一种方法:
01.
#include<stdio.h>
02.
int
main()
03.
{
04.
int
n,a,b,c,i,j;
05.
scanf
(
"%d"
,&n);
06.
while
(n--)
07.
{
08.
scanf
(
"%d%d%d"
,&a,&b,&c);
09.
if
(a<b)
10.
{
11.
i=a;
12.
a=b;
13.
b=i;
14.
}
15.
j=1;
16.
while
(j)
17.
{
18.
j=a%b;
19.
a=b;
20.
b=j;
21.
}
22.
if
(c%a==0)
23.
printf
(
"Yes\n"
);
24.
else
25.
printf
(
"No\n"
);
26.
}
27.
return
0;
28.
}
第二种方法:
01.
#include<iostream>
02.
#include<time.h>
03.
#include<stdio.h>
04.
using
namespace
std;
05.
int
gcd(
int
a,
int
b)
06.
{
07.
if
(a==0)
return
b;
08.
return
gcd(b%a,a);
09.
}
10.
int
main()
11.
{
12.
int
a,b,n,m;
13.
//freopen("2.txt","r",stdin);
14.
//freopen("1.txt","w",stdout);
15.
cin>>m;
16.
for
(
int
i=0;i<m;i++){
17.
scanf
(
"%d %d %d"
,&a,&b,&n);
18.
if
((n%gcd(a,b))==0)
19.
printf
(
"Yes\n"
);
20.
else
printf
(
"No\n"
);
21.
}
22.
23.
}
还可以用
gcd(a,b,c)是a,b,c的公约数
这个函数来求解树之间的公约数