답안 #45121

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
45121 2018-04-11T10:15:28 Z model_code The grade (info1cup18_thegrade) C++17
100 / 100
319 ms 4908 KB
#include <iostream>
#include <algorithm>
#include <cassert>

#define MOD 1000000007LL

using namespace std;

long long fact[1000010], inv[1000010];
int frq[1000010];

long long put (long long n, long long p)
{
    long long rez = 1LL;
    for (long long i = 0LL; (1LL << i) <= p; i += 1LL)
    {
        if ((1LL << i) & p) rez *= n, rez %= MOD;

        n *= n;
        n %= MOD;
    }

    return rez;
}

int main ()
{
   // freopen ("input", "r", stdin);
    //freopen ("output4", "w", stdout);

    int q, p;
    cin >> q >> p;

    assert (1 <= q && q <= 1000000);
    assert (1 <= p && p <= 100000);

    fact[0] = inv[0] = 1LL;
    for (int i = 1; i <= max (2 * p, q); ++i)
    {
        fact[i] = 1LL * i * fact[i - 1];
        fact[i] %= MOD;

        inv[i] = put (1LL * fact[i], MOD - 2LL);
    }

    int n = 0;
    long long sum = 0LL, perm = 1LL;
    for (; q; --q)
    {
        int op, x;
        cin >> op >> x;

        if (!op)
        {
            ++n;
            sum += 1LL * x;

            perm *= 1LL * n;
            perm %= MOD;

            if (perm == 0LL)
            {
                //fprintf (stderr, "1");
                return 0;
            }

            perm *= fact[frq[x]];
            perm %= MOD;

            if (perm == 0LL)
            {
                //fprintf (stderr, "2");
                return 0;
            }

            ++frq[x];
            perm *= inv[frq[x]];
            perm %= MOD;

            if (perm == 0LL)
            {
                //fprintf (stderr, "3");
                return 0;
            }
        }

        else
        {
            perm *= inv[n];
            perm %= MOD;

            if (perm == 0LL)
            {
                //fprintf (stderr, "4");
                return 0;
            }

            --n;
            sum -= 1LL * x;

            perm *= fact[n];
            perm %= MOD;
            if (perm == 0LL)
            {
                //fprintf (stderr, "5");
                return 0;
            }

            perm *= fact[frq[x]];
            perm %= MOD;
            if (perm == 0LL)
            {
                //fprintf (stderr, "6");
                return 0;
            }

            --frq[x];
            perm *= inv[frq[x]];
            perm %= MOD;
            if (perm == 0LL)
            {
                //fprintf (stderr, "%d", q);
                return 0;
            }
        }

        if (sum > 1LL * p)
        {
            cout << "-1\n";
            continue;
        }

        long long aux = 1LL * (p + n) - sum;

        long long rez = fact[aux] * inv[n];
        rez %= MOD;

        rez *= inv[aux - n];
        rez %= MOD;

        rez *= perm;
        rez %= MOD;

        cout << rez << '\n';
    }

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 456 KB Output is correct
3 Correct 2 ms 532 KB Output is correct
4 Correct 2 ms 532 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 628 KB Output is correct
2 Correct 271 ms 2864 KB Output is correct
3 Correct 318 ms 2936 KB Output is correct
4 Correct 233 ms 3044 KB Output is correct
5 Correct 227 ms 3044 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 3044 KB Output is correct
2 Correct 319 ms 3044 KB Output is correct
3 Correct 256 ms 3044 KB Output is correct
4 Correct 258 ms 3044 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 456 KB Output is correct
3 Correct 2 ms 532 KB Output is correct
4 Correct 2 ms 532 KB Output is correct
5 Correct 6 ms 628 KB Output is correct
6 Correct 271 ms 2864 KB Output is correct
7 Correct 318 ms 2936 KB Output is correct
8 Correct 233 ms 3044 KB Output is correct
9 Correct 227 ms 3044 KB Output is correct
10 Correct 9 ms 3044 KB Output is correct
11 Correct 319 ms 3044 KB Output is correct
12 Correct 256 ms 3044 KB Output is correct
13 Correct 258 ms 3044 KB Output is correct
14 Correct 10 ms 3044 KB Output is correct
15 Correct 294 ms 3176 KB Output is correct
16 Correct 248 ms 3244 KB Output is correct
17 Correct 237 ms 3244 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 628 KB Output is correct
2 Correct 271 ms 2864 KB Output is correct
3 Correct 318 ms 2936 KB Output is correct
4 Correct 233 ms 3044 KB Output is correct
5 Correct 227 ms 3044 KB Output is correct
6 Correct 251 ms 4908 KB Output is correct
7 Correct 245 ms 4908 KB Output is correct
8 Correct 257 ms 4908 KB Output is correct
9 Correct 286 ms 4908 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 3044 KB Output is correct
2 Correct 319 ms 3044 KB Output is correct
3 Correct 256 ms 3044 KB Output is correct
4 Correct 258 ms 3044 KB Output is correct
5 Correct 279 ms 4908 KB Output is correct
6 Correct 287 ms 4908 KB Output is correct
7 Correct 277 ms 4908 KB Output is correct
8 Correct 245 ms 4908 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 456 KB Output is correct
3 Correct 2 ms 532 KB Output is correct
4 Correct 2 ms 532 KB Output is correct
5 Correct 6 ms 628 KB Output is correct
6 Correct 271 ms 2864 KB Output is correct
7 Correct 318 ms 2936 KB Output is correct
8 Correct 233 ms 3044 KB Output is correct
9 Correct 227 ms 3044 KB Output is correct
10 Correct 9 ms 3044 KB Output is correct
11 Correct 319 ms 3044 KB Output is correct
12 Correct 256 ms 3044 KB Output is correct
13 Correct 258 ms 3044 KB Output is correct
14 Correct 10 ms 3044 KB Output is correct
15 Correct 294 ms 3176 KB Output is correct
16 Correct 248 ms 3244 KB Output is correct
17 Correct 237 ms 3244 KB Output is correct
18 Correct 251 ms 4908 KB Output is correct
19 Correct 245 ms 4908 KB Output is correct
20 Correct 257 ms 4908 KB Output is correct
21 Correct 286 ms 4908 KB Output is correct
22 Correct 279 ms 4908 KB Output is correct
23 Correct 287 ms 4908 KB Output is correct
24 Correct 277 ms 4908 KB Output is correct
25 Correct 245 ms 4908 KB Output is correct
26 Correct 239 ms 4908 KB Output is correct
27 Correct 246 ms 4908 KB Output is correct
28 Correct 230 ms 4908 KB Output is correct
29 Correct 243 ms 4908 KB Output is correct
30 Correct 286 ms 4908 KB Output is correct