1056. 线性同余方程

shwei
#include <iostream>

using namespace std;

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

int main() {
    int a, b, m;
    // ax + my = b
    while (cin >> a >> b >> m) {
        int x, y;
        int d = exgcd(a, m, x, y);
        if (b % d != 0) cout << "No Answer." << endl;
        else {
            x *= b / d; // 特解x'
            // x = x' + k*m/d
            int t = m / d;
            x = (x % t + t) % t;
            for (int i = 0; i < d; i++) {
                cout << x << " ";
                x += t;
            }
            cout << endl;
        }
    }
    return 0;
}
你当前正在回复 博客/题目
存在问题!