답안 #339958

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
339958 2020-12-26T12:23:31 Z Nimbostratus Slon (COCI15_slon) C++17
0 / 120
8 ms 896 KB
#include <bits/stdc++.h>
#define pb push_back
#define ppb pop_back
#define lb lower_bound
#define ub upper_bound
#define all(x) (x.begin() , x.end())
#define sz(x) (x.size())
#define fre freopen("in","r",stdin); freopen("out","w",stdout);
#define fast_io ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef vector<pair<int,int>> vpii;
typedef vector<int> vint;

const int MAXN = 1e5;

int prec(char op)
{
	if(op == '+' or op == '-') return 1;
	if(op == '*') return 2;
	return 0;
}

int applyOp(int val1,int val2,char op)
{
	if(op == '+') return val1 + val2;
	if(op == '-') return val1 - val2;
	if(op == '*') return val1 * val2;
}

int eval(string a)
{
	stack<char> op;
	stack<int> val;
	for(int i=0;i<sz(a);i++)
	{
		if(a[i] == '(') op.push('(');
		else if(isdigit(a[i]))
		{
			int num = 0;
			while(i < sz(a) and isdigit(a[i]))
			{
				num = num*10 + (a[i]-'0');
				i++;
			}
			val.push(num);
			i--;
		}
		else if(a[i] == ')')
		{
			while(!op.empty() and op.top() != '(')
			{
				int val2 = val.top();
				val.pop();
				int val1 = val.top();
				val.pop();
				char opp = op.top();
				op.pop();
				val.push(applyOp(val1,val2,opp));
			}
			op.pop();
		}
		else
		{
			while(!op.empty() and prec(op.top()) >= prec(a[i]))
			{
				int val2 = val.top();
				val.pop();
				int val1 = val.top();
				val.pop();
				char opp = op.top();
				op.pop();
				val.push(applyOp(val1,val2,opp));
			}
			op.push(a[i]);
		}
	}
	while(!op.empty())
	{
		int val2 = val.top();
		val.pop();
		int val1 = val.top();
		val.pop();
		char opp = op.top();
		op.pop();
		val.push(applyOp(val1,val2,opp));
	}
	return val.top();
}


int extract_c(string& b)
{
	string a = b;
	for(int i=0;i<sz(a);i++)
		if(a[i] == 'x')
			a[i] = '0';
	return eval(a);
}


int extract_x (string& b)
{
	string a = b;
	for(int i=0;i<sz(a);i++)
		if(a[i] == 'x')
			a[i] = '1';
	return eval(a) - extract_c(b);
}

string A;
int M,P;

int32_t main()
{
	//fre;
	fast_io;
	cin >> A >> P >> M;
	int cx = extract_x(A);
	int cc = extract_c(A);
	//cout << cx << " " << cc << endl;
	for(int i=0;i<=M+1;i++)
	{
		if((cx*i + cc)% M == P)
		{
			cout << i << endl;
			return 0;
		}
	}
}

Compilation message

slon.cpp: In function 'int eval(std::string)':
slon.cpp:36:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   36 |  for(int i=0;i<sz(a);i++)
      |               ^
slon.cpp:42:12: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   42 |    while(i < sz(a) and isdigit(a[i]))
      |            ^
slon.cpp: In function 'int extract_c(std::string&)':
slon.cpp:96:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   96 |  for(int i=0;i<sz(a);i++)
      |               ^
slon.cpp: In function 'int extract_x(std::string&)':
slon.cpp:106:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  106 |  for(int i=0;i<sz(a);i++)
      |               ^
slon.cpp: In function 'int applyOp(int, int, char)':
slon.cpp:30:1: warning: control reaches end of non-void function [-Wreturn-type]
   30 | }
      | ^
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 364 KB Output isn't correct
2 Incorrect 8 ms 896 KB Output isn't correct
3 Incorrect 1 ms 364 KB Output isn't correct
4 Incorrect 5 ms 364 KB Output isn't correct
5 Incorrect 4 ms 364 KB Output isn't correct
6 Incorrect 5 ms 364 KB Output isn't correct
7 Incorrect 1 ms 364 KB Output isn't correct
8 Incorrect 5 ms 492 KB Output isn't correct
9 Incorrect 5 ms 620 KB Output isn't correct
10 Incorrect 3 ms 748 KB Output isn't correct