#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
constexpr bool typetest = 0;
constexpr int N = 5e2 + 2;
constexpr ll mod = 1e9 + 7;
/* https://codeforces.com/blog/entry/57324?#comment-409800 */
int n;
/// dp[i][j]: choose i first elements in j first blocks
ll dp[N][N * 2], d[N * 2][N], inv[N];
pair<int, int> s[N];
vector<int> block;
void Read()
{
cin >> n;
block.reserve(n * 2);
for (int i = 1; i <= n; ++i)
{
cin >> s[i].first >> s[i].second;
block.emplace_back(s[i].first - 1);
block.emplace_back(s[i].second);
}
sort(block.begin(), block.end());
block.resize(unique(block.begin(), block.end()) - block.begin());
}
ll Pow(ll a, ll b)
{
ll ans(1);
for (; b > 0; b >>= 1)
{
if (b & 1)
ans = ans * a % mod;
a = a * a % mod;
}
return ans;
}
void Solve()
{
inv[0] = 1;
for (int i = 1; i <= n; ++i)
inv[i] = Pow(i, mod - 2) % mod;
/// Pre-calculate d[], I'll define d[] below
for (int i = 1; i < (int)block.size(); ++i)
{
d[i][0] = 1;
for (int j = 1; j <= n; ++j)
{
ll c1(1), c2(1);
for (int t = 0; t < j && t + 1 <= block[i] - block[i - 1]; ++t)
{
c2 = c2 * ((block[i] - block[i - 1] - (t + 1) + 1) % mod) % mod * inv[t + 1] % mod;
(d[i][j] += c1 * c2) %= mod;
c1 = c1 * (j - 1 - (t + 1) + 1) % mod * inv[t + 1] % mod;
}
}
}
for (int i = 0; i < (int)block.size(); ++i)
dp[0][i] = 1;
for (int i = 1; i <= n; ++i)
{
dp[i][0] = 1;
for (int j = 1; j < (int)block.size(); ++j)
{
/// no element in j block
dp[i][j] = dp[i][j - 1];
/// there're some elements in j block
for (int t = i, cnt(0); t; --t)
if (s[t].first <= block[j - 1] + 1 && s[t].second >= block[j])
{
++cnt;
//cout << cnt << "+" << t << "-";
(dp[i][j] += dp[t - 1][j - 1] * d[j][cnt]) %= mod;
/// d[j][i] : number of ways to make array that have less or equal i element only from block j
/// to prevent overlap counting, we need to change sthg about definition
}
//cout << dp[i][j] << " ";
}
//cout << "\n";
}
cout << dp[n][block.size() - 1] - 1;
}
int32_t main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t(1);
if (typetest)
cin >> t;
for (int _ = 1; _ <= t; ++_)
{
Read();
Solve();
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1138 ms |
8068 KB |
Output is correct |
2 |
Correct |
1144 ms |
8140 KB |
Output is correct |
3 |
Correct |
1131 ms |
8080 KB |
Output is correct |
4 |
Correct |
1153 ms |
8244 KB |
Output is correct |
5 |
Correct |
1153 ms |
8276 KB |
Output is correct |
6 |
Correct |
1014 ms |
8120 KB |
Output is correct |
7 |
Correct |
1022 ms |
8148 KB |
Output is correct |
8 |
Correct |
1022 ms |
8152 KB |
Output is correct |
9 |
Correct |
1069 ms |
8156 KB |
Output is correct |
10 |
Correct |
1055 ms |
8244 KB |
Output is correct |
11 |
Correct |
1059 ms |
8108 KB |
Output is correct |
12 |
Correct |
1051 ms |
8128 KB |
Output is correct |
13 |
Correct |
1008 ms |
8132 KB |
Output is correct |
14 |
Correct |
1067 ms |
8076 KB |
Output is correct |
15 |
Correct |
1047 ms |
8124 KB |
Output is correct |
16 |
Correct |
213 ms |
3652 KB |
Output is correct |
17 |
Correct |
220 ms |
3736 KB |
Output is correct |
18 |
Correct |
230 ms |
3764 KB |
Output is correct |
19 |
Correct |
237 ms |
3688 KB |
Output is correct |
20 |
Correct |
214 ms |
3636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1138 ms |
8068 KB |
Output is correct |
2 |
Correct |
1144 ms |
8140 KB |
Output is correct |
3 |
Correct |
1131 ms |
8080 KB |
Output is correct |
4 |
Correct |
1153 ms |
8244 KB |
Output is correct |
5 |
Correct |
1153 ms |
8276 KB |
Output is correct |
6 |
Correct |
1014 ms |
8120 KB |
Output is correct |
7 |
Correct |
1022 ms |
8148 KB |
Output is correct |
8 |
Correct |
1022 ms |
8152 KB |
Output is correct |
9 |
Correct |
1069 ms |
8156 KB |
Output is correct |
10 |
Correct |
1055 ms |
8244 KB |
Output is correct |
11 |
Correct |
1059 ms |
8108 KB |
Output is correct |
12 |
Correct |
1051 ms |
8128 KB |
Output is correct |
13 |
Correct |
1008 ms |
8132 KB |
Output is correct |
14 |
Correct |
1067 ms |
8076 KB |
Output is correct |
15 |
Correct |
1047 ms |
8124 KB |
Output is correct |
16 |
Correct |
213 ms |
3652 KB |
Output is correct |
17 |
Correct |
220 ms |
3736 KB |
Output is correct |
18 |
Correct |
230 ms |
3764 KB |
Output is correct |
19 |
Correct |
237 ms |
3688 KB |
Output is correct |
20 |
Correct |
214 ms |
3636 KB |
Output is correct |
21 |
Correct |
420 ms |
8000 KB |
Output is correct |
22 |
Correct |
432 ms |
7924 KB |
Output is correct |
23 |
Correct |
414 ms |
7772 KB |
Output is correct |
24 |
Correct |
442 ms |
7692 KB |
Output is correct |
25 |
Correct |
435 ms |
7796 KB |
Output is correct |
26 |
Correct |
518 ms |
7660 KB |
Output is correct |
27 |
Correct |
528 ms |
7760 KB |
Output is correct |
28 |
Correct |
574 ms |
7732 KB |
Output is correct |
29 |
Correct |
559 ms |
7656 KB |
Output is correct |
30 |
Correct |
1125 ms |
8224 KB |
Output is correct |
31 |
Correct |
1038 ms |
8172 KB |
Output is correct |
32 |
Correct |
1066 ms |
8132 KB |
Output is correct |
33 |
Correct |
1047 ms |
8200 KB |
Output is correct |
34 |
Correct |
987 ms |
8116 KB |
Output is correct |
35 |
Correct |
1090 ms |
8088 KB |
Output is correct |
36 |
Correct |
1124 ms |
8056 KB |
Output is correct |
37 |
Correct |
1138 ms |
8136 KB |
Output is correct |
38 |
Correct |
1092 ms |
8132 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
1612 KB |
Output is correct |
2 |
Correct |
18 ms |
1668 KB |
Output is correct |
3 |
Correct |
19 ms |
1588 KB |
Output is correct |
4 |
Correct |
18 ms |
1612 KB |
Output is correct |
5 |
Correct |
18 ms |
1596 KB |
Output is correct |
6 |
Correct |
20 ms |
1644 KB |
Output is correct |
7 |
Correct |
21 ms |
1612 KB |
Output is correct |
8 |
Correct |
20 ms |
1596 KB |
Output is correct |
9 |
Correct |
19 ms |
1612 KB |
Output is correct |
10 |
Correct |
20 ms |
1612 KB |
Output is correct |
11 |
Correct |
19 ms |
1612 KB |
Output is correct |
12 |
Correct |
24 ms |
1612 KB |
Output is correct |
13 |
Correct |
19 ms |
1612 KB |
Output is correct |
14 |
Correct |
18 ms |
1612 KB |
Output is correct |
15 |
Correct |
20 ms |
1640 KB |
Output is correct |
16 |
Correct |
9 ms |
1228 KB |
Output is correct |
17 |
Correct |
10 ms |
1228 KB |
Output is correct |
18 |
Correct |
10 ms |
1228 KB |
Output is correct |
19 |
Correct |
9 ms |
1248 KB |
Output is correct |
20 |
Correct |
10 ms |
1216 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1138 ms |
8068 KB |
Output is correct |
2 |
Correct |
1144 ms |
8140 KB |
Output is correct |
3 |
Correct |
1131 ms |
8080 KB |
Output is correct |
4 |
Correct |
1153 ms |
8244 KB |
Output is correct |
5 |
Correct |
1153 ms |
8276 KB |
Output is correct |
6 |
Correct |
1014 ms |
8120 KB |
Output is correct |
7 |
Correct |
1022 ms |
8148 KB |
Output is correct |
8 |
Correct |
1022 ms |
8152 KB |
Output is correct |
9 |
Correct |
1069 ms |
8156 KB |
Output is correct |
10 |
Correct |
1055 ms |
8244 KB |
Output is correct |
11 |
Correct |
1059 ms |
8108 KB |
Output is correct |
12 |
Correct |
1051 ms |
8128 KB |
Output is correct |
13 |
Correct |
1008 ms |
8132 KB |
Output is correct |
14 |
Correct |
1067 ms |
8076 KB |
Output is correct |
15 |
Correct |
1047 ms |
8124 KB |
Output is correct |
16 |
Correct |
213 ms |
3652 KB |
Output is correct |
17 |
Correct |
220 ms |
3736 KB |
Output is correct |
18 |
Correct |
230 ms |
3764 KB |
Output is correct |
19 |
Correct |
237 ms |
3688 KB |
Output is correct |
20 |
Correct |
214 ms |
3636 KB |
Output is correct |
21 |
Correct |
420 ms |
8000 KB |
Output is correct |
22 |
Correct |
432 ms |
7924 KB |
Output is correct |
23 |
Correct |
414 ms |
7772 KB |
Output is correct |
24 |
Correct |
442 ms |
7692 KB |
Output is correct |
25 |
Correct |
435 ms |
7796 KB |
Output is correct |
26 |
Correct |
518 ms |
7660 KB |
Output is correct |
27 |
Correct |
528 ms |
7760 KB |
Output is correct |
28 |
Correct |
574 ms |
7732 KB |
Output is correct |
29 |
Correct |
559 ms |
7656 KB |
Output is correct |
30 |
Correct |
1125 ms |
8224 KB |
Output is correct |
31 |
Correct |
1038 ms |
8172 KB |
Output is correct |
32 |
Correct |
1066 ms |
8132 KB |
Output is correct |
33 |
Correct |
1047 ms |
8200 KB |
Output is correct |
34 |
Correct |
987 ms |
8116 KB |
Output is correct |
35 |
Correct |
1090 ms |
8088 KB |
Output is correct |
36 |
Correct |
1124 ms |
8056 KB |
Output is correct |
37 |
Correct |
1138 ms |
8136 KB |
Output is correct |
38 |
Correct |
1092 ms |
8132 KB |
Output is correct |
39 |
Correct |
18 ms |
1612 KB |
Output is correct |
40 |
Correct |
18 ms |
1668 KB |
Output is correct |
41 |
Correct |
19 ms |
1588 KB |
Output is correct |
42 |
Correct |
18 ms |
1612 KB |
Output is correct |
43 |
Correct |
18 ms |
1596 KB |
Output is correct |
44 |
Correct |
20 ms |
1644 KB |
Output is correct |
45 |
Correct |
21 ms |
1612 KB |
Output is correct |
46 |
Correct |
20 ms |
1596 KB |
Output is correct |
47 |
Correct |
19 ms |
1612 KB |
Output is correct |
48 |
Correct |
20 ms |
1612 KB |
Output is correct |
49 |
Correct |
19 ms |
1612 KB |
Output is correct |
50 |
Correct |
24 ms |
1612 KB |
Output is correct |
51 |
Correct |
19 ms |
1612 KB |
Output is correct |
52 |
Correct |
18 ms |
1612 KB |
Output is correct |
53 |
Correct |
20 ms |
1640 KB |
Output is correct |
54 |
Correct |
9 ms |
1228 KB |
Output is correct |
55 |
Correct |
10 ms |
1228 KB |
Output is correct |
56 |
Correct |
10 ms |
1228 KB |
Output is correct |
57 |
Correct |
9 ms |
1248 KB |
Output is correct |
58 |
Correct |
10 ms |
1216 KB |
Output is correct |
59 |
Execution timed out |
2055 ms |
7404 KB |
Time limit exceeded |
60 |
Halted |
0 ms |
0 KB |
- |