Submission #256487

# Submission time Handle Problem Language Result Execution time Memory
256487 2020-08-02T18:29:13 Z Vimmer Slon (COCI15_slon) C++14
0 / 120
3 ms 640 KB
#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;}
ll mult(ll x, ll y) {return (x * y) % m;}

bool gd(ll x) {return a[x] != '+' && a[x] != '-' && a[x] != '*' && a[x] != '(' && a[x] != ')';}

ll get_num(ll &i)
{
    ll num = 0, j = 0;

    while (i + j + 1 < sz(a) && gd(i + j + 1)) j++;

    for (ll 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;

    return num;
}

pair <pair <ll, ll>, ll>  calc(ll v);

pair <ll, ll> get_mult(ll cur, ll kft, ll dob, ll &i)
{
    for(;; i++)
    {
        if (a[i] == '(')
        {
            pair <pair <ll, ll>, ll> pt = calc(i);

            if (pt.F.S != 0) {kft = mult(pt.F.S, cur); if (pt.F.F != 0) dob = pt.F.F;}
            else
            if (pt.F.F != 0) {kft = mult(kft, pt.F.F); cur = mult(cur, pt.F.F);}

            i = pt.S;
        }
        else
        {
            ll num = get_num(i);

            if (num == -1) kft = cur; else {cur = mult(cur, num); kft = mult(kft, num);}
        }

        if (a[i] == '*') continue;

        ll sumr = 0;

        if (kft == 0) dob = 1;

        sumr = sm(sumr, mult(dob, cur));

        return {sumr, kft};
    }
}

pair <pair <ll, ll>, ll>  calc(ll v)
{
    ll kf = 0, val = 0, otr = 1;

    for (ll i = v + 1; i < sz(a); i++)
    {
        ll cur = 1, kft = 0, dob = 0;

        if (a[i] == ')') return {{val, kf}, i + 1};

        if (a[i] == '+') {otr = 1; continue;}

        if (a[i] == '-') {otr = -1; continue;}

        if (a[i] == '(')
        {
            pair <pair <ll, ll>, ll> 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 { if (pt.F.S != 0) {dob = pt.F.F; kft = pt.F.S;} else cur = max(cur, pt.F.F);}
        }
        else
        {
            ll num = get_num(i);

            if (a[i] != '*')
            {
                if (num != -1) val = sm(val, mult(otr, num)); else kf = sm(kf, otr);

                i--;

                continue;
            }

            if (num == -1) kft = 1; else cur = num;
        }

        i++;

        pair <ll, ll> tr = get_mult(cur, kft, dob, i);

        kf = sm(kf, mult(otr, tr.S));

        val = sm(val, mult(otr, tr.F));

        i--;
    }
}

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>, ll> 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 << (ll(1e9) * m + sm(0, mult(p, x))) % m << endl;
}

Compilation message

slon.cpp: In function 'std::pair<std::pair<long long int, long long int>, long long int> calc(ll)':
slon.cpp:149:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^
slon.cpp: In function 'int main()':
slon.cpp:173:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
     freopen("input.txt", "r", stdin); //freopen("output4.txt", "w", stdout);
     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Runtime error 3 ms 640 KB Execution killed with signal 8 (could be triggered by violating memory limits)
2 Runtime error 2 ms 640 KB Execution killed with signal 8 (could be triggered by violating memory limits)
3 Runtime error 3 ms 640 KB Execution killed with signal 8 (could be triggered by violating memory limits)
4 Runtime error 2 ms 640 KB Execution killed with signal 8 (could be triggered by violating memory limits)
5 Runtime error 2 ms 640 KB Execution killed with signal 8 (could be triggered by violating memory limits)
6 Runtime error 3 ms 640 KB Execution killed with signal 8 (could be triggered by violating memory limits)
7 Runtime error 2 ms 640 KB Execution killed with signal 8 (could be triggered by violating memory limits)
8 Runtime error 3 ms 640 KB Execution killed with signal 8 (could be triggered by violating memory limits)
9 Runtime error 3 ms 640 KB Execution killed with signal 8 (could be triggered by violating memory limits)
10 Runtime error 3 ms 640 KB Execution killed with signal 8 (could be triggered by violating memory limits)