답안 #340056

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
340056 2020-12-26T16:56:36 Z AlperenT Slon (COCI15_slon) C++17
120 / 120
6 ms 620 KB
#include <bits/stdc++.h>

using namespace std;

long long p, m, numx, numa;

string str, postfix;
stack<char> poststack;

struct operand{
	long long x, a;

	operand operator+(operand y){
		struct operand temp;
		temp.x = (x + y.x) % m;
		temp.a = (a + y.a) % m;
		return temp;
	}

	operand operator-(operand y){
		struct operand temp;
		temp.x = (x - y.x) % m;
		temp.a = (a - y.a) % m;
		return temp;
	}

	operand operator*(operand y){
		struct operand temp;
		
		if(x == 0){
			temp.x = (a * y.x) % m;
			temp.a = (a * y.a) % m;
		}
		else{
			temp.x = (y.a * x) % m;
			temp.a = (y.a * a) % m;
		}

		return temp;
	}
};

stack<operand> operands;

int main(){    
	cin >> str >> p >> m;

	for(int i = 0; i < str.size(); i++){
		if(str[i] == 'x'){
			postfix += str[i];
			postfix += " ";
		}
		else if(isdigit(str[i])){
			long long val = 0;
			while(i < str.size() && isdigit(str[i])){
				val = ((val * 10) + (str[i] - '0')) % m;
				i++;
			}
			postfix += to_string(val);
			postfix += " ";
			i--;
		}
		else{
			if(str[i] == '+' || str[i] == '-'){
				while(!poststack.empty() && poststack.top() != '(' && poststack.top() != ')'){
					postfix += poststack.top();
					postfix += " ";
					poststack.pop();
				}
				poststack.push(str[i]);
			}
			else if(str[i] == '*'){
				while(!poststack.empty() && poststack.top() == '*' && poststack.top() != '(' && poststack.top() != ')'){
					postfix += poststack.top();
					postfix += " ";
					poststack.pop();
				}
				poststack.push(str[i]);
			}
			else if(str[i] == '('){
				poststack.push(str[i]);
			}
			else if(str[i] == ')'){
				while(!poststack.empty() && poststack.top() != '('){
					postfix += poststack.top();
					postfix += " ";
					poststack.pop();
				}
				if(poststack.top() == '('){
					poststack.pop();
				}
			}
		}
	}

	while(!poststack.empty()){
		postfix += poststack.top();
		postfix += " ";
		poststack.pop();
	}

	for(int i = 0; i < postfix.size(); i++){
		if(isdigit(postfix[i])){
			long long val = 0;

			while(i < postfix.size() && isdigit(postfix[i])){
				val = ((val * 10) + (postfix[i] - '0')) % m;
				i++;
			}

			struct operand temp;
			temp.x = 0;
			temp.a = val;

			operands.push(temp);
		}
		else if(postfix[i] == 'x'){
			struct operand temp;
			temp.x = 1;
			temp.a = 0;

			operands.push(temp);
		}
		else if(postfix[i] == '+' || postfix[i] == '-' || postfix[i] == '*'){
			struct operand tempa, tempb;

			tempb = operands.top();
			operands.pop();

			tempa = operands.top();
			operands.pop();

			if(postfix[i] == '+'){
				operands.push(tempa + tempb);
			}

			else if(postfix[i] == '-'){
				operands.push(tempa - tempb);
			}

			else{
				operands.push(tempa * tempb);
			}
		}
	}

	numx = operands.top().x;
	numa = operands.top().a;

	if(numx < 0){
		numx = m - abs(numx);
	}

	if(numa < 0){
		numa = m - abs(numa);
	}

	numx %= m;
	numa %= m;

	for(long long i = 0; i <= m; i++){
		if((((numx * i) + numa) % m ) == p){
			cout << i << "\n";
			return 0;
		}
	}
}

Compilation message

slon.cpp: In function 'int main()':
slon.cpp:48:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   48 |  for(int i = 0; i < str.size(); i++){
      |                 ~~^~~~~~~~~~~~
slon.cpp:55:12: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   55 |    while(i < str.size() && isdigit(str[i])){
      |          ~~^~~~~~~~~~~~
slon.cpp:102:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  102 |  for(int i = 0; i < postfix.size(); i++){
      |                 ~~^~~~~~~~~~~~~~~~
slon.cpp:106:12: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  106 |    while(i < postfix.size() && isdigit(postfix[i])){
      |          ~~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 364 KB Output is correct
2 Correct 6 ms 620 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 2 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 3 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 2 ms 364 KB Output is correct
9 Correct 3 ms 492 KB Output is correct
10 Correct 4 ms 512 KB Output is correct