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>
using namespace std;
const int N = 505, mod = 1e9 + 7;
int n, m, a[N], b[N], cnt[N * 2], dp[N * 2][N], inv[N];
vector<int> v, f;
signed main(){
cin.tie(0), cout.tie(0) -> sync_with_stdio(0);
cin >> n;
for (int i = 1; i <= n; i++)
cin >> a[i] >> b[i], b[i] ++;
inv[1] = 1;
for (int i = 2; i <= n; i++)
inv[i] = mod - 1ll * (mod / i) * inv[mod % i] % mod;
for (int i = 1; i <= n; i++)
v.push_back(a[i]), v.push_back(b[i]);
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
f.resize(m = v.size() - 1);
for (int i = 1; i < (int) v.size(); i++)
f[i - 1] = v[i] - v[i - 1];
for (int i = 1; i <= n; i++) {
int l = lower_bound(v.begin(), v.end(), a[i]) - v.begin(), r = lower_bound(v.begin(), v.end(), b[i]) - v.begin(), cur = 1;
for (int j = 0; j < l; j++) {
for (int k = cnt[j]; k; k--)
if ((cur += dp[j][k]) >= mod)
cur -= mod;
}
for (int j = l; j < r; j++) {
int nxt = cur;
for (int k = cnt[j]; k; k--) {
if ((nxt += dp[j][k]) >= mod)
nxt -= mod;
dp[j][k + 1] += 1ll * dp[j][k] * (f[j] - k) % mod * inv[k + 1] % mod;
if (dp[j][k + 1] >= mod) dp[j][k + 1] -= mod;
}
++ cnt[j];
dp[j][1] += 1ll * cur * f[j] % mod;
if (dp[j][1] >= mod) dp[j][1] -= mod;
swap(nxt, cur);
}
}
int res = 0;
for (int j = 0; j < m; j++)
for (int k = 1; k <= cnt[j]; k++)
if ((res += dp[j][k]) >= mod)
res -= mod;
cout << res << '\n';
}
# | 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... |