#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define F first
#define S second
#define pii pair<int,int>
#define mpr make_pair
const int maxn = 1010;
const ll inf = 1e18+10;
const int mod = 1e9+7;
const int N = 2e7+10;
int n;
int dp[maxn][maxn], sum[maxn][maxn], C[maxn][maxn], C2[maxn][maxn], a[maxn], b[maxn], fac[maxn], repo[maxn];
vector<int> ve;
int pw(int a, int b)
{
if(b == 0) return 1;
int x = pw(a,b/2); x = (x * 1ll * x) % mod;
if(b&1) x = (x * 1ll * a) % mod;
return x;
}
signed main()
{
//ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
scanf("%d", &n);
fac[0] = 1; repo[0] = 1;
for(int i = 1; i <= n; i++)
{
fac[i] = (fac[i-1] * 1ll * i) % mod;
repo[i] = pw(fac[i],mod-2);
scanf("%d%d", &a[i], &b[i]);
ve.push_back(a[i]);
ve.push_back(b[i]);
}
ve.push_back(0);
sort(ve.begin(), ve.end());
ve.resize(unique(ve.begin(), ve.end()) - ve.begin());
int k = ve.size();
for(int j = 0; j < k; j++)
for(int x = 0; x <= n; x++)
{
int T;
if(j == 0) T = 1;
else T = ve[j] - ve[j-1];
if(x <= 2)
{
int up = 1, dow = 1;
for(int y = 1; y <= min(x,T); y++)
{
up = (up * 1ll * (T-y+1)) % mod;
dow = (repo[y-1] * 1ll * repo[x-y]) % mod;
dow = (dow * 1ll * repo[y]) % mod;
(C[j][x] += (((fac[x-1] * 1ll * up) % mod) * 1ll * dow) % mod) %= mod;
}
up = 1; dow = 1; T--;
for(int y = 0; y <= min(x,T); y++)
{
if(y) up = (up * 1ll * (T-y+1)) % mod;
dow = (repo[y] * 1ll * repo[x-y]) % mod;
dow = (dow * 1ll * repo[y]) % mod;
(C2[j][x] += (((fac[x] * 1ll * up) % mod) * 1ll * dow) % mod) %= mod;
}
}
else
{
int up = 1;
for(int y = 1; y <= x; y++)
up = (up * 1ll * (y+T-1)) % mod;
C[j][x] = C2[j][x] = (up * 1ll * repo[x]) % mod;
}
//cout<< T <<" "<< x <<" "<< C[j][x] <<" "<< C2[j][x] <<"\n";
}
int ANS = 0;
for(int i = 0; i <= n; i++)
for(int j = 0; j < k; j++)
{
if(i == 0)
{
sum[i][j] = 1;
dp[i][0] = 1;
continue;
}
if(j == 0)
{
dp[i][j] = 1;
sum[i][j] = 1;
}
else
{
int L = ve[j-1]+1, R = ve[j];
bool X = 0; int nm = 0;
for(int x = i-1; x >= 0; x--)
{
int l = max(L,a[x+1]), r = min(R,b[x+1]);
nm++;
if(l > r && x == i-1) break;
if(l > r) nm--;
if(l == r)
{
if(x == i-1) X = 1;
nm--;
}
int val = (sum[x][j-1]-1); if(!x) val = 1;
if(!X)
(dp[i][j] += (val * 1ll * C[j][nm]) % mod) %= mod;
else
(dp[i][j] += ((val * 1ll * C2[j][nm]) % mod)) %= mod;
}
sum[i][j] = (sum[i][j-1] + dp[i][j]) % mod;
(ANS += dp[i][j]) %= mod;
}
//cout<< i <<" "<< j <<" "<< dp[i][j] <<"\n";
}
printf("%d", ANS);
}
/*
3
1 2
5 10
100 100
3
1 2
2 3
3 4
*/
Compilation message
boat.cpp: In function 'int main()':
boat.cpp:34:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &n);
~~~~~^~~~~~~~~~
boat.cpp:41:12: 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 |
362 ms |
8312 KB |
Output is correct |
2 |
Correct |
362 ms |
8312 KB |
Output is correct |
3 |
Correct |
365 ms |
8312 KB |
Output is correct |
4 |
Correct |
360 ms |
8312 KB |
Output is correct |
5 |
Correct |
361 ms |
8304 KB |
Output is correct |
6 |
Correct |
363 ms |
8312 KB |
Output is correct |
7 |
Correct |
360 ms |
8360 KB |
Output is correct |
8 |
Correct |
364 ms |
8312 KB |
Output is correct |
9 |
Correct |
361 ms |
8312 KB |
Output is correct |
10 |
Correct |
360 ms |
8312 KB |
Output is correct |
11 |
Correct |
361 ms |
8312 KB |
Output is correct |
12 |
Correct |
361 ms |
8312 KB |
Output is correct |
13 |
Correct |
361 ms |
8312 KB |
Output is correct |
14 |
Correct |
361 ms |
8312 KB |
Output is correct |
15 |
Correct |
361 ms |
8312 KB |
Output is correct |
16 |
Correct |
67 ms |
5112 KB |
Output is correct |
17 |
Correct |
72 ms |
5100 KB |
Output is correct |
18 |
Correct |
69 ms |
4984 KB |
Output is correct |
19 |
Correct |
72 ms |
5112 KB |
Output is correct |
20 |
Correct |
69 ms |
4984 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
362 ms |
8312 KB |
Output is correct |
2 |
Correct |
362 ms |
8312 KB |
Output is correct |
3 |
Correct |
365 ms |
8312 KB |
Output is correct |
4 |
Correct |
360 ms |
8312 KB |
Output is correct |
5 |
Correct |
361 ms |
8304 KB |
Output is correct |
6 |
Correct |
363 ms |
8312 KB |
Output is correct |
7 |
Correct |
360 ms |
8360 KB |
Output is correct |
8 |
Correct |
364 ms |
8312 KB |
Output is correct |
9 |
Correct |
361 ms |
8312 KB |
Output is correct |
10 |
Correct |
360 ms |
8312 KB |
Output is correct |
11 |
Correct |
361 ms |
8312 KB |
Output is correct |
12 |
Correct |
361 ms |
8312 KB |
Output is correct |
13 |
Correct |
361 ms |
8312 KB |
Output is correct |
14 |
Correct |
361 ms |
8312 KB |
Output is correct |
15 |
Correct |
361 ms |
8312 KB |
Output is correct |
16 |
Correct |
67 ms |
5112 KB |
Output is correct |
17 |
Correct |
72 ms |
5100 KB |
Output is correct |
18 |
Correct |
69 ms |
4984 KB |
Output is correct |
19 |
Correct |
72 ms |
5112 KB |
Output is correct |
20 |
Correct |
69 ms |
4984 KB |
Output is correct |
21 |
Correct |
962 ms |
11640 KB |
Output is correct |
22 |
Correct |
946 ms |
11516 KB |
Output is correct |
23 |
Correct |
920 ms |
11492 KB |
Output is correct |
24 |
Correct |
942 ms |
11512 KB |
Output is correct |
25 |
Correct |
917 ms |
11492 KB |
Output is correct |
26 |
Correct |
974 ms |
11384 KB |
Output is correct |
27 |
Correct |
1010 ms |
11384 KB |
Output is correct |
28 |
Correct |
975 ms |
11384 KB |
Output is correct |
29 |
Correct |
985 ms |
11256 KB |
Output is correct |
30 |
Correct |
685 ms |
11960 KB |
Output is correct |
31 |
Correct |
683 ms |
11896 KB |
Output is correct |
32 |
Correct |
688 ms |
11828 KB |
Output is correct |
33 |
Correct |
687 ms |
11900 KB |
Output is correct |
34 |
Correct |
679 ms |
11792 KB |
Output is correct |
35 |
Correct |
361 ms |
8312 KB |
Output is correct |
36 |
Correct |
361 ms |
8324 KB |
Output is correct |
37 |
Correct |
362 ms |
8440 KB |
Output is correct |
38 |
Correct |
358 ms |
8312 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
2688 KB |
Output is correct |
2 |
Correct |
10 ms |
2688 KB |
Output is correct |
3 |
Correct |
11 ms |
2688 KB |
Output is correct |
4 |
Correct |
10 ms |
2688 KB |
Output is correct |
5 |
Correct |
10 ms |
2688 KB |
Output is correct |
6 |
Correct |
11 ms |
2688 KB |
Output is correct |
7 |
Correct |
12 ms |
2688 KB |
Output is correct |
8 |
Correct |
11 ms |
2728 KB |
Output is correct |
9 |
Correct |
11 ms |
2688 KB |
Output is correct |
10 |
Correct |
13 ms |
2688 KB |
Output is correct |
11 |
Correct |
10 ms |
2688 KB |
Output is correct |
12 |
Correct |
10 ms |
2688 KB |
Output is correct |
13 |
Correct |
10 ms |
2688 KB |
Output is correct |
14 |
Correct |
10 ms |
2688 KB |
Output is correct |
15 |
Correct |
10 ms |
2688 KB |
Output is correct |
16 |
Correct |
5 ms |
1792 KB |
Output is correct |
17 |
Correct |
5 ms |
1792 KB |
Output is correct |
18 |
Correct |
5 ms |
1792 KB |
Output is correct |
19 |
Correct |
5 ms |
1792 KB |
Output is correct |
20 |
Correct |
5 ms |
1924 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
362 ms |
8312 KB |
Output is correct |
2 |
Correct |
362 ms |
8312 KB |
Output is correct |
3 |
Correct |
365 ms |
8312 KB |
Output is correct |
4 |
Correct |
360 ms |
8312 KB |
Output is correct |
5 |
Correct |
361 ms |
8304 KB |
Output is correct |
6 |
Correct |
363 ms |
8312 KB |
Output is correct |
7 |
Correct |
360 ms |
8360 KB |
Output is correct |
8 |
Correct |
364 ms |
8312 KB |
Output is correct |
9 |
Correct |
361 ms |
8312 KB |
Output is correct |
10 |
Correct |
360 ms |
8312 KB |
Output is correct |
11 |
Correct |
361 ms |
8312 KB |
Output is correct |
12 |
Correct |
361 ms |
8312 KB |
Output is correct |
13 |
Correct |
361 ms |
8312 KB |
Output is correct |
14 |
Correct |
361 ms |
8312 KB |
Output is correct |
15 |
Correct |
361 ms |
8312 KB |
Output is correct |
16 |
Correct |
67 ms |
5112 KB |
Output is correct |
17 |
Correct |
72 ms |
5100 KB |
Output is correct |
18 |
Correct |
69 ms |
4984 KB |
Output is correct |
19 |
Correct |
72 ms |
5112 KB |
Output is correct |
20 |
Correct |
69 ms |
4984 KB |
Output is correct |
21 |
Correct |
962 ms |
11640 KB |
Output is correct |
22 |
Correct |
946 ms |
11516 KB |
Output is correct |
23 |
Correct |
920 ms |
11492 KB |
Output is correct |
24 |
Correct |
942 ms |
11512 KB |
Output is correct |
25 |
Correct |
917 ms |
11492 KB |
Output is correct |
26 |
Correct |
974 ms |
11384 KB |
Output is correct |
27 |
Correct |
1010 ms |
11384 KB |
Output is correct |
28 |
Correct |
975 ms |
11384 KB |
Output is correct |
29 |
Correct |
985 ms |
11256 KB |
Output is correct |
30 |
Correct |
685 ms |
11960 KB |
Output is correct |
31 |
Correct |
683 ms |
11896 KB |
Output is correct |
32 |
Correct |
688 ms |
11828 KB |
Output is correct |
33 |
Correct |
687 ms |
11900 KB |
Output is correct |
34 |
Correct |
679 ms |
11792 KB |
Output is correct |
35 |
Correct |
361 ms |
8312 KB |
Output is correct |
36 |
Correct |
361 ms |
8324 KB |
Output is correct |
37 |
Correct |
362 ms |
8440 KB |
Output is correct |
38 |
Correct |
358 ms |
8312 KB |
Output is correct |
39 |
Correct |
10 ms |
2688 KB |
Output is correct |
40 |
Correct |
10 ms |
2688 KB |
Output is correct |
41 |
Correct |
11 ms |
2688 KB |
Output is correct |
42 |
Correct |
10 ms |
2688 KB |
Output is correct |
43 |
Correct |
10 ms |
2688 KB |
Output is correct |
44 |
Correct |
11 ms |
2688 KB |
Output is correct |
45 |
Correct |
12 ms |
2688 KB |
Output is correct |
46 |
Correct |
11 ms |
2728 KB |
Output is correct |
47 |
Correct |
11 ms |
2688 KB |
Output is correct |
48 |
Correct |
13 ms |
2688 KB |
Output is correct |
49 |
Correct |
10 ms |
2688 KB |
Output is correct |
50 |
Correct |
10 ms |
2688 KB |
Output is correct |
51 |
Correct |
10 ms |
2688 KB |
Output is correct |
52 |
Correct |
10 ms |
2688 KB |
Output is correct |
53 |
Correct |
10 ms |
2688 KB |
Output is correct |
54 |
Correct |
5 ms |
1792 KB |
Output is correct |
55 |
Correct |
5 ms |
1792 KB |
Output is correct |
56 |
Correct |
5 ms |
1792 KB |
Output is correct |
57 |
Correct |
5 ms |
1792 KB |
Output is correct |
58 |
Correct |
5 ms |
1924 KB |
Output is correct |
59 |
Correct |
1020 ms |
12456 KB |
Output is correct |
60 |
Correct |
1018 ms |
12280 KB |
Output is correct |
61 |
Correct |
985 ms |
12408 KB |
Output is correct |
62 |
Correct |
1010 ms |
12160 KB |
Output is correct |
63 |
Correct |
1010 ms |
12280 KB |
Output is correct |
64 |
Correct |
1111 ms |
12412 KB |
Output is correct |
65 |
Correct |
1114 ms |
12316 KB |
Output is correct |
66 |
Correct |
1112 ms |
12328 KB |
Output is correct |
67 |
Correct |
1126 ms |
12256 KB |
Output is correct |
68 |
Correct |
1123 ms |
12296 KB |
Output is correct |
69 |
Correct |
939 ms |
12280 KB |
Output is correct |
70 |
Correct |
934 ms |
12280 KB |
Output is correct |
71 |
Correct |
928 ms |
12408 KB |
Output is correct |
72 |
Correct |
936 ms |
12380 KB |
Output is correct |
73 |
Correct |
945 ms |
12204 KB |
Output is correct |
74 |
Correct |
112 ms |
5112 KB |
Output is correct |
75 |
Correct |
107 ms |
5112 KB |
Output is correct |
76 |
Correct |
109 ms |
5112 KB |
Output is correct |
77 |
Correct |
109 ms |
5112 KB |
Output is correct |
78 |
Correct |
111 ms |
5112 KB |
Output is correct |