hdu2669-Romantic

来源:互联网 发布:淘宝开店的类目 编辑:程序博客网 时间:2024/05/01 06:54

题意: 给定a,b,求出方程ax+by=1的解,x是最小的非负数。

简单的扩展欧几里得。

 通解为:x = x0 + b*t

     y = y0 – a*t

代码:

#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <cmath>using namespace std;int E_Gcd(int a, int b, int &x, int &y){if (b == 0){x = 1;y = 0;return a;}int ans = E_Gcd(b, a%b, x, y);int tem = x;x = y;y = tem - a / b*y;return ans;}int main(){int a, b;int x, y;while (scanf("%d%d", &a, &b) != EOF){int ans = E_Gcd(a, b, x, y);//ans表示为a,b的最大公约数。//printf("%d\n", ans);if (ans != 1) puts("sorry");//如果a,b不互质则无解。else{while (x < 0){x += b;//求满足的最小非负x y -= a;}printf("%d %d\n", x, y);}}return 0;}


0 0
原创粉丝点击