Submission #920159

# Submission time Handle Problem Language Result Execution time Memory
920159 2024-02-02T07:03:12 Z TIN Slon (COCI15_slon) C++17
0 / 120
6 ms 3328 KB
#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

slon.cpp: In function 'Node* build(std::string&)':
slon.cpp:44:20: warning: array subscript has type 'char' [-Wchar-subscripts]
   44 |   } else if (p[s[i]] > 0) {
      |                    ^
slon.cpp:45:57: warning: array subscript has type 'char' [-Wchar-subscripts]
   45 |    while (!stC.empty() && stC.top() != '(' && (p[stC.top()] >= p[s[i]])) {
      |                                                  ~~~~~~~^~
slon.cpp:45:70: warning: array subscript has type 'char' [-Wchar-subscripts]
   45 |    while (!stC.empty() && stC.top() != '(' && (p[stC.top()] >= p[s[i]])) {
      |                                                                      ^
slon.cpp: In function 'void Task()':
slon.cpp:74:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   74 |   freopen(FNAME".inp","r",stdin);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
slon.cpp:75:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   75 |   freopen(FNAME".out","w",stdout);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 3 ms 348 KB Output isn't correct
2 Incorrect 6 ms 3328 KB Output isn't correct
3 Incorrect 1 ms 348 KB Output isn't correct
4 Incorrect 3 ms 348 KB Output isn't correct
5 Incorrect 3 ms 348 KB Output isn't correct
6 Incorrect 5 ms 604 KB Output isn't correct
7 Incorrect 1 ms 860 KB Output isn't correct
8 Incorrect 4 ms 1116 KB Output isn't correct
9 Incorrect 4 ms 2396 KB Output isn't correct
10 Incorrect 3 ms 2396 KB Output isn't correct