题目描述
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.
输入
The input contains multiple test cases.
Each case two nonnegative integer a,b (0<a, b<=2^31)
输出
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.
样例输入 Copy
77 51
10 44
34 79
样例输出 Copy
2 -3
sorry
7 -3

数学-扩展欧

直接打扩展欧的板子,注意ax+by=1。若x<0,让x+=b,y-=a,等式不变

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll exgcd(ll a,ll b,ll &x,ll &y)
{
	if(b==0)
	{
		x=1,y=0;return a;
	}
	ll d=exgcd(b,a%b,x,y);
	ll tmp=x;
	x=y;
	y=tmp-a/b*y;
	return d;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    ll a,b;
    while(cin>>a>>b)
    {
    	ll x,y;
        ll d=exgcd(a,b,x,y);
        while(x<0) x+=b,y-=a;
    	if(d!=1) cout<<"sorry"<<'\n';
    	else cout<<x<<' '<<y<<'\n';
    }
}

 posted on 2023-05-19 10:32  ruoye123456  阅读(23)  评论(0)    收藏  举报