#include <bits/stdc++.h>
using namespace std;
#define FNAME "test"
#define sz(s) (int) (s).size()
#define int long long
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] == ' ') continue;
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;
P = (P + M - b) % M;
for (int x = 0; x < M; x++) {
if ((a * x) % M == P % M) {
cout << x;
return;
}
}
}
signed main() {
Task();
Solve();
cerr << "\nTime run: " << 1000*clock()/CLOCKS_PER_SEC << "ms";
return 37^37;
}
Compilation message
slon.cpp: In function 'Node* build(std::string&)':
slon.cpp:47:20: warning: array subscript has type 'char' [-Wchar-subscripts]
47 | } else if (p[s[i]] > 0) {
| ^
slon.cpp:48:57: warning: array subscript has type 'char' [-Wchar-subscripts]
48 | while (!stC.empty() && stC.top() != '(' && (p[stC.top()] >= p[s[i]])) {
| ~~~~~~~^~
slon.cpp:48:70: warning: array subscript has type 'char' [-Wchar-subscripts]
48 | while (!stC.empty() && stC.top() != '(' && (p[stC.top()] >= p[s[i]])) {
| ^
slon.cpp: In function 'void Task()':
slon.cpp:77:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
77 | freopen(FNAME".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
slon.cpp:78:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
78 | freopen(FNAME".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
344 KB |
Output isn't correct |
2 |
Incorrect |
6 ms |
3068 KB |
Output isn't correct |
3 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
4 |
Incorrect |
3 ms |
344 KB |
Output isn't correct |
5 |
Incorrect |
3 ms |
344 KB |
Output isn't correct |
6 |
Incorrect |
4 ms |
600 KB |
Output isn't correct |
7 |
Incorrect |
1 ms |
856 KB |
Output isn't correct |
8 |
Incorrect |
3 ms |
1112 KB |
Output isn't correct |
9 |
Incorrect |
4 ms |
2392 KB |
Output isn't correct |
10 |
Incorrect |
3 ms |
2136 KB |
Output isn't correct |