p140.cpp
======================
#include <bits/stdc++.h>

using namespace std;

template<typename T1, typename T2>
ostream& operator<<(ostream& out, const pair<T1, T2>& x) {
    return out << x.first << ' ' << x.second;
}

template<typename T1, typename T2>
istream& operator>>(istream& in, pair<T1, T2>& x) {
    return in >> x.first >> x.second;
}

template<typename T>
istream& operator>>(istream& in, vector<T>& a) {
    for(auto& x: a) {
        in >> x;
    }
    return in;
};

template<typename T>
ostream& operator<<(ostream& out, const vector<T>& a) {
    for(auto x: a) {
        out << x << ' ';
    }
    return out;
};

int n, p, b;
vector<int> a;

void read() {
    cin >> n >> p >> b;
    a.resize(n);
    cin >> a;

    for(auto& ai: a) {
        ai %= p;
    }
}

int64_t extended_euclid(int64_t a, int64_t b, int64_t& x, int64_t& y) {
    if(b == 0) {
        x = 1;
        y = 0;
        return a;
    }
    int64_t x1, y1;
    int64_t d = extended_euclid(b, a % b, x1, y1);
    x = y1;
    y = x1 - y1 * (a / b);
    return d;
}

pair<vector<int64_t>, int64_t> inductive_solve(const vector<int>& a) {
    vector<int64_t> x(a.size());
    int64_t g = extended_euclid(
        a[a.size() - 2], a[a.size() - 1], x[a.size() - 2], x[a.size() - 1]
    );

    for(int i = n - 2; i >= 0; i--) {
        int64_t prv_g = g, mult;
        g = extended_euclid(a[i], prv_g, x[i], mult);
        for(int j = i + 1; j < n; j++) {
            x[j] = x[j] * mult;
        }
    }

    return {x, g};
}

void solve() {
    a.push_back(p);
    auto [x, g] = inductive_solve(a);

    if(b % g != 0) {
        cout << "NO\n";
        return;
    }

    cout << "YES\n";
    x.pop_back();

    int64_t mult = b / g;
    for(auto& xi: x) {
        xi = xi * mult % p;
        if(xi < 0) {
            xi += p;
        }
    }

    cout << x << '\n';
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int T = 1;
    // cin >> T;
    for(int test = 1; test <= T; test++) {
        read();
        // cout << "Case #" << test << ": ";
        solve();
    }

    return 0;
}

=================
statement.txt
======================
140. Integer Sequences

time limit per test: 0.25 sec.
memory limit per test: 4096 KB


A sequence A is called an integer sequence of length N if all its elements A1 A2 .. AN are non-negative integers less than 2 000 000 000. Consider two integer sequences of length N, A and X. The result of their multiplication (A*X) is an integer number R=A1*X1 + A2*X2 + .. + AN*XN. Your task is to solve the equation A*X=B (mod P), given the integer sequence A and the integer numbers B and P.


Input

The first line contains the integer numbers N (1<=N<=100) - the length of the integer sequences - P (1<=P<=10 000) and B (0<=B<=P-1). The second line contains the elements of the sequence A, separated by blanks: A1 A2 .. AN.


Output

You should print one line containing the word "YES" if there exists at least one integer sequence X which is a solution to the equation, or print "NO" otherwise. If the answer is "YES", the next line should contain N non-negative integers separated by blanks: X1 X2 .. XN.


Sample Input #1

2 7 4
7 3
Sample Output #1

YES
0 6
Sample Input #2

3 10 1
2 4 6
Sample Output #2

NO
Author	: Mugurel Ionut Andreica
Resource	: SSU::Online Contester Fall Contest #2
Date	: Fall 2002

=================
