#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> ii;
#define SZ(x) ((int)x.size())
const int mod = (int)1e9+7;
const int N = 505;
int n, f[N], g[N], ftmp[N], gtmp[N], a[N], b[N], inv[N], tmp[N], m;
int choose[N][N], nCk[N], nCk2[N], choose1[N][N], choose2[N][N];
vector<int> c;
int mul(int x, int y) {
x %= mod, y %= mod;
if (x < 0) x += mod;
if (y < 0) y += mod;
ll re = (ll)x * (ll)y % (ll)mod;
return (int)re;
}
void add(int &x, int y) {
x += y, x %= mod;
if (x < 0) x += mod;
}
int fast_pw(int x, int y) {
if (y == 1) return x % mod;
if (y == 0) return 1;
int t = fast_pw(x, y >> 1);
t = mul(t, t);
if (y & 1) t = mul(t, x);
return t;
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
scanf("%d%d", &a[i], &b[i]);
c.push_back(a[i]);
c.push_back(b[i]);
}
sort(c.begin(), c.end());
c.resize(distance(c.begin(), unique(c.begin(), c.end())));
for (int i = 0; i <= n; ++i) choose[i][i] = choose[i][0] = 1;
inv[0] = f[0] = g[0] = 1;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j < i; ++j) choose[i][j] = (choose[i - 1][j - 1] + choose[i - 1][j]) % mod;
inv[i] = mul(inv[i - 1], fast_pw(i, mod - 2));
g[i] = 1;
}
int sz = SZ(c);
c.push_back(n);
for (int i = 0; i < sz; ++i) {
m = 0;
int total = c[i + 1] - c[i], tmp_mul1 = 1, tmp_mul2 = 1;
for (int j = 1; j <= n; ++j) {
if (b[j] == c[i]) ftmp[j] = gtmp[j] = g[j - 1];
else if (a[j] <= c[i] && c[i] <= b[j]) tmp[++m] = j;
if (total - j + 1 > 0) {
tmp_mul1 = mul(tmp_mul1, total - j + 1);
nCk[j] = mul(tmp_mul1, inv[j]);
}
if (total - j > 0) {
tmp_mul2 = mul(tmp_mul2, total - j);
nCk2[j] = mul(tmp_mul2, inv[j]);
}
}
if (m > 0) {
for (int j = 1; j <= n; ++j) add(gtmp[j], gtmp[j - 1]);
for (int j = 0; j <= m - 2; ++j) {
int lmt = min(j, total - 2);
for (int k = 0; k <= lmt; ++k) {
choose1[j][k] = mul(choose[j][k], nCk[k + 2]);
choose2[j][k] = mul(choose[j][k], nCk2[k + 2]);
if (k > 0) add(choose1[j][k], choose1[j][k - 1]);
if (k > 0) add(choose2[j][k], choose2[j][k - 1]);
}
}
for (int j = m; j > 0; --j) {
int id = tmp[j];
add(ftmp[id], mul(nCk[1], g[id - 1]));
if (total > 1) {
for (int k = j - 1; k > 0; --k) {
int mid = j - k - 1, id2 = tmp[k];
add(ftmp[id], mul(g[id2 - 1], choose1[mid][min(mid, total - 2)]));
if (total > 2) add(ftmp[id], mul(gtmp[id2 - 1], choose2[mid][min(mid, total - 3)]));
}
}
}
}
g[0] = 1;
for (int i = 1; i <= n; ++i) {
add(f[i], ftmp[i]);
g[i] = (g[i - 1] + f[i]) % mod;
ftmp[i] = gtmp[i] = 0;
}
}
printf("%d", (g[n] - 1 + mod) % mod);
return 0;
}
Compilation message
boat.cpp: In function 'int main()':
boat.cpp:40:17: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &n);
^
boat.cpp:42:30: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &a[i], &b[i]);
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
1272 KB |
Output is correct |
2 |
Correct |
9 ms |
1376 KB |
Output is correct |
3 |
Correct |
9 ms |
1448 KB |
Output is correct |
4 |
Correct |
10 ms |
1464 KB |
Output is correct |
5 |
Correct |
11 ms |
1500 KB |
Output is correct |
6 |
Correct |
9 ms |
1552 KB |
Output is correct |
7 |
Correct |
10 ms |
1552 KB |
Output is correct |
8 |
Correct |
9 ms |
1572 KB |
Output is correct |
9 |
Correct |
10 ms |
1572 KB |
Output is correct |
10 |
Correct |
9 ms |
1572 KB |
Output is correct |
11 |
Correct |
10 ms |
1572 KB |
Output is correct |
12 |
Correct |
9 ms |
1572 KB |
Output is correct |
13 |
Correct |
10 ms |
1572 KB |
Output is correct |
14 |
Correct |
9 ms |
1572 KB |
Output is correct |
15 |
Correct |
9 ms |
1572 KB |
Output is correct |
16 |
Correct |
4 ms |
1572 KB |
Output is correct |
17 |
Correct |
4 ms |
1572 KB |
Output is correct |
18 |
Correct |
4 ms |
1572 KB |
Output is correct |
19 |
Correct |
4 ms |
1572 KB |
Output is correct |
20 |
Correct |
4 ms |
1572 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
1272 KB |
Output is correct |
2 |
Correct |
9 ms |
1376 KB |
Output is correct |
3 |
Correct |
9 ms |
1448 KB |
Output is correct |
4 |
Correct |
10 ms |
1464 KB |
Output is correct |
5 |
Correct |
11 ms |
1500 KB |
Output is correct |
6 |
Correct |
9 ms |
1552 KB |
Output is correct |
7 |
Correct |
10 ms |
1552 KB |
Output is correct |
8 |
Correct |
9 ms |
1572 KB |
Output is correct |
9 |
Correct |
10 ms |
1572 KB |
Output is correct |
10 |
Correct |
9 ms |
1572 KB |
Output is correct |
11 |
Correct |
10 ms |
1572 KB |
Output is correct |
12 |
Correct |
9 ms |
1572 KB |
Output is correct |
13 |
Correct |
10 ms |
1572 KB |
Output is correct |
14 |
Correct |
9 ms |
1572 KB |
Output is correct |
15 |
Correct |
9 ms |
1572 KB |
Output is correct |
16 |
Correct |
4 ms |
1572 KB |
Output is correct |
17 |
Correct |
4 ms |
1572 KB |
Output is correct |
18 |
Correct |
4 ms |
1572 KB |
Output is correct |
19 |
Correct |
4 ms |
1572 KB |
Output is correct |
20 |
Correct |
4 ms |
1572 KB |
Output is correct |
21 |
Incorrect |
362 ms |
2696 KB |
Output isn't correct |
22 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
8 ms |
2696 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
1272 KB |
Output is correct |
2 |
Correct |
9 ms |
1376 KB |
Output is correct |
3 |
Correct |
9 ms |
1448 KB |
Output is correct |
4 |
Correct |
10 ms |
1464 KB |
Output is correct |
5 |
Correct |
11 ms |
1500 KB |
Output is correct |
6 |
Correct |
9 ms |
1552 KB |
Output is correct |
7 |
Correct |
10 ms |
1552 KB |
Output is correct |
8 |
Correct |
9 ms |
1572 KB |
Output is correct |
9 |
Correct |
10 ms |
1572 KB |
Output is correct |
10 |
Correct |
9 ms |
1572 KB |
Output is correct |
11 |
Correct |
10 ms |
1572 KB |
Output is correct |
12 |
Correct |
9 ms |
1572 KB |
Output is correct |
13 |
Correct |
10 ms |
1572 KB |
Output is correct |
14 |
Correct |
9 ms |
1572 KB |
Output is correct |
15 |
Correct |
9 ms |
1572 KB |
Output is correct |
16 |
Correct |
4 ms |
1572 KB |
Output is correct |
17 |
Correct |
4 ms |
1572 KB |
Output is correct |
18 |
Correct |
4 ms |
1572 KB |
Output is correct |
19 |
Correct |
4 ms |
1572 KB |
Output is correct |
20 |
Correct |
4 ms |
1572 KB |
Output is correct |
21 |
Incorrect |
362 ms |
2696 KB |
Output isn't correct |
22 |
Halted |
0 ms |
0 KB |
- |