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;
typedef long long llint;
const int maxn = 1010;
const int mod = 1e9+7;
int mul(int a, int b) {
llint out = (llint)a * b;
return out % mod;
}
int pot(int a, int b) {
if (b == 0) return 1;
if (b % 2 == 1) return pot(a, b - 1);
int out = pot(a, b / 2);
return mul(out, out);
}
int n;
int a[maxn], b[maxn];
int fac[maxn];
int dp[maxn][maxn];
int inv[maxn];
int len[maxn];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++)
scanf("%d%d", a+i, b+i);
vector< int > v;
for (int i = 1; i <= n; i++) {
v.push_back(a[i]);
v.push_back(b[i] + 1);
}
sort(v.begin(), v.end());
v.resize(unique(v.begin(), v.end()) - v.begin());
for (int i = 1; i <= n; i++) {
a[i] = upper_bound(v.begin(), v.end(), a[i]) - v.begin();
b[i] = upper_bound(v.begin(), v.end(), b[i]) - v.begin();
}
for (int i = 0; i < v.size() - 1; i++) len[i + 1] = v[i + 1] - v[i];
//for (int i = 0; i < v.size() - 1; i++) printf("%d ", len[i]); printf("\n\n");
for (int i = 1; i <= n; i++) inv[i] = pot(i, mod - 2);
for (int i = 0; i < v.size(); i++) dp[0][i] = 1;
for (int i = 1; i <= n; i++) {
for (int j = a[i]; j <= b[i]; j++) {
if (a[i] <= j && j <= b[i]) {
dp[i][j] = mul(len[j], dp[i - 1][j - 1]);
int kol = 1;
int pov = len[j] - 1;
for (int k = i - 1; k > 0; k--) {
if (a[k] <= j && j <= b[k]) {
pov = mul(pov, mul(len[j] - 1 + kol, inv[kol + 1])); kol++;
dp[i][j] += mul(dp[k - 1][j - 1], pov), dp[i][j] %= mod;
}
}
}
}
for (int j = 0; j < v.size(); j++) {
dp[i][j] += dp[i - 1][j], dp[i][j] %= mod;
if (j > 0) {
dp[i][j] += dp[i][j - 1], dp[i][j] %= mod;
dp[i][j] += mod - dp[i - 1][j - 1], dp[i][j] %= mod;
}
}
//for (int j = 0; j < v.size(); j++) printf("%d ", dp[i][j]);
//printf("\n");
}
printf("%d\n", (mod + dp[n][v.size() - 1] - 1) % mod);
return 0;
}
Compilation message (stderr)
boat.cpp: In function 'int main()':
boat.cpp:44:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
44 | for (int i = 0; i < v.size() - 1; i++) len[i + 1] = v[i + 1] - v[i];
| ~~^~~~~~~~~~~~~~
boat.cpp:48:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
48 | for (int i = 0; i < v.size(); i++) dp[0][i] = 1;
| ~~^~~~~~~~~~
boat.cpp:63:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
63 | for (int j = 0; j < v.size(); j++) {
| ~~^~~~~~~~~~
boat.cpp:30:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
30 | scanf("%d", &n);
| ~~~~~^~~~~~~~~~
boat.cpp:32:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
32 | scanf("%d%d", a+i, b+i);
| ~~~~~^~~~~~~~~~~~~~~~~~
# | 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... |