This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define NRM 1000002
#define ll long long
using namespace std;
const ll MOD = 1e9+7;
ll pwr (ll a, ll b)
{
if(b == 0)
return 1;
ll p = pwr(a, b / 2);
if(b & 1)
return p * p % MOD * a % MOD;
return p * p % MOD;
}
ll inv (ll x)
{
return pwr(x, MOD - 2);
}
int q, n;
ll vf[NRM];
ll nr, sum;
ll perm = 1;
ll fact[NRM], invfact[NRM];
ll comb(ll a, ll b)
{
return fact[a] * invfact[b] % MOD * invfact[a - b] % MOD;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> q >> n;
fact[0] = 1;
for(int i = 1; i <= 1e6; i++)
fact[i] = fact[i - 1] * i % MOD;
for(int i = 0; i <= 1e6; i++)
invfact[i] = pwr(fact[i], MOD - 2);
while(q--)
{
int op, val;
cin >> op >> val;
if(op == 0)
{
vf[val]++;
nr++;
perm = perm * nr % MOD * inv(vf[val]) % MOD;
sum += val;
ll ans;
if(sum > n)
ans = 0;
else
ans = perm * comb(n - sum + nr, n - sum) % MOD;
if(ans == 0)
cout << "-1\n";
else
cout << ans << "\n";
}
else
{
perm = perm * inv(nr) % MOD * vf[val] % MOD;
vf[val]--;
nr--;
sum -= val;
ll ans;
if(sum > n)
ans = 0;
else
ans = perm * comb(n - sum + nr, n - sum) % MOD;
if(ans == 0)
cout << "-1\n";
else
cout << ans << "\n";
}
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |