#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;
temp.a = a + y.a;
return temp;
}
operand operator-(operand y){
struct operand temp;
temp.x = x - y.x;
temp.a = a - y.a;
return temp;
}
operand operator*(operand y){
struct operand temp;
if(x == 0){
temp.x = a * y.x;
temp.a = a * y.a;
}
else{
temp.x = y.a * x;
temp.a = y.a * a;
}
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');
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');
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 % m;
numa = operands.top().a % m;
for(long long i = 0; i <= m; i++){
if((numx * i + numa) % m == p){
cout << i;
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 |
Incorrect |
3 ms |
364 KB |
Output isn't correct |
2 |
Runtime error |
6 ms |
1132 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
3 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
4 |
Incorrect |
5 ms |
364 KB |
Output isn't correct |
5 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
6 |
Incorrect |
9 ms |
364 KB |
Output isn't correct |
7 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
8 |
Incorrect |
5 ms |
364 KB |
Output isn't correct |
9 |
Incorrect |
7 ms |
492 KB |
Output isn't correct |
10 |
Incorrect |
4 ms |
492 KB |
Output isn't correct |