链接:戳这里
Romantic
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Problem Description
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 X*a + Y*b = 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
思路:
裸地扩展欧几里得算法 我的一些推导过程在代码中给出
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<iomanip>
#include<cmath>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
#define maxn 0x3f3f3f3f
#define MAX 1000100
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef unsigned long long ull;
#define INF (1ll<<60)-1
using namespace std;
ll gcd(ll a,ll b){
return b==0 ? a : gcd(b,a%b);
}
/*
满足|x| + |y| 最小 使得 ax+by=d
d=gcd(a,b)
ax1+by1=gcd(a,b);
bx2+(a%b)y2=gcd(b,a%b);
=> ax1+by1=bx2+(a%b)y2
ax1+by1=bx2+(a-a/b*b)y2
ax1+by1=bx2+ay2-b*(a/b)y2
ax1+by1=ay2+b(x2-(a/b)y2)
=> x1=y2
y1=x2-(a/b)*y2
*/
void exgcd(ll a,ll b,ll &d,ll &x,ll &y){
if(!b) {d=a;x=1;y=0;}
else {
exgcd(b,a%b,d,x,y);
ll x1=y;
ll y1=x-(a/b)*y;
x=x1;y=y1;
/*
exgcd(b,a%b,d,y,x);
y=y-x*(a/b);
*/
}
}
int main(){
ll a,b;
while(scanf("%I64d%I64d",&a,&b)!=EOF){
if(gcd(a,b)!=1) printf("sorry\n");
else {
ll d=1,x,y;
exgcd(a,b,d,x,y);
while(x<=0){
x+=b;
y-=a;
}
/*
ax+by=1 (x<0)
a(x+1)=1-by+a
ax'=1-b(y-a/b)
x'=x+1
y'=y-a/b
=>x+ 1 <==>y+ -a/b
x+ b <==>y+ -a
*/
printf("%I64d %I64d\n",x,y);
}
}
return 0;
}

本文详细介绍了如何使用扩展欧几里得算法解决特定类型的数学问题,即找到两个非负整数a和b的非负整数解X和整数解Y,使等式X*a + Y*b = 1成立。
1万+

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



