#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <set>
#include <bitset>
#include <string>
#include <unordered_map>
#pragma optimize("O2")
#define mod 1000000007
using namespace std;
long long powmod(long long a, long long b, long long p) {
if (b == 0) {
return 1;
}
if (b % 2 == 1) {
return (a * powmod(a, b - 1, p)) % p;
}
long long P = powmod(a, b / 2, p);
return (P * P) % p;
}
long long inv(long long a, long long p) {
return powmod(a, p - 2, p);
}
vector<long long> fact;
long long combs(long long n, long long k, long long p) {
return ((fact[n] * inv(fact[k], p) % p) * inv(fact[n - k] , p)) % p;
}
long long stars_and_bars(long long n, long long k, long long p) {
return combs(n + k - 1, n, p);
}
void precompute(long long n , long long p) {
fact = vector<long long>(n + 1);
fact[0] = 1;
for (int i = 1; i <= n; ++i) {
fact[i] = fact[i - 1] * i;
fact[i] %= p;
}
}
int main() {
int q, n;
cin >> q >> n;
precompute(n, mod);
vector<int> f(1000001);
long long sum = 0;
long long perms = 1;
long long elem = 0;
while (q--) {
int t, x;
cin >> t >> x;
perms *= inv(fact[elem], mod);
perms %= mod;
if (t == 1) {
perms *= fact[f[x]];
perms %= mod;
f[x]--;
perms *= inv(fact[f[x]], mod);
perms %= mod;
elem--;
sum -= x;
}
else {
perms *= fact[f[x]];
perms %= mod;
f[x]++;
perms *= inv(fact[f[x]], mod);
perms %= mod;
sum += x;
elem++;
}
perms *= fact[elem];
perms %= mod;
if (sum > n) {
cout << -1 << '\n';
continue;
}
cout << (perms * stars_and_bars(n - sum, elem + 1, mod)) % mod << '\n';
}
}
Compilation message
thegrade.cpp:9: warning: ignoring #pragma optimize [-Wunknown-pragmas]
9 | #pragma optimize("O2")
|
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4172 KB |
Output is correct |
2 |
Correct |
3 ms |
4172 KB |
Output is correct |
3 |
Correct |
3 ms |
4172 KB |
Output is correct |
4 |
Correct |
3 ms |
4172 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
4276 KB |
Output is correct |
2 |
Correct |
405 ms |
5380 KB |
Output is correct |
3 |
Correct |
448 ms |
5784 KB |
Output is correct |
4 |
Correct |
413 ms |
5600 KB |
Output is correct |
5 |
Correct |
434 ms |
5408 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
4176 KB |
Output is correct |
2 |
Correct |
374 ms |
5656 KB |
Output is correct |
3 |
Correct |
376 ms |
5600 KB |
Output is correct |
4 |
Correct |
383 ms |
5752 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4172 KB |
Output is correct |
2 |
Correct |
3 ms |
4172 KB |
Output is correct |
3 |
Correct |
3 ms |
4172 KB |
Output is correct |
4 |
Correct |
3 ms |
4172 KB |
Output is correct |
5 |
Correct |
21 ms |
4276 KB |
Output is correct |
6 |
Correct |
405 ms |
5380 KB |
Output is correct |
7 |
Correct |
448 ms |
5784 KB |
Output is correct |
8 |
Correct |
413 ms |
5600 KB |
Output is correct |
9 |
Correct |
434 ms |
5408 KB |
Output is correct |
10 |
Correct |
14 ms |
4176 KB |
Output is correct |
11 |
Correct |
374 ms |
5656 KB |
Output is correct |
12 |
Correct |
376 ms |
5600 KB |
Output is correct |
13 |
Correct |
383 ms |
5752 KB |
Output is correct |
14 |
Correct |
19 ms |
4300 KB |
Output is correct |
15 |
Correct |
432 ms |
5556 KB |
Output is correct |
16 |
Correct |
403 ms |
5644 KB |
Output is correct |
17 |
Correct |
404 ms |
5544 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
4276 KB |
Output is correct |
2 |
Correct |
405 ms |
5380 KB |
Output is correct |
3 |
Correct |
448 ms |
5784 KB |
Output is correct |
4 |
Correct |
413 ms |
5600 KB |
Output is correct |
5 |
Correct |
434 ms |
5408 KB |
Output is correct |
6 |
Correct |
433 ms |
6532 KB |
Output is correct |
7 |
Correct |
436 ms |
6476 KB |
Output is correct |
8 |
Correct |
455 ms |
6468 KB |
Output is correct |
9 |
Correct |
451 ms |
6432 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
4176 KB |
Output is correct |
2 |
Correct |
374 ms |
5656 KB |
Output is correct |
3 |
Correct |
376 ms |
5600 KB |
Output is correct |
4 |
Correct |
383 ms |
5752 KB |
Output is correct |
5 |
Correct |
342 ms |
6324 KB |
Output is correct |
6 |
Correct |
367 ms |
6344 KB |
Output is correct |
7 |
Correct |
381 ms |
6572 KB |
Output is correct |
8 |
Correct |
336 ms |
6300 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4172 KB |
Output is correct |
2 |
Correct |
3 ms |
4172 KB |
Output is correct |
3 |
Correct |
3 ms |
4172 KB |
Output is correct |
4 |
Correct |
3 ms |
4172 KB |
Output is correct |
5 |
Correct |
21 ms |
4276 KB |
Output is correct |
6 |
Correct |
405 ms |
5380 KB |
Output is correct |
7 |
Correct |
448 ms |
5784 KB |
Output is correct |
8 |
Correct |
413 ms |
5600 KB |
Output is correct |
9 |
Correct |
434 ms |
5408 KB |
Output is correct |
10 |
Correct |
14 ms |
4176 KB |
Output is correct |
11 |
Correct |
374 ms |
5656 KB |
Output is correct |
12 |
Correct |
376 ms |
5600 KB |
Output is correct |
13 |
Correct |
383 ms |
5752 KB |
Output is correct |
14 |
Correct |
19 ms |
4300 KB |
Output is correct |
15 |
Correct |
432 ms |
5556 KB |
Output is correct |
16 |
Correct |
403 ms |
5644 KB |
Output is correct |
17 |
Correct |
404 ms |
5544 KB |
Output is correct |
18 |
Correct |
433 ms |
6532 KB |
Output is correct |
19 |
Correct |
436 ms |
6476 KB |
Output is correct |
20 |
Correct |
455 ms |
6468 KB |
Output is correct |
21 |
Correct |
451 ms |
6432 KB |
Output is correct |
22 |
Correct |
342 ms |
6324 KB |
Output is correct |
23 |
Correct |
367 ms |
6344 KB |
Output is correct |
24 |
Correct |
381 ms |
6572 KB |
Output is correct |
25 |
Correct |
336 ms |
6300 KB |
Output is correct |
26 |
Correct |
429 ms |
6468 KB |
Output is correct |
27 |
Correct |
445 ms |
6420 KB |
Output is correct |
28 |
Correct |
444 ms |
6468 KB |
Output is correct |
29 |
Correct |
424 ms |
6420 KB |
Output is correct |
30 |
Correct |
428 ms |
6408 KB |
Output is correct |