• hdu2669扩展欧几里得


    赤裸裸的,直接贴代码。。不过注意题目要求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;
    }
  • 相关阅读:
    luogu P5494 【模板】线段树分裂
    珂朵莉树(ODT)
    luogu P5787 二分图 /【模板】线段树分治
    线段树
    luogu P1450 [HAOI2008]硬币购物
    树形DP
    luogu P3047 [USACO12FEB]Nearby Cows G
    1069: 向Z同学学习
    1067: 有问题的里程表
    1066: 字符分类统计
  • 原文地址:https://www.cnblogs.com/moonbay/p/2579491.html
Copyright © 2020-2023  润新知