| # | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 | 
|---|---|---|---|---|---|---|---|
| 255752 | Vimmer | Slon (COCI15_slon) | C++14 | 2 ms | 640 KiB | 
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//#pragma GCC optimize("unroll-loops")
//#pragma GCC optimize("-O3")
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("fast-math")
//#pragma GCC optimize("no-stack-protector")
#define F first
#define S second
#define sz(x) int(x.size())
#define pb push_back
#define pf push_front
#define N 100050
#define M ll(1e9 + 7)
#define inf 1e9 + 1e9
using namespace std;
//using namespace __gnu_pbds;
typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
typedef short int si;
typedef array <ll, 3> a3;
typedef array <ll, 5> a5;
//typedef tree <ll, null_type, less_equal<ll>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
string a;
ll p, m;
ll sm(ll x, ll y) {return (x + y + m + m) % m;}
ll mult(ll x, ll y) {return (x * y) % m;}
bool gd(int x) {return a[x] != '+' && a[x] != '-' && a[x] != '*' && a[x] != '(' && a[x] != ')';}
pair <pair <ll, ll>, int>  calc(int v)
{
    ll kf = 0, val = 0, otr = 1;
    for (int i = v + 1; i < sz(a); i++)
    {
        if (a[i] == ')') return {{val, kf}, i + 1};
        if (a[i] == '(')
        {
            pair <pair <ll, ll>, int> pt = calc(i);
            i = pt.S;
            if (a[i] != '*')
            {
                val = sm(val, mult(otr, pt.F.F));
                kf = sm(kf, mult(otr, pt.F.S));
                i--;
                continue;
            }
            else
             {
                ll cur = max(1ll, pt.F.F), kft = 0, from = 1, dob = 0;
                if (pt.F.S != 0) {if (pt.F.F != 0) dob = 1; kft = pt.F.S;}
                  else from = cur;
                i++;
                while (1)
                {
                    if (a[i] == '(')
                    {
                        pair <pair <ll, ll>, int> pt = calc(i);
                        if (pt.F.S == 0) from = mult(from, pt.F.F);
                        if (pt.F.S != 0) kft = pt.F.S;
                        if (pt.F.F != 0) {if (kft == 0 || pt.F.S != 0) dob = 1; cur = mult(cur, pt.F.F);}
                        i = pt.S;
                        if (a[i] == '*') {i++; continue;}
                        kft = mult(kft, from);
                        kf = sm(kf, mult(kft, otr));
                        if (kft == 0) val = sm(val, mult(cur, otr));
                        else
                        val = sm(val, mult(dob, mult(cur, otr)));
                        if (a[i] == '+') otr = 1;
                        if (a[i] == '-') otr = -1;
                        if (a[i] == ')') return {{val, kf}, i + 1};
                        break;
                    }
                    ll num = 0, j = 0;
                    while (i + j + 1 < sz(a) && gd(i + j + 1)) j++;
                    for (int u = i; u <= i + j; u++) {num *= 10; num += a[u] - '0'; num = sm(num, 0);}
                    if (j == 0 && a[i + j] == 'x') num = -1;
                    if (num == -1) {kft = 1;}else {from = mult(from, num); cur = mult(cur, num);}
                    i += j + 1;
                    if (a[i] == '*') {i++; continue;}
                    kft = mult(from, kft);
                    kf = sm(kf, mult(kft, otr));
                    if (kft == 0) val = sm(val, mult(cur, otr));
                    else
                    val = sm(val, mult(dob, mult(cur, otr)));
                    if (a[i] == '+') otr = 1;
                    if (a[i] == '-') otr = -1;
                    if (a[i] == ')') return {{val, kf}, i + 1};
                    break;
                }
            }
            continue;
        }
        if (a[i] == '+') {otr = 1; continue;}
        if (a[i] == '-') {otr = -1; continue;}
        int j = 0;
        ll num = 0;
        while (i + j + 1 < sz(a) && gd(i + j + 1)) j++;
        for (int u = i; u <= i + j; u++) {num *= 10; num += a[u] - '0'; num = sm(num, 0);}
        if (j == 0 && a[i + j] == 'x')  num = -1;
        i += j + 1;
        if (a[i] == '+') {if (num != -1) val = sm(val, mult(otr, num)); else kf = sm(kf, otr); otr = 1; continue;}
        if (a[i] == '-') {if (num != -1) val = sm(val, mult(otr, num)); else kf = sm(kf, otr); otr = -1; continue;}
        if (a[i] == ')')
        {
            if (num == -1) kf = sm(kf, otr);
              else val = sm(val, mult(otr, num));
            return {{val, kf}, i + 1};
        }
        {
            ll cur = 1, kft = 0, from = 1, dob = 0;
            if (num != -1)
                {
                    from = mult(from, num); cur = num;} else kft = 1;
            i++;
            while (1)
            {
                if (a[i] == '(')
                {
                    pair <pair <ll, ll>, int> pt = calc(i);
                    if (pt.F.S == 0) from = mult(from, pt.F.F);
                    if (pt.F.S != 0) kft = pt.F.S;
                    if (pt.F.F != 0) {if (kft == 0 || pt.F.S != 0) dob = 1; cur = mult(cur, pt.F.F);}
                    i = pt.S;
                    if (a[i] == '*') {i++; continue;}
                    kft = mult(kft, from);
                    kf = sm(kf, mult(kft, otr));
                    if (kft == 0) val = sm(val, mult(cur, otr));
                    else
                    val = sm(val, mult(dob, mult(cur, otr)));
                    if (a[i] == '+') otr = 1;
                    if (a[i] == '-') otr = -1;
                    if (a[i] == ')') return {{val, kf}, i + 1};
                    break;
                }
                ll num = 0, j = 0;
                while (i + j + 1 < sz(a) && gd(i + j + 1)) j++;
                for (int u = i; u <= i + j; u++) {num *= 10; num += a[u] - '0'; num = sm(num, 0);}
                if (j == 0 && a[i + j] == 'x') num = -1;
                if (num == -1)
                {
                    kft = 1;
                } else {cur = mult(cur, num); from = mult(from, num);}
                i += j + 1;
                if (a[i] == '*') {i++; continue;}
                kft = mult(kft, from);
                kf = sm(kf, mult(kft, otr));
                if (kft == 0) val = sm(val, mult(cur, otr));
                else
                val = sm(val, mult(dob, mult(cur, otr)));
                if (a[i] == '+') otr = 1;
                if (a[i] == '-') otr = -1;
                if (a[i] == ')') return {{val, kf}, i + 1};
                break;
            }
        }
    }
}
void gcd(ll a, ll b, ll &x, ll &y)
{
    if (b == 0)
    {
        x = 1;
        y = 0;
        return;
    }
    ll X, Y;
    gcd(b, a % b, X, Y);
    x = Y;
    y = (X - (a / b) * Y + m) % m;
}
int main()
{
    //freopen("input.txt", "r", stdin); //freopen("output4.txt", "w", stdout);
    ios_base::sync_with_stdio(0); istream::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin >> a;
    cin >> p >> m;
    a = '(' + a;
    a = a + ')';
    pair <pair <ll, ll>, int> pt = calc(0);
    ll val = pt.F.F, kf = pt.F.S;
   // cout << val << " " << kf << endl;
    p -= val;
    p = sm(p, m);
    ll gc = __gcd(m, kf);
    m /= gc;
    p /= gc;
    kf /= gc;
    ll x, y;
    gcd(kf, m, x, y);
    cout << mult(p, x) << endl;
}
컴파일 시 표준 에러 (stderr) 메시지
| # | Verdict | Execution time | Memory | Grader output | 
|---|---|---|---|---|
| Fetching results... | ||||
