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;
#define dbg(x) cerr << #x << ": " << x << endl;
const int MOD = 1e9 + 7;
int add(int a, int b) {
int res = a + b;
if (res >= MOD) res -= MOD;
return res;
}
int mul(long long a, int b) {
return a * b % MOD;
}
int binpow(int base, int power) {
int res = 1;
while (power > 0) {
if (power & 1) {
res = mul(res, base);
}
base = mul(base, base);
power >>= 1;
}
return res;
}
const int MAX_N = 500 + 5;
int dp[MAX_N][MAX_N * 2];
int c[MAX_N * 2][MAX_N];
int a[MAX_N];
int b[MAX_N];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<int> p;
for (int i = 1; i <= n; ++i) {
cin >> a[i] >> b[i];
p.push_back(a[i]);
p.push_back(b[i] + 1);
}
sort(p.begin(), p.end());
p.resize(unique(p.begin(), p.end()) - p.begin());
vector<pair<int, int>> segments;
for (int i = 0; i < (int)p.size() - 1; ++i) {
for (int j = 1; j <= n; ++j) {
if (a[j] <= p[i] && p[i + 1] - 1 <= b[j]) {
segments.emplace_back(p[i], p[i + 1] - 1);
break;
}
}
}
int m = segments.size();
segments.insert(segments.begin(), {-1, -1});
for (int i = 1; i <= m; ++i) {
auto [l, r] = segments[i];
c[i][1] = r - l + 1;
for (int j = 2; j <= n; ++j) {
c[i][j] = mul(c[i][j - 1], mul(r - l + j, binpow(j, MOD - 2)));
}
}
for (int j = 0; j <= m; ++j) {
dp[0][j] = 1;
}
for (int i = 1; i <= n; ++i) {
dp[i][0] = 1;
for (int j = 1; j <= m; ++j) {
auto [l, r] = segments[j];
dp[i][j] = dp[i][j - 1];
int cnt = 0;
for (int k = i; k >= 1; --k) {
if (a[k] <= l && r <= b[k]) {
++cnt;
dp[i][j] = add(dp[i][j], mul(dp[k - 1][j - 1], c[j][cnt]));
}
}
}
}
cout << dp[n][m] - 1 << '\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... |