# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
920159 | TIN | Slon (COCI15_slon) | C++17 | 6 ms | 3328 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define FNAME "test"
#define sz(s) (int) (s).size()
string A;
int P, M;
typedef struct Node {
char data;
int a, b;
struct Node *L, *R;
} * nptr;
nptr newNode(char c) {
nptr n = new Node;
n->data = '.', n->a = 0, n->b = 0;
if (isdigit(c)) n->a = 0, n->b = (c - '0');
else if (isalpha(c)) n->a = 1, n->b = 0;
else n->data = c;
n->L = n->R = nullptr;
return n;
}
nptr build(string& s) {
stack<nptr> stN;
stack<char> stC;
nptr t, t1, t2;
int p[123] = { 0 };
p['+'] = p['-'] = 1;
p['/'] = p['*'] = 2;
p['^'] = 3;
p[')'] = 0;
for (int i = 0; i < sz(s); i++) {
if (s[i] == '(') stC.push(s[i]);
else if (isalpha(s[i]) || isdigit(s[i])) {
t = newNode(s[i]);
stN.push(t);
} else if (p[s[i]] > 0) {
while (!stC.empty() && stC.top() != '(' && (p[stC.top()] >= p[s[i]])) {
t = newNode(stC.top()); stC.pop();
t1 = stN.top(); stN.pop();
t2 = stN.top(); stN.pop();
t->L = t2, t->R = t1;
stN.push(t);
}
stC.push(s[i]);
} else if (s[i] == ')') {
while (!stC.empty() && stC.top() != '(') {
t = newNode(stC.top()); stC.pop();
t1 = stN.top(); stN.pop();
t2 = stN.top(); stN.pop();
t->L = t2, t->R = t1;
stN.push(t);
}
stC.pop();
}
}
t = stN.top();
return t;
}
void Task() {
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cout << fixed << setprecision(9);
if (fopen(FNAME".inp","r")) {
freopen(FNAME".inp","r",stdin);
freopen(FNAME".out","w",stdout);
}
}
pair<int,int> get(nptr root) {
if (root == nullptr) return {0, 0};
if (root->data == '.') return {root->a, root->b};
pair<int,int> p1 = get(root->L);
pair<int,int> p2 = get(root->R);
pair<int,int> p;
if (root->data == '+') {
p.first = p1.first + p2.first;
p.second = p1.second + p2.second;
}
if (root->data == '-') {
p.first = p1.first - p2.first;
p.second = p1.second - p2.second;
}
if (root->data == '*') {
p.first = p1.first * p2.second + p2.first * p1.second;
p.second = p1.second * p2.second;
}
return p;
}
void Solve() {
//Your Code
cin >> A;
cin >> P >> M;
P %= M;
A = "(" + A + ")";
nptr root = build(A);
pair<int,int> p = get(root);
int a = p.first, b = p.second;
b %= M;
P = P + M - b;
for (int x = 0; x < M; x++) {
if ((a * x - P) % M == 0) {
cout << x;
return;
}
}
}
int main() {
Task();
Solve();
cerr << "\nTime run: " << 1000*clock()/CLOCKS_PER_SEC << "ms";
return 37^37;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |