Submission #259439

#TimeUsernameProblemLanguageResultExecution timeMemory
259439NONAMESlon (COCI15_slon)C++14
0 / 120
7 ms640 KiB
#include <bits/stdc++.h> #define sz(x) int(x.size()) #define all(x) x.begin(), x.end() #define F first #define S second #define PB push_back #define MP make_pair #define dbg(x) cerr << #x << " = " << x << "\n" #define fast_io ios_base::sync_with_stdio(0); cin.tie(0); cout.tie() using namespace std; using ll = long long; int n, P, M; string s; stack <int> operand; bool is_digit(char c) { return (c >= '0' && c <= '9'); } int prior(char c) { return (c == '*' ? 2 : 1); } int solve(char op) { int v1 = operand.top(); operand.pop(); int v2 = operand.top(); operand.pop(); if (op == '*') (v1 *= v2) %= M; else if (op == '+') (v1 += v2) %= M; else if (op == '-') ((v1 -= v2) += M) %= M; return v1; } int rpn(int x) { string t = s; for (int i = 0; i < n; ++i) if (t[i] == 'x') t[i] = char(x + '0'); while (!operand.empty()) operand.pop(); stack <char> operation; for (int i = 0; i < n; ++i) { if (t[i] == ' ') continue; if (!is_digit(t[i])) { if (t[i] == '(') operation.push(t[i]); else if (t[i] == ')') { while (operation.top() != '(') operand.push(solve(operation.top())), operation.pop(); operation.pop(); } else { while (!operation.empty() && (operation.top() != '(') && (prior(operation.top()) >= prior(t[i]))) operand.push(solve(operation.top())), operation.pop(); operation.push(t[i]); } } else operand.push(t[i] - '0'); } while (!operation.empty()) operand.push(solve(operation.top())), operation.pop(); return operand.top(); } int main() { fast_io; cin >> s; n = sz(s); cin >> P >> M; int diff = rpn(2); ((diff -= rpn(1)) += M) %= M; int res = rpn(0); int ans = 0; // cout << res << "\n"; // cout << diff << "\n"; if (res % M == P) return void(cout << ans << "\n"), 0; for (int i = 0; i <= M; ++i) { ++ans; (res += diff) %= M; if (res == P) break; } cout << ans << "\n"; }
#Verdict Execution timeMemoryGrader output
Fetching results...