Language: C++



#include <iostream>
#include <vector>
#include <cmath> // ??????

#define REP(i,n) for(int (i)=0;(i)<(int)(n);(i)++)
#define RREP(i,n) for(int (i)=(int)(n)-1;i>=0;i--)
#define REMOVE(Itr,n) (Itr).erase(remove((Itr).begin(),(Itr).end(),n),(Itr).end())
typedef long long ll;


using namespace std;

long long inv_mod(long long n, long long mod) {
    long long a = n % mod, b = mod - 2, ret = 1;
    while (b > 0) {
        if (b & 1) ret = (ret * a) % mod;
        a = (a * a) % mod;
        b >>= 1;
    }
    return ret;
}

struct FourArithmeticOperationParser {
    
    int pos;
    bool div_zero;
    string Formula;
    long long mod;
    const long long INF = 0x7fffffffffffffff;
    
    long long operator () (string F, long long m) {
        pos = 0;
        div_zero = false;
        Formula = F;
        mod = m;
        long long ret = expr();
        return div_zero ? INF : ret;
    }
    
    long long number() {
        long long ret = 0;
        while (pos != Formula.size()) {
            if (Formula[pos] < '0' || Formula[pos] > '9') {
                break;
            } else {
                ret *= 10;
                ret += (Formula[pos++] - '0');
            }
        }
        return ret % mod;
    }
    
    long long expr() {
        long long x = term();
        while (pos < Formula.size()) {
            switch (Formula[pos]) {
                case '+':
                    pos++;
                    x += term();
                    continue;
                case '-':
                    pos++;
                    x -= term();
                    continue;
            }
            break;
        }
        return x % mod;
    }
    
    long long term() {
        long long x = factor();
        while (pos < Formula.size()) {
            switch (Formula[pos]) {
                case '*':
                    pos++;
                    x *= factor();
                    continue;
                case '/':
                    pos++;
                    long long div = factor();
                    if (div == 0) {
                        div_zero = true;
                    } else {
                        div = inv_mod(div, mod);
                        x *= div;
                        x %= mod;
                    }
                    continue;
            }
            break;
        }
        return x % mod;
    }
    
    long long factor() {
        if (pos < Formula.size()) {
            if (Formula[pos] == '(') {
                pos++;
                long long ret = expr();
                if (Formula[pos] == ')') {
                    pos++;
                }
                return ret;
            }
        }
        return number();
    }
    
};

int main() {
   
    string mod;
    string f;
    
    while (true) {
        cin >> mod;
        long long m = stoi(mod.substr(0, mod.size() - 1));
        if (m == 0) break;
        getline(cin, f);
        REMOVE(f, ' ');
        ll ans = FourArithmeticOperationParser()(f, m);
        if (ans == FourArithmeticOperationParser().INF) cout << "NG" << endl;
        else {
            ans %= m;
            ans = (ans + m * 2) % m;
            cout << f << " = " << ans << " (mod " << m << ")" << endl;
        }
    }
   
    return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:9:53: error: cannot convert 'std::__cxx11::basic_string<char>::iterator' to 'const char*'
    9 | #define REMOVE(Itr,n) (Itr).erase(remove((Itr).begin(),(Itr).end(),n),(Itr).end())
      |                                          ~~~~~~~~~~~^~
      |                                                     |
      |                                                     std::__cxx11::basic_string<char>::iterator
a.cc:124:9: note: in expansion of macro 'REMOVE'
  124 |         REMOVE(f, ' ');
      |         ^~~~~~
In file included from /usr/include/c++/14/cstdio:42,
                 from /usr/include/c++/14/ext/string_conversions.h:45,
                 from /usr/include/c++/14/bits/basic_string.h:4154,
                 from /usr/include/c++/14/string:54,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:3:
/usr/include/stdio.h:158:32: note:   initializing argument 1 of 'int remove(const char*)'
  158 | extern int remove (const char *__filename) __THROW;
      |                    ~~~~~~~~~~~~^~~~~~~~~~