#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 |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
44 ms |
2480 KB |
Output is correct |
3 |
Correct |
43 ms |
2648 KB |
Output is correct |
4 |
Correct |
42 ms |
2764 KB |
Output is correct |
5 |
Correct |
42 ms |
2468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
340 KB |
Output is correct |
2 |
Correct |
44 ms |
2528 KB |
Output is correct |
3 |
Correct |
45 ms |
2508 KB |
Output is correct |
4 |
Correct |
44 ms |
2512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
44 ms |
2480 KB |
Output is correct |
7 |
Correct |
43 ms |
2648 KB |
Output is correct |
8 |
Correct |
42 ms |
2764 KB |
Output is correct |
9 |
Correct |
42 ms |
2468 KB |
Output is correct |
10 |
Correct |
2 ms |
340 KB |
Output is correct |
11 |
Correct |
44 ms |
2528 KB |
Output is correct |
12 |
Correct |
45 ms |
2508 KB |
Output is correct |
13 |
Correct |
44 ms |
2512 KB |
Output is correct |
14 |
Correct |
2 ms |
340 KB |
Output is correct |
15 |
Correct |
43 ms |
2740 KB |
Output is correct |
16 |
Correct |
42 ms |
2768 KB |
Output is correct |
17 |
Correct |
44 ms |
2680 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
44 ms |
2480 KB |
Output is correct |
3 |
Correct |
43 ms |
2648 KB |
Output is correct |
4 |
Correct |
42 ms |
2764 KB |
Output is correct |
5 |
Correct |
42 ms |
2468 KB |
Output is correct |
6 |
Correct |
43 ms |
2852 KB |
Output is correct |
7 |
Correct |
43 ms |
2780 KB |
Output is correct |
8 |
Correct |
49 ms |
2796 KB |
Output is correct |
9 |
Correct |
44 ms |
2736 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
340 KB |
Output is correct |
2 |
Correct |
44 ms |
2528 KB |
Output is correct |
3 |
Correct |
45 ms |
2508 KB |
Output is correct |
4 |
Correct |
44 ms |
2512 KB |
Output is correct |
5 |
Correct |
43 ms |
3020 KB |
Output is correct |
6 |
Correct |
43 ms |
2980 KB |
Output is correct |
7 |
Correct |
50 ms |
3324 KB |
Output is correct |
8 |
Correct |
47 ms |
3176 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
44 ms |
2480 KB |
Output is correct |
7 |
Correct |
43 ms |
2648 KB |
Output is correct |
8 |
Correct |
42 ms |
2764 KB |
Output is correct |
9 |
Correct |
42 ms |
2468 KB |
Output is correct |
10 |
Correct |
2 ms |
340 KB |
Output is correct |
11 |
Correct |
44 ms |
2528 KB |
Output is correct |
12 |
Correct |
45 ms |
2508 KB |
Output is correct |
13 |
Correct |
44 ms |
2512 KB |
Output is correct |
14 |
Correct |
2 ms |
340 KB |
Output is correct |
15 |
Correct |
43 ms |
2740 KB |
Output is correct |
16 |
Correct |
42 ms |
2768 KB |
Output is correct |
17 |
Correct |
44 ms |
2680 KB |
Output is correct |
18 |
Correct |
43 ms |
2852 KB |
Output is correct |
19 |
Correct |
43 ms |
2780 KB |
Output is correct |
20 |
Correct |
49 ms |
2796 KB |
Output is correct |
21 |
Correct |
44 ms |
2736 KB |
Output is correct |
22 |
Correct |
43 ms |
3020 KB |
Output is correct |
23 |
Correct |
43 ms |
2980 KB |
Output is correct |
24 |
Correct |
50 ms |
3324 KB |
Output is correct |
25 |
Correct |
47 ms |
3176 KB |
Output is correct |
26 |
Correct |
45 ms |
2856 KB |
Output is correct |
27 |
Correct |
46 ms |
2872 KB |
Output is correct |
28 |
Correct |
43 ms |
2844 KB |
Output is correct |
29 |
Correct |
43 ms |
2768 KB |
Output is correct |
30 |
Correct |
46 ms |
2752 KB |
Output is correct |