Submission #1351316

#TimeUsernameProblemLanguageResultExecution timeMemory
1351316chithanhnguyenSlon (COCI15_slon)C++20
120 / 120
3 ms580 KiB
/*
Author: Nguyen Chi Thanh - High School for the Gifted - VNU.HCM (i2528)
*/
#include <bits/stdc++.h>
using namespace std;

/* START OF TEMPALTE */

#define int long long
#define ll long long
#define ull unsigned long long
#define ld long double
#define pii pair<int, int>
#define pll pair<ll, ll>
#define fi first
#define se second
#define popcount __builtin_popcountll
#define all(x) (x).begin(), (x).end()
#define BIT(x, i) (((x) >> (i)) & 1)
#define MASK(x) (1ll << (x))
#define SZ(a) ((int32_t)a.size())

#define debug(a, l, r) {for (int _i = (l); _i <= (r); ++_i) cout << (a)[_i] << ' '; cout << '\n';}

template<class X, class Y>
bool minimize(X &x, const Y &y) {
    if (x > y) {
        x = y;
        return true;
    } else return false;
}

template<class X, class Y>
bool maximize(X &x, const Y &y) {
    if (x < y) {
        x = y;
        return true;
    } else return false;
}

/* END OF TEMPALTE */

string s;
int p, m;

int priority(char op) {
    if (op == '*') return 2;
    if (op == '+' || op == '-') return 1;
    return 0;
}

pii calc(pii a, pii b, char op) {
    if (op == '+') return {(a.fi + b.fi) % m, (a.se + b.se) % m};
    if (op == '-') {
        int left = ((a.fi - b.fi) % m + m) % m;
        int right = ((a.se - b.se) % m + m) % m;
        return {left, right};
    }
    
    int left = (a.fi * b.se % m) + (a.se * b.fi % m) % m;
    int right = a.se * b.se % m;
    return {left, right};
}

void solve() {
    cin >> s >> p >> m;

    vector<pii> val;
    vector<char> op;

    auto work = [&]() {
        pii b = val.back(); val.pop_back();
        pii a = val.back(); val.pop_back();
        char c = op.back(); op.pop_back();
        val.push_back(calc(a, b, c));
    };

    for (int i = 0; i < SZ(s);) {
        if (isdigit(s[i])) {
            int x = 0;
            while (i < SZ(s) && isdigit(s[i])) {
                x = ((x * 10) % m + (s[i] - '0')) % m;
                ++i;
            }
            val.push_back({0, x});
        } 
        else if (s[i] == 'x') {
            val.push_back({1, 0});
            ++i;
        }
        else if (s[i] == '(') {
            op.push_back(s[i]);
            ++i;
        } else if (s[i] == ')') {
            while (op.back() != '(') work();
            op.pop_back();
            ++i;
        } else {
            while (!op.empty() && op.back() != '(' && priority(op.back()) >= priority(s[i])) 
                work();
            op.push_back(s[i]);
            ++i;
        }
    }

    while (!op.empty()) work();

    pii poly = val.back();
    for (int i = 0; i < m; ++i) {
        int val = ((poly.fi * i % m) + poly.se) % m;
        if (val == p) {
            cout << i;
            return;
        }
    }
}

signed main() {
    #ifdef NCTHANH
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
    ios_base::sync_with_stdio(0);
    cin.tie(nullptr); cout.tie(nullptr);

    solve();

    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...