#include <bits/stdc++.h>
using namespace std;
typedef long long Long;
typedef pair <int, int> pii;
const int mod = 1000000007;
Long bigmod(int x, int y) {
if(y == 0) return 1;
if(y & 1) return (bigmod(x, y-1) * x) % mod;
Long m = bigmod(x, y >> 1);
return (m * m) % mod;
}
Long inv[505];
int a[505];
int b[505];
int n;
int cntG;
vector <int> g[1005];
int cost[1005];
int c[505][505];
bool overlap(int l, int r, int i) {
if(a[i] <= l and r <= b[i]) return true;
else return false;
}
bool veryBad(int l, int r, int i) {
if(l <= a[i] and a[i] <= r) return true;
if(l <= b[i] and b[i] <= r) return true;
else return false;
}
void divideGroups() {
vector <pii> v;
for(int i = 1; i <= n; i++) {
v.push_back(pii(a[i], 0));
v.push_back(pii(b[i], 1));
}
sort(v.begin(), v.end());
int prev = v.at(0).first;
int now;
for(int i = 1; i < (int) v.size(); i++) {
pii x = v.at(i);
if(x.second == 0) {
now = x.first - 1;
} else {
now = x.first;
}
cost[i] = now - prev + 1;
for(int j = 1; j <= n; j++) {
if(prev > now) continue;
if(overlap(prev, now, j)) {
g[i].push_back(j);
} else {
assert(veryBad(prev, now, j) == false);
}
}
prev = now + 1;
}
cntG = v.size() - 1;
}
int calc(int costx, int x) {
Long sum = 0;
Long nCr = 1;
for(int i = 1; i <= x; i++) {
if(i > costx) break;
nCr *= (costx - i + 1);
nCr %= mod;
nCr *= inv[i];
nCr %= mod;
sum += c[x - 1][i - 1] * nCr;
sum %= mod;
}
return sum;
}
int fn[1005][505];
int gn[1005][505];
int dp(int group, int x) {
if(group > cntG) return 1;
if(fn[group][x] != -1) return fn[group][x];
Long ans = dp(group + 1, x);
int k = 0;
for(int i : g[group]) {
if(i < x) continue;
++k;
ans += 1LL * gn[group][k] * dp(group + 1, i + 1);
ans %= mod;
}
return fn[group][x] = ans;
}
int main(int argc, char const *argv[])
{
ios_base :: sync_with_stdio (false);
cin.tie (0);
for(int i = 1; i <= 500; i++) {
inv[i] = bigmod(i, mod - 2);
}
for(int i = 0; i <= 500; i++) c[i][0] = 1;
for(int i = 1; i <= 500; i++) {
for(int j = 1; j <= 500; j++) {
c[i][j] = c[i - 1][j] + c[i - 1][j - 1];
c[i][j] %= mod;
}
}
cin >> n;
for(int i = 1; i <= n; i++) {
cin >> a[i] >> b[i];
}
divideGroups();
for(int i = 1; i <= cntG; i++) {
int size = g[i].size();
for(int j = 1; j <= size; j++) {
gn[i][j] = calc(cost[i], j);
}
}
memset(fn, -1, sizeof fn);
int ans = dp(1, 1);
ans -= 1;
if(ans < 0) ans += mod;
cout << ans << endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
7180 KB |
Output is correct |
2 |
Correct |
6 ms |
7180 KB |
Output is correct |
3 |
Correct |
13 ms |
7180 KB |
Output is correct |
4 |
Correct |
6 ms |
7180 KB |
Output is correct |
5 |
Correct |
9 ms |
7180 KB |
Output is correct |
6 |
Correct |
6 ms |
7180 KB |
Output is correct |
7 |
Correct |
6 ms |
7180 KB |
Output is correct |
8 |
Correct |
6 ms |
7180 KB |
Output is correct |
9 |
Correct |
6 ms |
7180 KB |
Output is correct |
10 |
Correct |
9 ms |
7180 KB |
Output is correct |
11 |
Correct |
6 ms |
7180 KB |
Output is correct |
12 |
Correct |
6 ms |
7180 KB |
Output is correct |
13 |
Correct |
6 ms |
7180 KB |
Output is correct |
14 |
Correct |
9 ms |
7180 KB |
Output is correct |
15 |
Correct |
9 ms |
7180 KB |
Output is correct |
16 |
Correct |
9 ms |
7180 KB |
Output is correct |
17 |
Correct |
9 ms |
7180 KB |
Output is correct |
18 |
Correct |
3 ms |
7180 KB |
Output is correct |
19 |
Correct |
6 ms |
7180 KB |
Output is correct |
20 |
Correct |
9 ms |
7180 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
7180 KB |
Output is correct |
2 |
Correct |
6 ms |
7180 KB |
Output is correct |
3 |
Correct |
13 ms |
7180 KB |
Output is correct |
4 |
Correct |
6 ms |
7180 KB |
Output is correct |
5 |
Correct |
9 ms |
7180 KB |
Output is correct |
6 |
Correct |
6 ms |
7180 KB |
Output is correct |
7 |
Correct |
6 ms |
7180 KB |
Output is correct |
8 |
Correct |
6 ms |
7180 KB |
Output is correct |
9 |
Correct |
6 ms |
7180 KB |
Output is correct |
10 |
Correct |
9 ms |
7180 KB |
Output is correct |
11 |
Correct |
6 ms |
7180 KB |
Output is correct |
12 |
Correct |
6 ms |
7180 KB |
Output is correct |
13 |
Correct |
6 ms |
7180 KB |
Output is correct |
14 |
Correct |
9 ms |
7180 KB |
Output is correct |
15 |
Correct |
9 ms |
7180 KB |
Output is correct |
16 |
Correct |
9 ms |
7180 KB |
Output is correct |
17 |
Correct |
9 ms |
7180 KB |
Output is correct |
18 |
Correct |
3 ms |
7180 KB |
Output is correct |
19 |
Correct |
6 ms |
7180 KB |
Output is correct |
20 |
Correct |
9 ms |
7180 KB |
Output is correct |
21 |
Correct |
209 ms |
7972 KB |
Output is correct |
22 |
Correct |
193 ms |
7972 KB |
Output is correct |
23 |
Correct |
176 ms |
7840 KB |
Output is correct |
24 |
Correct |
199 ms |
7972 KB |
Output is correct |
25 |
Correct |
193 ms |
7972 KB |
Output is correct |
26 |
Correct |
353 ms |
8368 KB |
Output is correct |
27 |
Correct |
353 ms |
8368 KB |
Output is correct |
28 |
Correct |
343 ms |
8368 KB |
Output is correct |
29 |
Correct |
343 ms |
8368 KB |
Output is correct |
30 |
Correct |
13 ms |
7180 KB |
Output is correct |
31 |
Correct |
9 ms |
7180 KB |
Output is correct |
32 |
Correct |
13 ms |
7180 KB |
Output is correct |
33 |
Correct |
13 ms |
7180 KB |
Output is correct |
34 |
Correct |
6 ms |
7180 KB |
Output is correct |
35 |
Correct |
6 ms |
7180 KB |
Output is correct |
36 |
Correct |
6 ms |
7180 KB |
Output is correct |
37 |
Correct |
13 ms |
7180 KB |
Output is correct |
38 |
Correct |
9 ms |
7180 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
7180 KB |
Output is correct |
2 |
Correct |
3 ms |
7180 KB |
Output is correct |
3 |
Correct |
3 ms |
7180 KB |
Output is correct |
4 |
Correct |
6 ms |
7180 KB |
Output is correct |
5 |
Correct |
3 ms |
7180 KB |
Output is correct |
6 |
Correct |
0 ms |
7180 KB |
Output is correct |
7 |
Correct |
9 ms |
7180 KB |
Output is correct |
8 |
Correct |
9 ms |
7180 KB |
Output is correct |
9 |
Correct |
6 ms |
7180 KB |
Output is correct |
10 |
Correct |
6 ms |
7180 KB |
Output is correct |
11 |
Correct |
6 ms |
7180 KB |
Output is correct |
12 |
Correct |
6 ms |
7180 KB |
Output is correct |
13 |
Correct |
6 ms |
7180 KB |
Output is correct |
14 |
Correct |
6 ms |
7180 KB |
Output is correct |
15 |
Correct |
6 ms |
7180 KB |
Output is correct |
16 |
Correct |
0 ms |
7180 KB |
Output is correct |
17 |
Correct |
3 ms |
7180 KB |
Output is correct |
18 |
Correct |
3 ms |
7180 KB |
Output is correct |
19 |
Correct |
0 ms |
7180 KB |
Output is correct |
20 |
Correct |
3 ms |
7180 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
7180 KB |
Output is correct |
2 |
Correct |
6 ms |
7180 KB |
Output is correct |
3 |
Correct |
13 ms |
7180 KB |
Output is correct |
4 |
Correct |
6 ms |
7180 KB |
Output is correct |
5 |
Correct |
9 ms |
7180 KB |
Output is correct |
6 |
Correct |
6 ms |
7180 KB |
Output is correct |
7 |
Correct |
6 ms |
7180 KB |
Output is correct |
8 |
Correct |
6 ms |
7180 KB |
Output is correct |
9 |
Correct |
6 ms |
7180 KB |
Output is correct |
10 |
Correct |
9 ms |
7180 KB |
Output is correct |
11 |
Correct |
6 ms |
7180 KB |
Output is correct |
12 |
Correct |
6 ms |
7180 KB |
Output is correct |
13 |
Correct |
6 ms |
7180 KB |
Output is correct |
14 |
Correct |
9 ms |
7180 KB |
Output is correct |
15 |
Correct |
9 ms |
7180 KB |
Output is correct |
16 |
Correct |
9 ms |
7180 KB |
Output is correct |
17 |
Correct |
9 ms |
7180 KB |
Output is correct |
18 |
Correct |
3 ms |
7180 KB |
Output is correct |
19 |
Correct |
6 ms |
7180 KB |
Output is correct |
20 |
Correct |
9 ms |
7180 KB |
Output is correct |
21 |
Correct |
209 ms |
7972 KB |
Output is correct |
22 |
Correct |
193 ms |
7972 KB |
Output is correct |
23 |
Correct |
176 ms |
7840 KB |
Output is correct |
24 |
Correct |
199 ms |
7972 KB |
Output is correct |
25 |
Correct |
193 ms |
7972 KB |
Output is correct |
26 |
Correct |
353 ms |
8368 KB |
Output is correct |
27 |
Correct |
353 ms |
8368 KB |
Output is correct |
28 |
Correct |
343 ms |
8368 KB |
Output is correct |
29 |
Correct |
343 ms |
8368 KB |
Output is correct |
30 |
Correct |
13 ms |
7180 KB |
Output is correct |
31 |
Correct |
9 ms |
7180 KB |
Output is correct |
32 |
Correct |
13 ms |
7180 KB |
Output is correct |
33 |
Correct |
13 ms |
7180 KB |
Output is correct |
34 |
Correct |
6 ms |
7180 KB |
Output is correct |
35 |
Correct |
6 ms |
7180 KB |
Output is correct |
36 |
Correct |
6 ms |
7180 KB |
Output is correct |
37 |
Correct |
13 ms |
7180 KB |
Output is correct |
38 |
Correct |
9 ms |
7180 KB |
Output is correct |
39 |
Correct |
3 ms |
7180 KB |
Output is correct |
40 |
Correct |
3 ms |
7180 KB |
Output is correct |
41 |
Correct |
3 ms |
7180 KB |
Output is correct |
42 |
Correct |
6 ms |
7180 KB |
Output is correct |
43 |
Correct |
3 ms |
7180 KB |
Output is correct |
44 |
Correct |
0 ms |
7180 KB |
Output is correct |
45 |
Correct |
9 ms |
7180 KB |
Output is correct |
46 |
Correct |
9 ms |
7180 KB |
Output is correct |
47 |
Correct |
6 ms |
7180 KB |
Output is correct |
48 |
Correct |
6 ms |
7180 KB |
Output is correct |
49 |
Correct |
6 ms |
7180 KB |
Output is correct |
50 |
Correct |
6 ms |
7180 KB |
Output is correct |
51 |
Correct |
6 ms |
7180 KB |
Output is correct |
52 |
Correct |
6 ms |
7180 KB |
Output is correct |
53 |
Correct |
6 ms |
7180 KB |
Output is correct |
54 |
Correct |
0 ms |
7180 KB |
Output is correct |
55 |
Correct |
3 ms |
7180 KB |
Output is correct |
56 |
Correct |
3 ms |
7180 KB |
Output is correct |
57 |
Correct |
0 ms |
7180 KB |
Output is correct |
58 |
Correct |
3 ms |
7180 KB |
Output is correct |
59 |
Correct |
456 ms |
7972 KB |
Output is correct |
60 |
Correct |
419 ms |
7972 KB |
Output is correct |
61 |
Correct |
409 ms |
7972 KB |
Output is correct |
62 |
Correct |
466 ms |
8104 KB |
Output is correct |
63 |
Correct |
459 ms |
7972 KB |
Output is correct |
64 |
Correct |
976 ms |
8500 KB |
Output is correct |
65 |
Correct |
966 ms |
8500 KB |
Output is correct |
66 |
Correct |
976 ms |
8500 KB |
Output is correct |
67 |
Correct |
969 ms |
8500 KB |
Output is correct |
68 |
Correct |
976 ms |
8500 KB |
Output is correct |
69 |
Correct |
426 ms |
7972 KB |
Output is correct |
70 |
Correct |
426 ms |
7972 KB |
Output is correct |
71 |
Correct |
419 ms |
7972 KB |
Output is correct |
72 |
Correct |
449 ms |
7972 KB |
Output is correct |
73 |
Correct |
466 ms |
7972 KB |
Output is correct |
74 |
Correct |
86 ms |
7312 KB |
Output is correct |
75 |
Correct |
79 ms |
7312 KB |
Output is correct |
76 |
Correct |
69 ms |
7312 KB |
Output is correct |
77 |
Correct |
69 ms |
7312 KB |
Output is correct |
78 |
Correct |
69 ms |
7312 KB |
Output is correct |