답안 #259441

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
259441 2020-08-07T20:34:17 Z NONAME Slon (COCI15_slon) C++14
0 / 120
8 ms 640 KB
#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 {
            string cur = "";
            int j = i;
            while (j < n && is_digit(t[j]))
                cur += t[j++];

            operand.push(atoi(cur.c_str()));
            i = j - 1;
        }
    }

    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 << "0\n"), 0;

    for (int i = 0; i <= M; ++i) {
        ++ans;
        (res += diff) %= M;
        if (res == P)
            break;
    }

    cout << ans << "\n";
}
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 384 KB Output isn't correct
2 Incorrect 8 ms 640 KB Output isn't correct
3 Incorrect 1 ms 384 KB Output isn't correct
4 Incorrect 1 ms 384 KB Output isn't correct
5 Incorrect 3 ms 384 KB Output isn't correct
6 Incorrect 6 ms 384 KB Output isn't correct
7 Incorrect 1 ms 384 KB Output isn't correct
8 Incorrect 5 ms 384 KB Output isn't correct
9 Incorrect 6 ms 512 KB Output isn't correct
10 Incorrect 5 ms 584 KB Output isn't correct