#include <bits/stdc++.h>
#define lsb(x) (x & (-x))
#define ll long long
using namespace std;
const int MOD = (int) 1e9 + 7;
const int MAXN = 500;
inline int lgput(int a, int b) {
int ans = 1;
while(b > 0) {
if(b & 1)
ans = (1LL * ans * a) % MOD;
b >>= 1;
a = (1LL * a * a) % MOD;
}
return ans;
}
inline void mod(int &x) {
if(x >= MOD)
x -= MOD;
}
int dp[MAXN + 1][2 * MAXN + 1];
int main() {
//ifstream cin("C.in");
//ofstream cout("C.out");
int i, j, n;
ios::sync_with_stdio(false);
cin >> n;
vector <int> a(n + 1), b(n + 1);
vector <int> nrm;
for(i = 1; i <= n; i++) {
cin >> a[i] >> b[i];
b[i]++;
nrm.push_back(a[i]);
nrm.push_back(b[i]);
}
sort(nrm.begin(), nrm.end());
nrm.resize(unique(nrm.begin(), nrm.end()) - nrm.begin());
for(i = 1; i <= n; i++) {
a[i] = lower_bound(nrm.begin(), nrm.end(), a[i]) - nrm.begin();
b[i] = lower_bound(nrm.begin(), nrm.end(), b[i]) - nrm.begin();
}
vector <int> inv(n + 1);
for(i = 1; i <= n; i++) {
inv[i] = lgput(i, MOD - 2);
}
vector <int> len(2 * n);
for(i = 0; i < nrm.size(); i++) {
if(i + 1 < nrm.size()) {
len[i] = nrm[i + 1] - nrm[i];
}
dp[0][i] = 1;
}
for(i = 1; i <= n; i++) {
dp[i][0] = 1;
for(j = a[i] + 1; j <= b[i]; j++) {
dp[i][j] = (1LL * dp[i - 1][j - 1] * len[j - 1]) % MOD;
int coef = len[j - 1];
int nr = 1;
for(int p = i - 2; p >= 0; p--) {
if(a[p + 1] < j && j <= b[p + 1]) {
nr++;
if(nr > len[j - 1]) {
break;
}
// comb(len + nr - 2, nr)
coef = (1LL * coef * (len[j - 1] + nr - 2)) % MOD;
coef = (1LL * coef * inv[nr]) % MOD;
dp[i][j] = (dp[i][j] + 1LL * coef * dp[p][j - 1]) % MOD;
}
}
}
for(j = 1; j < nrm.size(); j++) {
dp[i][j] = (1LL * dp[i][j] + dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1] + MOD) % MOD;
}
}
cout << (dp[n][nrm.size() - 1] - 1 + MOD) % MOD;
//cin.close();
//cout.close();
return 0;
}
Compilation message
boat.cpp: In function 'int main()':
boat.cpp:53:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(i = 0; i < nrm.size(); i++) {
~~^~~~~~~~~~~~
boat.cpp:54:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(i + 1 < nrm.size()) {
~~~~~~^~~~~~~~~~~~
boat.cpp:78:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(j = 1; j < nrm.size(); j++) {
~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
2296 KB |
Output is correct |
2 |
Correct |
9 ms |
2304 KB |
Output is correct |
3 |
Correct |
9 ms |
2496 KB |
Output is correct |
4 |
Correct |
9 ms |
2704 KB |
Output is correct |
5 |
Correct |
10 ms |
2704 KB |
Output is correct |
6 |
Correct |
8 ms |
2708 KB |
Output is correct |
7 |
Correct |
8 ms |
2708 KB |
Output is correct |
8 |
Correct |
8 ms |
2716 KB |
Output is correct |
9 |
Correct |
8 ms |
2728 KB |
Output is correct |
10 |
Correct |
9 ms |
2792 KB |
Output is correct |
11 |
Correct |
8 ms |
2904 KB |
Output is correct |
12 |
Correct |
9 ms |
2904 KB |
Output is correct |
13 |
Correct |
8 ms |
2904 KB |
Output is correct |
14 |
Correct |
8 ms |
2904 KB |
Output is correct |
15 |
Correct |
8 ms |
2904 KB |
Output is correct |
16 |
Correct |
5 ms |
2904 KB |
Output is correct |
17 |
Correct |
5 ms |
2912 KB |
Output is correct |
18 |
Correct |
5 ms |
2928 KB |
Output is correct |
19 |
Correct |
6 ms |
2972 KB |
Output is correct |
20 |
Correct |
4 ms |
2972 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
2296 KB |
Output is correct |
2 |
Correct |
9 ms |
2304 KB |
Output is correct |
3 |
Correct |
9 ms |
2496 KB |
Output is correct |
4 |
Correct |
9 ms |
2704 KB |
Output is correct |
5 |
Correct |
10 ms |
2704 KB |
Output is correct |
6 |
Correct |
8 ms |
2708 KB |
Output is correct |
7 |
Correct |
8 ms |
2708 KB |
Output is correct |
8 |
Correct |
8 ms |
2716 KB |
Output is correct |
9 |
Correct |
8 ms |
2728 KB |
Output is correct |
10 |
Correct |
9 ms |
2792 KB |
Output is correct |
11 |
Correct |
8 ms |
2904 KB |
Output is correct |
12 |
Correct |
9 ms |
2904 KB |
Output is correct |
13 |
Correct |
8 ms |
2904 KB |
Output is correct |
14 |
Correct |
8 ms |
2904 KB |
Output is correct |
15 |
Correct |
8 ms |
2904 KB |
Output is correct |
16 |
Correct |
5 ms |
2904 KB |
Output is correct |
17 |
Correct |
5 ms |
2912 KB |
Output is correct |
18 |
Correct |
5 ms |
2928 KB |
Output is correct |
19 |
Correct |
6 ms |
2972 KB |
Output is correct |
20 |
Correct |
4 ms |
2972 KB |
Output is correct |
21 |
Incorrect |
21 ms |
2972 KB |
Output isn't correct |
22 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
2972 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
2296 KB |
Output is correct |
2 |
Correct |
9 ms |
2304 KB |
Output is correct |
3 |
Correct |
9 ms |
2496 KB |
Output is correct |
4 |
Correct |
9 ms |
2704 KB |
Output is correct |
5 |
Correct |
10 ms |
2704 KB |
Output is correct |
6 |
Correct |
8 ms |
2708 KB |
Output is correct |
7 |
Correct |
8 ms |
2708 KB |
Output is correct |
8 |
Correct |
8 ms |
2716 KB |
Output is correct |
9 |
Correct |
8 ms |
2728 KB |
Output is correct |
10 |
Correct |
9 ms |
2792 KB |
Output is correct |
11 |
Correct |
8 ms |
2904 KB |
Output is correct |
12 |
Correct |
9 ms |
2904 KB |
Output is correct |
13 |
Correct |
8 ms |
2904 KB |
Output is correct |
14 |
Correct |
8 ms |
2904 KB |
Output is correct |
15 |
Correct |
8 ms |
2904 KB |
Output is correct |
16 |
Correct |
5 ms |
2904 KB |
Output is correct |
17 |
Correct |
5 ms |
2912 KB |
Output is correct |
18 |
Correct |
5 ms |
2928 KB |
Output is correct |
19 |
Correct |
6 ms |
2972 KB |
Output is correct |
20 |
Correct |
4 ms |
2972 KB |
Output is correct |
21 |
Incorrect |
21 ms |
2972 KB |
Output isn't correct |
22 |
Halted |
0 ms |
0 KB |
- |