Skip to content
Author: lllyouo
Date: 20250705
tag: 线性同余方程
link: https://www.luogu.com.cn/problem/P1082

问题描述

link

分析

参考代码

cpp
#include <bits/stdc++.h>
using namespace std;

int exgcd(int a, int b, int &x, int &y) {
    if (b == 0) {
        x = 1;
        y = 0;
        return a;
    }
    int d = exgcd(b, a % b, x, y);
    int t = x;
    x = y;
    y = t - y * (a / b);
    return d;
}

int main() {
    // ax = 1 mod b
    int a, b; cin >> a >> b;
    // ax + by = 1
    int x0, y0;
    int g = exgcd(a, b, x0, y0);
    int x = x0 * 1 / g;
    cout << (x % (b / g) + (b / g)) % (b / g) << endl;
    
    return 0;
}