1、欧几里得

欧几里得方法又称:“辗转相乘法”,这个很简单,直接上代码

long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }

2、拓展欧几里得

void exd_gcd(long long a, long long b, long long &x, long long &y) {
    if (b == 0) {
        x = 1;
        y = 0;
        return;
    }
    long long x1, y1;
    exd_gcd(b, a % b, x1, y1);
    x = y1;
    y = x1 - a / b * y1;
    return;
}

3、习题:青蛙约会

拓展欧几里得算法_技术
答案

#include <stdarg.h>
#include <stdio.h>
void exd_gcd(long long a, long long b, long long &x, long long &y) {
    if (b == 0) {
        x = 1;
        y = 0;
        return;
    }
    long long x1, y1;
    exd_gcd(b, a % b, x1, y1);
    x = y1;
    y = x1 - a / b * y1;
    return;
}

long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }

int main() {
    long long x, y, m, n, L;
    while (~scanf("%lld%lld%lld%lld%lld", &x, &y, &m, &n, &L)) {
        long long a = L, b = n - m, x0, y0;
        long long d = gcd(a, b);
        if ((x - y) % d)
            printf("Impossible\n");
        else {
            exd_gcd(a, b, x0, y0);
            y0 = (x - y) * y0;
            printf("%d\n", (y0 % a + a) % a);
        }
    }
    return 0;
}