The Sky is Sprite.
The Birds is Fly in the Sky.
The Wind is Wonderful.
Blew Throw the Trees
Trees are Shaking, Leaves are Falling.
Lovers Walk passing, and so are You.
…Write in English class by yifenfei
Girls are clever and bright. In HDU every girl like math. Every girl like to solve math problem!
Now tell you two nonnegative integer a and b. Find the nonnegative integer X and integer Y to satisfy Xa + Yb = 1. If no such answer print “sorry” instead.
Input
The input contains multiple test cases.
Each case two nonnegative integer a,b (0<a, b<=2^31)
Output
output nonnegative integer X and integer Y, if there are more answers than the X smaller one will be choosed. If no answer put “sorry” instead.
Sample Input
77 51
10 44
34 79
Sample Output
2 -3
sorry
7 -3
题意:Xa + Yb = 1,求X的非负解和Y。
思路:扩展欧几里得模板题,注意判读X,Y互质以及X的非负
AC代码:
#include<cstdio>
using namespace std;
typedef long long LL;
void extend_Eulid(LL a, LL b, LL &x, LL &y, LL &d)
{
if (!b) {d = a, x = 1, y = 0;}
else
{
extend_Eulid(b, a % b, y, x, d);
y -= x * (a / b);
}
}
int main(){
LL a, b, d, x, y;
while(~scanf("%lld%lld", &a, &b)){
extend_Eulid(a, b, x, y, d);
if(d==1)
{
while(x<0)
{
x+=b;
y-=a;
}
printf("%lld %lld\n", x, y);
}
else
printf("sorry\n");
}
}
本文详细介绍了扩展欧几里得算法的应用,通过解决Xa+Yb=1的问题,探讨了如何寻找非负整数X和整数Y的解。通过AC代码示例,展示了算法的具体实现过程。
1万+

被折叠的 条评论
为什么被折叠?



