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 maxn = 505;
const long long mod = 1e9 + 7;
int n;
int a[maxn], b[maxn], has[maxn];
long long sum[maxn], g[maxn], p[maxn], dp[maxn], ivp[maxn];
long long f[2 * maxn][maxn]; // segment i, last boat: j
vector<int> c;
vector<vector<int>> adj(2 * maxn);
long long pow_(long long o, long long q) {
if (!q) return 1;
long long t = pow_(o, q / 2);
t = (t * t) % mod;
if (q % 2) t = (t * o) % mod;
return t;
}
void load() {
p[0] = 1;
for (int i = 1; i <= n; i++) p[i] = (p[i - 1] * i) % mod;
for (int i = 0; i <= n; i++) ivp[i] = pow_(p[i], mod - 2);
for (int i = 1; i <= n; i++) {
int l = lower_bound(c.begin(), c.end(), a[i] - 1) - c.begin();
int r = lower_bound(c.begin(), c.end(), b[i]) - c.begin();
for (int j = l; j < r; j++) adj[j].push_back(i);
}
}
long long C(int k, int n_) {
if (!k || k == n_) return 1;
if (k > n) return 0;
return p[n_] * (ivp[k] * ivp[n_ - k] % mod) % mod;
}
void solve() {
sum[0] = 1;
for (int i = 0; i < (int)c.size(); i++) {
int sz = (int)adj[i].size();
int value = c[i + 1] - c[i];
for (int j = 1; j <= sz; j++) {
dp[j] = 0;
long long res = 1;
for (int z = 0; z < j; z++) {
res = (res * (value - z)) % mod;
if (z < j - 1) dp[j] = (dp[j] + C(z, j - 1) * g[z + 1]) % mod;
}
g[j] = (res * ivp[j]) % mod;
if (j > value) g[j] = 0;
dp[j] = (dp[j] + g[j]) % mod;
}
for (int j = 1; j <= sz; j++) g[j] = 0;
for (auto j: adj[i]) {
has[j]++;
int cnt = 1;
for (int z = j - 1; z >= 0; z--) {
f[i][j] = (f[i][j] + sum[z] * dp[cnt]) % mod;
if (has[z]) cnt++;
}
}
for (auto j: adj[i]) sum[j] = (sum[j] + f[i][j]) % mod, has[j]--;
}
long long ans = 0;
for (int i = 1; i <= n; i++) ans = (ans + sum[i]) % mod;
cout << ans << '\n';
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i] >> b[i];
c.push_back(a[i] - 1);
c.push_back(b[i]);
}
sort(c.begin(), c.end());
c.resize(unique(c.begin(), c.end()) - c.begin());
load();
solve();
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... |