赤裸裸的,直接贴代码。。不过注意题目要求X非负,所以最后还要再处理一下。。
/* * hdu2669/win.cpp * Created on: 2012-7-6 * Author : ben */ #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <ctime> #include <iostream> #include <algorithm> #include <queue> #include <set> #include <map> #include <stack> #include <string> #include <vector> #include <deque> #include <list> #include <functional> #include <numeric> #include <cctype> using namespace std; int gcd(int m, int n) { int r = m % n; while (r) { m = n; n = r; r = m % n; } return n; } int extend_gcd(int a, int b, int &x, int &y) { int d, xx; if (b == 0) { x = 1, y = 0; return a; } d = extend_gcd(b, a % b, x, y); xx = x; x = y; y = xx - a / b * y; return d; } int main() { #ifndef ONLINE_JUDGE freopen("data.in", "r", stdin); #endif int m, n; while(scanf("%d%d", &m, &n) == 2) { if(gcd(m, n) > 1) { puts("sorry"); } else { int x, y; extend_gcd(m, n, x, y); while(x < 0) { x += n; y -= m; } printf("%d %d\n", x, y); } } return 0; }