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>
using namespace std;
int solution(string s, int M)
{
// apply_product;
string pf;
int num = 0;
for(int i = 0; i < s.size(); i++)
{
// cerr << "i : " << i << endl;
if('0' <= s[i] && s[i] <= '9')
num = (num * 10ll % M + s[i] - '0') % M;
else if(s[i] != '*')
pf += to_string(num), num = 0, pf += s[i];
else
{
int num2 = 0;
bool b = true;
for(int j = i + 1; j < s.size(); j++)
{
if(s[j] < '0' || s[j] > '9') {
i = j - 1;
b = false;
break;
}
num2 = (num2 * 10ll % M + s[j] - '0') % M;
}
// cerr << num << ' ' << num2 << endl;
// cerr << num * num2 % M << endl;
num = 1ll * num * num2 % M;
if(b) break;
}
}
// cerr << num << endl;
pf += to_string(num);
pf = "+" + pf;
// cerr << pf << endl;
int ans = 0;
for(int i = 0; i < pf.size(); i ++)
{
int c = (pf[i] == '+') ? 1 : -1;
int num2 = 0;
bool b = true;
for(int j = i + 1; j < pf.size(); j++)
{
if(pf[j] < '0' || pf[j] > '9') {
i = j - 1;
b = false;
break;
}
num2 = (num2 * 10ll % M + pf[j] - '0') % M;
}
// cerr << num2 << endl;
num2 *= c;
ans = ((ans + num2) % M + M) % M;
if(b) break;
}
// cerr << ans << endl;
return ans;
}
int solve(string s, int x, int M)
{
string problem = '(' + s + ')';
vector<int> last;
vector<string> exp = {""};
for(char i : problem)
{
if(i == '(')
exp.push_back("");
else if(i == ')')
{
// cerr << "exp: " << exp.back() << endl;
int val = solution(exp.back(), M);
exp.pop_back();
exp.back() += to_string(val);
}
else if(i == 'x')
exp.back() += to_string(x);
else
exp.back() += i;
}
return stoi(exp.back()) % M;
}
int main()
{
string s;
int P, M;
cin >> s >> P >> M;
int f = solve(s, 0, M), sec = solve(s, 1, M);
int d = ((sec - f) % M + M) % M;
// cerr << f << ' ' << sec << endl;
for(int i = 0; i < M; i++)
{
f = (f % M + M) % M;
if(f == P)
{
cout << i << endl;
return 0;
}
f += d;
}
assert(false);
cout << "I was just kidding\n";
return 0;
}
Compilation message (stderr)
nekameleoni.cpp: In function 'int solution(std::string, int)':
nekameleoni.cpp:11:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
11 | for(int i = 0; i < s.size(); i++)
| ~~^~~~~~~~~~
nekameleoni.cpp:22:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
22 | for(int j = i + 1; j < s.size(); j++)
| ~~^~~~~~~~~~
nekameleoni.cpp:42:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
42 | for(int i = 0; i < pf.size(); i ++)
| ~~^~~~~~~~~~~
nekameleoni.cpp:47:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
47 | for(int j = i + 1; j < pf.size(); j++)
| ~~^~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |