Submission #687700

# Submission time Handle Problem Language Result Execution time Memory
687700 2023-01-26T21:23:16 Z divad The grade (info1cup18_thegrade) C++14
100 / 100
48 ms 4196 KB
#include <bits/stdc++.h>
#define int long long
#define NMAX 1000002
#define FMAX 100002
#define MOD  1000000007
using namespace std;
int fr[NMAX],fact[FMAX],invfact[FMAX],nrperm;
int q,p,t,x,n,sum;

int lgput(int n, int a){
    if(a == 0){
        return 1;
    }else{
        if(a%2 == 0){
            int val = lgput(n, a/2);
            return (val*val)%MOD;
        }else{
            return (n*lgput(n, a-1))%MOD;
        }
    }
}

int comb(int n, int m){
    return ((fact[n]*invfact[m])%MOD*invfact[n-m])%MOD;
}

int stars_and_bars(int n, int k){
    return comb(n+k-1, k-1);
}

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cin >> q >> p;
    fact[0] = 1;
    invfact[0] = lgput(fact[0], MOD-2);
    for(int i = 1; i <= q; i++){
        fact[i] = fact[i-1]*i;
        fact[i] %= MOD;
        invfact[i] = lgput(fact[i], MOD-2);
    }
    nrperm = 1;
    while(q--){
        cin >> t >> x;
        nrperm *= invfact[n];
        nrperm %= MOD;
        if(t == 0){
            nrperm *= fact[fr[x]];
            nrperm %= MOD;
            n++;
            sum += x;
            fr[x]++;
            nrperm *= invfact[fr[x]];
            nrperm %= MOD;
        }else{
            nrperm *= fact[fr[x]];
            nrperm %= MOD;
            n--;
            sum -= x;
            fr[x]--;
            nrperm *= invfact[fr[x]];
            nrperm %= MOD;
        }
        nrperm *= fact[n];
        nrperm %= MOD;
        if(sum > p){
            /// nu putem sa le distantam
            cout << "-1\n";
            continue;
        }
        /// p-sum = cu cate ne putem juca
        /// nrperm = nr de permutari distincte din A
        cout << (nrperm * stars_and_bars(p-sum, n+1))%MOD << "\n";
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 43 ms 2888 KB Output is correct
3 Correct 44 ms 3404 KB Output is correct
4 Correct 48 ms 3276 KB Output is correct
5 Correct 48 ms 3096 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 340 KB Output is correct
2 Correct 45 ms 3324 KB Output is correct
3 Correct 45 ms 3276 KB Output is correct
4 Correct 48 ms 3304 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 43 ms 2888 KB Output is correct
7 Correct 44 ms 3404 KB Output is correct
8 Correct 48 ms 3276 KB Output is correct
9 Correct 48 ms 3096 KB Output is correct
10 Correct 2 ms 340 KB Output is correct
11 Correct 45 ms 3324 KB Output is correct
12 Correct 45 ms 3276 KB Output is correct
13 Correct 48 ms 3304 KB Output is correct
14 Correct 2 ms 340 KB Output is correct
15 Correct 43 ms 3148 KB Output is correct
16 Correct 44 ms 3148 KB Output is correct
17 Correct 43 ms 3188 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 43 ms 2888 KB Output is correct
3 Correct 44 ms 3404 KB Output is correct
4 Correct 48 ms 3276 KB Output is correct
5 Correct 48 ms 3096 KB Output is correct
6 Correct 44 ms 3368 KB Output is correct
7 Correct 47 ms 3276 KB Output is correct
8 Correct 47 ms 3344 KB Output is correct
9 Correct 44 ms 3276 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 340 KB Output is correct
2 Correct 45 ms 3324 KB Output is correct
3 Correct 45 ms 3276 KB Output is correct
4 Correct 48 ms 3304 KB Output is correct
5 Correct 42 ms 3804 KB Output is correct
6 Correct 44 ms 3920 KB Output is correct
7 Correct 47 ms 4196 KB Output is correct
8 Correct 43 ms 3804 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 43 ms 2888 KB Output is correct
7 Correct 44 ms 3404 KB Output is correct
8 Correct 48 ms 3276 KB Output is correct
9 Correct 48 ms 3096 KB Output is correct
10 Correct 2 ms 340 KB Output is correct
11 Correct 45 ms 3324 KB Output is correct
12 Correct 45 ms 3276 KB Output is correct
13 Correct 48 ms 3304 KB Output is correct
14 Correct 2 ms 340 KB Output is correct
15 Correct 43 ms 3148 KB Output is correct
16 Correct 44 ms 3148 KB Output is correct
17 Correct 43 ms 3188 KB Output is correct
18 Correct 44 ms 3368 KB Output is correct
19 Correct 47 ms 3276 KB Output is correct
20 Correct 47 ms 3344 KB Output is correct
21 Correct 44 ms 3276 KB Output is correct
22 Correct 42 ms 3804 KB Output is correct
23 Correct 44 ms 3920 KB Output is correct
24 Correct 47 ms 4196 KB Output is correct
25 Correct 43 ms 3804 KB Output is correct
26 Correct 44 ms 3324 KB Output is correct
27 Correct 44 ms 3388 KB Output is correct
28 Correct 48 ms 3364 KB Output is correct
29 Correct 44 ms 3384 KB Output is correct
30 Correct 44 ms 3292 KB Output is correct