# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
339958 | Nimbostratus | Slon (COCI15_slon) | C++17 | 8 ms | 896 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>
#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 (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |