#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define repd(i, a, b) for (int i = (a) - 1; i >= b; i--)
#define pb push_back
#define fi first
#define se second
#define sz(a) ((int)a.size())
typedef long long ll;
const int mod = 1e9 + 7;
const int maxn = 1e3 + 5;
int n, a[maxn], b[maxn];
ll precal[maxn][maxn], inv[maxn];
map<int, int> mm;
ll dp[maxn][maxn], f[maxn];
vector<ll> vi;
ll power_mod(ll a, ll b) {
ll res = 1;
while (b) {
if (b % 2) res = res * a % mod;
a = a * a % mod;
b >>= 1;
}
return res;
}
int main() {
rep(i, 1, maxn) {
inv[i] = power_mod(i, mod - 2);
}
cin >> n;
// mm[0];
rep(i, 0, n) {
cin >> a[i] >> b[i];
mm[a[i] - 1]++;
mm[b[i]]++;
}
int cnt = 0;
for (auto it = mm.begin(); it != mm.end(); it++) {
it->se = cnt++;
vi.pb(it->fi);
}
rep(i, 1, sz(vi)) {
int range = vi[i] - vi[i - 1];
if (range < 2) continue;
rep(j, 1, n + 1) {
// if (j > range) {
// precal[i][j] = precal[i][j - 1];
// continue;
// }
ll cur1 = 1; // Cij
ll cur2 = (ll) range * (range - 1) / 2; // Cirange
cur2 %= mod;
ll res = cur1 * cur2 % mod;
int tar = min(range - 2, j - 2);
rep(k, 1, tar + 1) {
cur1 = cur1 * (ll) (j - k - 1) % mod * inv[k] % mod;
cur2 = cur2 * (ll) (range - k - 1) % mod * inv[k + 2] % mod;
res = (res + cur1 * cur2 % mod) % mod;
}
precal[i][j] = res;
}
}
// cout << precal[3][3] << ' ' << precal[3][2] << endl;
ll ans = 0;
rep(i, 0, n) {
int hi = mm[b[i]] + 1;
int lo = mm[a[i] - 1] + 1;
dp[i][0] = 1; // ntc
repd(k, hi, lo) {
ll ways = (vi[k] - vi[k - 1]) * (i ? dp[i - 1][k - 1] : 1) % mod;
int tot = 1;
repd(j, i, 0) {
if (b[j] < vi[k - 1] + 1 || a[j] > vi[k]) continue;
tot++;
// cout << i << ' ' << k << ' ' << vi[k] - vi[k - 1] << ' ' << tot << endl;
ways += (((j ? dp[j - 1][k - 1] : 1) * precal[k][tot]) % mod);
if (precal[k][tot] == 0) break;
ways %= mod;
}
// cout << i << ' ' << k << ' ' << ways << endl;
dp[i][k] = ways;
}
rep(k, 1, cnt) {
dp[i][k] = (dp[i][k] + f[k]) % mod;
// if (i == 2) cout << f[k] << endl;
f[k] = dp[i][k];
dp[i][k] = (dp[i][k] + dp[i][k - 1]) % mod;
ans = dp[i][k];
}
}
// cout << ans << endl;
cout << ((ans - 1 + mod) % mod) << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
939 ms |
8312 KB |
Output is correct |
2 |
Correct |
936 ms |
8400 KB |
Output is correct |
3 |
Correct |
934 ms |
8436 KB |
Output is correct |
4 |
Correct |
936 ms |
8608 KB |
Output is correct |
5 |
Correct |
932 ms |
8608 KB |
Output is correct |
6 |
Correct |
927 ms |
8612 KB |
Output is correct |
7 |
Correct |
923 ms |
8616 KB |
Output is correct |
8 |
Correct |
935 ms |
8616 KB |
Output is correct |
9 |
Correct |
940 ms |
8744 KB |
Output is correct |
10 |
Correct |
990 ms |
8744 KB |
Output is correct |
11 |
Correct |
936 ms |
8744 KB |
Output is correct |
12 |
Correct |
938 ms |
8744 KB |
Output is correct |
13 |
Correct |
1008 ms |
8796 KB |
Output is correct |
14 |
Correct |
948 ms |
8828 KB |
Output is correct |
15 |
Correct |
946 ms |
8828 KB |
Output is correct |
16 |
Correct |
180 ms |
8828 KB |
Output is correct |
17 |
Correct |
177 ms |
8828 KB |
Output is correct |
18 |
Correct |
171 ms |
8828 KB |
Output is correct |
19 |
Correct |
179 ms |
8828 KB |
Output is correct |
20 |
Correct |
177 ms |
8828 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
939 ms |
8312 KB |
Output is correct |
2 |
Correct |
936 ms |
8400 KB |
Output is correct |
3 |
Correct |
934 ms |
8436 KB |
Output is correct |
4 |
Correct |
936 ms |
8608 KB |
Output is correct |
5 |
Correct |
932 ms |
8608 KB |
Output is correct |
6 |
Correct |
927 ms |
8612 KB |
Output is correct |
7 |
Correct |
923 ms |
8616 KB |
Output is correct |
8 |
Correct |
935 ms |
8616 KB |
Output is correct |
9 |
Correct |
940 ms |
8744 KB |
Output is correct |
10 |
Correct |
990 ms |
8744 KB |
Output is correct |
11 |
Correct |
936 ms |
8744 KB |
Output is correct |
12 |
Correct |
938 ms |
8744 KB |
Output is correct |
13 |
Correct |
1008 ms |
8796 KB |
Output is correct |
14 |
Correct |
948 ms |
8828 KB |
Output is correct |
15 |
Correct |
946 ms |
8828 KB |
Output is correct |
16 |
Correct |
180 ms |
8828 KB |
Output is correct |
17 |
Correct |
177 ms |
8828 KB |
Output is correct |
18 |
Correct |
171 ms |
8828 KB |
Output is correct |
19 |
Correct |
179 ms |
8828 KB |
Output is correct |
20 |
Correct |
177 ms |
8828 KB |
Output is correct |
21 |
Correct |
183 ms |
10576 KB |
Output is correct |
22 |
Correct |
172 ms |
10592 KB |
Output is correct |
23 |
Correct |
163 ms |
10592 KB |
Output is correct |
24 |
Correct |
184 ms |
10692 KB |
Output is correct |
25 |
Correct |
182 ms |
10692 KB |
Output is correct |
26 |
Correct |
278 ms |
10692 KB |
Output is correct |
27 |
Correct |
284 ms |
10692 KB |
Output is correct |
28 |
Correct |
293 ms |
10692 KB |
Output is correct |
29 |
Correct |
322 ms |
10692 KB |
Output is correct |
30 |
Correct |
887 ms |
12156 KB |
Output is correct |
31 |
Correct |
923 ms |
12156 KB |
Output is correct |
32 |
Correct |
907 ms |
12156 KB |
Output is correct |
33 |
Correct |
908 ms |
12156 KB |
Output is correct |
34 |
Correct |
886 ms |
12156 KB |
Output is correct |
35 |
Correct |
881 ms |
12156 KB |
Output is correct |
36 |
Correct |
888 ms |
12156 KB |
Output is correct |
37 |
Correct |
897 ms |
12156 KB |
Output is correct |
38 |
Correct |
960 ms |
12156 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
12156 KB |
Output is correct |
2 |
Correct |
19 ms |
12156 KB |
Output is correct |
3 |
Correct |
20 ms |
12156 KB |
Output is correct |
4 |
Correct |
20 ms |
12156 KB |
Output is correct |
5 |
Correct |
22 ms |
12156 KB |
Output is correct |
6 |
Correct |
20 ms |
12156 KB |
Output is correct |
7 |
Correct |
20 ms |
12156 KB |
Output is correct |
8 |
Correct |
20 ms |
12156 KB |
Output is correct |
9 |
Correct |
20 ms |
12156 KB |
Output is correct |
10 |
Correct |
30 ms |
12156 KB |
Output is correct |
11 |
Correct |
38 ms |
12156 KB |
Output is correct |
12 |
Correct |
20 ms |
12156 KB |
Output is correct |
13 |
Correct |
19 ms |
12156 KB |
Output is correct |
14 |
Correct |
19 ms |
12156 KB |
Output is correct |
15 |
Correct |
19 ms |
12156 KB |
Output is correct |
16 |
Correct |
10 ms |
12156 KB |
Output is correct |
17 |
Correct |
9 ms |
12156 KB |
Output is correct |
18 |
Correct |
10 ms |
12156 KB |
Output is correct |
19 |
Correct |
10 ms |
12156 KB |
Output is correct |
20 |
Correct |
10 ms |
12156 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
939 ms |
8312 KB |
Output is correct |
2 |
Correct |
936 ms |
8400 KB |
Output is correct |
3 |
Correct |
934 ms |
8436 KB |
Output is correct |
4 |
Correct |
936 ms |
8608 KB |
Output is correct |
5 |
Correct |
932 ms |
8608 KB |
Output is correct |
6 |
Correct |
927 ms |
8612 KB |
Output is correct |
7 |
Correct |
923 ms |
8616 KB |
Output is correct |
8 |
Correct |
935 ms |
8616 KB |
Output is correct |
9 |
Correct |
940 ms |
8744 KB |
Output is correct |
10 |
Correct |
990 ms |
8744 KB |
Output is correct |
11 |
Correct |
936 ms |
8744 KB |
Output is correct |
12 |
Correct |
938 ms |
8744 KB |
Output is correct |
13 |
Correct |
1008 ms |
8796 KB |
Output is correct |
14 |
Correct |
948 ms |
8828 KB |
Output is correct |
15 |
Correct |
946 ms |
8828 KB |
Output is correct |
16 |
Correct |
180 ms |
8828 KB |
Output is correct |
17 |
Correct |
177 ms |
8828 KB |
Output is correct |
18 |
Correct |
171 ms |
8828 KB |
Output is correct |
19 |
Correct |
179 ms |
8828 KB |
Output is correct |
20 |
Correct |
177 ms |
8828 KB |
Output is correct |
21 |
Correct |
183 ms |
10576 KB |
Output is correct |
22 |
Correct |
172 ms |
10592 KB |
Output is correct |
23 |
Correct |
163 ms |
10592 KB |
Output is correct |
24 |
Correct |
184 ms |
10692 KB |
Output is correct |
25 |
Correct |
182 ms |
10692 KB |
Output is correct |
26 |
Correct |
278 ms |
10692 KB |
Output is correct |
27 |
Correct |
284 ms |
10692 KB |
Output is correct |
28 |
Correct |
293 ms |
10692 KB |
Output is correct |
29 |
Correct |
322 ms |
10692 KB |
Output is correct |
30 |
Correct |
887 ms |
12156 KB |
Output is correct |
31 |
Correct |
923 ms |
12156 KB |
Output is correct |
32 |
Correct |
907 ms |
12156 KB |
Output is correct |
33 |
Correct |
908 ms |
12156 KB |
Output is correct |
34 |
Correct |
886 ms |
12156 KB |
Output is correct |
35 |
Correct |
881 ms |
12156 KB |
Output is correct |
36 |
Correct |
888 ms |
12156 KB |
Output is correct |
37 |
Correct |
897 ms |
12156 KB |
Output is correct |
38 |
Correct |
960 ms |
12156 KB |
Output is correct |
39 |
Correct |
19 ms |
12156 KB |
Output is correct |
40 |
Correct |
19 ms |
12156 KB |
Output is correct |
41 |
Correct |
20 ms |
12156 KB |
Output is correct |
42 |
Correct |
20 ms |
12156 KB |
Output is correct |
43 |
Correct |
22 ms |
12156 KB |
Output is correct |
44 |
Correct |
20 ms |
12156 KB |
Output is correct |
45 |
Correct |
20 ms |
12156 KB |
Output is correct |
46 |
Correct |
20 ms |
12156 KB |
Output is correct |
47 |
Correct |
20 ms |
12156 KB |
Output is correct |
48 |
Correct |
30 ms |
12156 KB |
Output is correct |
49 |
Correct |
38 ms |
12156 KB |
Output is correct |
50 |
Correct |
20 ms |
12156 KB |
Output is correct |
51 |
Correct |
19 ms |
12156 KB |
Output is correct |
52 |
Correct |
19 ms |
12156 KB |
Output is correct |
53 |
Correct |
19 ms |
12156 KB |
Output is correct |
54 |
Correct |
10 ms |
12156 KB |
Output is correct |
55 |
Correct |
9 ms |
12156 KB |
Output is correct |
56 |
Correct |
10 ms |
12156 KB |
Output is correct |
57 |
Correct |
10 ms |
12156 KB |
Output is correct |
58 |
Correct |
10 ms |
12156 KB |
Output is correct |
59 |
Execution timed out |
2023 ms |
12620 KB |
Time limit exceeded |
60 |
Halted |
0 ms |
0 KB |
- |