# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
1038740 | vjudge1 | Slon (COCI15_slon) | C++17 | 9 ms | 860 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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;
}
컴파일 시 표준 에러 (stderr) 메시지
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |