///usr/bin/g++ -O2 $0 -o ${0%.cpp} && echo "----------" && ./${0%.cpp}; exit;
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
const int N = 1024;
const int mod = 1e9 + 7;
int n, a[N], b[N], len[N], dp[N][N];
int fact[N], inv[N];
map<int, int> com;
int Pow(int n, int p) {
int ret = 1; while(p) {
if(p & 1) ret = (ll) ret * n % mod;
n = (ll) n * n % mod;
p >>= 1;
} return ret;
}
void precalc() {
fact[0] = 1;
for(int i = 1; i < N; i++)
fact[i] = (ll) fact[i - 1] * i % mod;
inv[N - 1] = Pow(fact[N - 1], mod - 2);
for(int i = N - 2; i >= 0; i--)
inv[i] = (ll) inv[i + 1] * (i + 1) % mod;
}
int C(int n, int r) {
int ret = (ll) fact[n] * inv[n - r] % mod;
return (ll) ret * inv[r] % mod;
}
void add(int &a, int b) {
a += b; if(a >= mod) a -= mod;
}
int main(int argc, char const *argv[]) {
precalc();
scanf("%d", &n);
vector<int> v = {0};
for(int i = 1; i <= n; i++) {
scanf("%d %d", &a[i], &b[i]); b[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());
for(int i = 1; i < v.size(); i++)
com[v[i]] = i, len[i] = v[i] - v[i - 1];
for(int i = 1; i <= n; i++)
a[i] = com[a[i]], b[i] = com[b[i]];
int m = v.size() - 1;
for(int i = 0; i <= m; i++) dp[0][i] = 1;
for(int i = 1; i <= n; i++) { dp[i][0] = 1;
for(int j = a[i]; j < b[i]; j++) {
int cnt = 1;
dp[i][j] = (ll) len[j] * dp[i - 1][j - 1] % mod;
for(int k = i - 1; k >= 1; k--)
if(a[k] <= j && j < b[k])
add(dp[i][j], (ll) C(len[j], ++cnt) * dp[k - 1][j - 1] % mod);
}
for(int j = 1; j <= m; j++) {
add(dp[i][j], dp[i - 1][j]);
add(dp[i][j], dp[i][j - 1]);
add(dp[i][j], mod - dp[i - 1][j - 1]);
}
}
add(dp[n][m], mod - 1);
printf("%d\n", dp[n][m]);
}
Compilation message
boat.cpp: In function 'int main(int, const char**)':
boat.cpp:54:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 1; i < v.size(); i++)
~~^~~~~~~~~~
boat.cpp:43:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &n);
~~~~~^~~~~~~~~~
boat.cpp:47:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &a[i], &b[i]); b[i]++;
~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
7 ms |
2428 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
7 ms |
2428 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
2 ms |
2428 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
7 ms |
2428 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |