#include <cstdio>
#include <cstring>
#include <vector>
#include <cstring>
#include <algorithm>
#define X first
#define Y second
#define PB push_back
using namespace std;
typedef long long ll;
const int N = 1005;
const int MOD = 1e9 + 7;
inline int add(const int &A, const int &B){
if(A + B >= MOD) return A + B - MOD;
return A + B;
}
inline int mul(const int &A, const int &B){
return (ll)A * B % MOD;
}
inline int sub(const int &A, const int &B){
if(A - B < 0) return A - B + MOD;
return A - B;
}
inline int eks(const int &A, int B){
int ans = 1, bs = A;
for(;B;B >>= 1){
if(B & 1)
ans = mul(ans, bs);
bs = mul(bs, bs);
}
return ans;
}
int n, inv[N], fak[N];
int ch[N][N], L[N], R[N], b_L[N], b_R[N], m, in[N][N], ch2[N][N], pos[N][N], dp[N][N];
vector < int > v;
void precompute(){
fak[0] = 1; inv[0] = 1;
for(int i = 1;i < N;i++){
fak[i] = mul(i, fak[i - 1]);
inv[i] = eks(fak[i], MOD - 2);
}
for(int i = 0;i < N;i++){
ch[i][i] = 1; ch[i][0] = 1;
}
for(int i = 1;i < N;i++){
for(int j = 1;j < i;j++){
ch[i][j] = add(ch[i - 1][j], ch[i - 1][j - 1]);
}
}
}
int f(int bl, int i){
if(bl == m) return 1;
if(dp[bl][i] != -1) return dp[bl][i];
int ret = f(bl + 1, i);
int cnt = 0;
for(int j = i;j < n;j++){
cnt += in[j][bl];
if(in[j][bl]){
ret = add(ret, mul(f(bl + 1, j + 1), pos[bl][cnt]));
}
}
return dp[bl][i] = ret;
}
int brute(int i,int lst){
if(i == n) return 1;
int ret = brute(i + 1, lst);
for(int j = max(L[i], lst + 1);j <= R[i];j++)
ret += brute(i + 1, j);
return ret;
}
int main(){
memset(dp, -1, sizeof(dp));
precompute();
scanf("%d", &n);
for(int i = 0;i < n;i++){
scanf("%d%d", L + i, R + i);
v.PB(L[i]); v.PB(R[i] + 1);
}
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
m = v.size() - 1;
for(int i = 0;i < m;i++){
b_L[i] = v[i]; b_R[i] = v[i + 1] - 1;
//printf("BLOK %d : %d %d\n", i, b_L[i], b_R[i]);
}
for(int i = 0;i < n;i++){
for(int j = 0;j < m;j++){
if(L[i] <= b_L[j] && b_R[j] <= R[i])
in[i][j] = 1;
}
}
for(int i = 0;i < m;i++){
int len = b_R[i] - b_L[i] + 1;
int cur = len; ch2[i][0] = 1;
for(int j = 1;j <= n;j++){
ch2[i][j] = mul(cur, inv[j]);
//printf("%d povrh %d = %d\n", len, j, ch2[i][j]);
cur = mul(cur, len - j);
}
}
for(int i = 0;i < m;i++){
for(int j = 1;j <= n;j++){
for(int k = 0;k < j;k++){
pos[i][j] = add(pos[i][j], mul(ch2[i][k + 1], ch[j - 1][k]));
}
//printf("pos[%d][%d] = %d\n", i, j, pos[i][j]);
}
}
printf("%d\n", sub(f(0,0), 1));
// printf("%d\n", sub(brute(0,0), 1));
}
Compilation message
boat.cpp: In function 'int main()':
boat.cpp:87:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &n);
~~~~~^~~~~~~~~~
boat.cpp:89:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", L + i, R + i);
~~~~~^~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
430 ms |
17884 KB |
Output is correct |
2 |
Correct |
463 ms |
17912 KB |
Output is correct |
3 |
Correct |
421 ms |
18040 KB |
Output is correct |
4 |
Correct |
438 ms |
17884 KB |
Output is correct |
5 |
Correct |
421 ms |
17912 KB |
Output is correct |
6 |
Correct |
454 ms |
18168 KB |
Output is correct |
7 |
Correct |
462 ms |
18188 KB |
Output is correct |
8 |
Correct |
451 ms |
18200 KB |
Output is correct |
9 |
Correct |
437 ms |
18296 KB |
Output is correct |
10 |
Correct |
437 ms |
18244 KB |
Output is correct |
11 |
Correct |
435 ms |
18172 KB |
Output is correct |
12 |
Correct |
433 ms |
18168 KB |
Output is correct |
13 |
Correct |
459 ms |
18120 KB |
Output is correct |
14 |
Correct |
461 ms |
18212 KB |
Output is correct |
15 |
Correct |
458 ms |
18260 KB |
Output is correct |
16 |
Correct |
87 ms |
11612 KB |
Output is correct |
17 |
Correct |
98 ms |
11768 KB |
Output is correct |
18 |
Correct |
93 ms |
11684 KB |
Output is correct |
19 |
Correct |
92 ms |
11796 KB |
Output is correct |
20 |
Correct |
89 ms |
11768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
430 ms |
17884 KB |
Output is correct |
2 |
Correct |
463 ms |
17912 KB |
Output is correct |
3 |
Correct |
421 ms |
18040 KB |
Output is correct |
4 |
Correct |
438 ms |
17884 KB |
Output is correct |
5 |
Correct |
421 ms |
17912 KB |
Output is correct |
6 |
Correct |
454 ms |
18168 KB |
Output is correct |
7 |
Correct |
462 ms |
18188 KB |
Output is correct |
8 |
Correct |
451 ms |
18200 KB |
Output is correct |
9 |
Correct |
437 ms |
18296 KB |
Output is correct |
10 |
Correct |
437 ms |
18244 KB |
Output is correct |
11 |
Correct |
435 ms |
18172 KB |
Output is correct |
12 |
Correct |
433 ms |
18168 KB |
Output is correct |
13 |
Correct |
459 ms |
18120 KB |
Output is correct |
14 |
Correct |
461 ms |
18212 KB |
Output is correct |
15 |
Correct |
458 ms |
18260 KB |
Output is correct |
16 |
Correct |
87 ms |
11612 KB |
Output is correct |
17 |
Correct |
98 ms |
11768 KB |
Output is correct |
18 |
Correct |
93 ms |
11684 KB |
Output is correct |
19 |
Correct |
92 ms |
11796 KB |
Output is correct |
20 |
Correct |
89 ms |
11768 KB |
Output is correct |
21 |
Correct |
595 ms |
17460 KB |
Output is correct |
22 |
Correct |
584 ms |
17528 KB |
Output is correct |
23 |
Correct |
586 ms |
17436 KB |
Output is correct |
24 |
Correct |
613 ms |
17296 KB |
Output is correct |
25 |
Correct |
629 ms |
17424 KB |
Output is correct |
26 |
Correct |
673 ms |
17384 KB |
Output is correct |
27 |
Correct |
764 ms |
17420 KB |
Output is correct |
28 |
Correct |
647 ms |
17144 KB |
Output is correct |
29 |
Correct |
664 ms |
17304 KB |
Output is correct |
30 |
Correct |
489 ms |
18168 KB |
Output is correct |
31 |
Correct |
476 ms |
18320 KB |
Output is correct |
32 |
Correct |
441 ms |
18296 KB |
Output is correct |
33 |
Correct |
441 ms |
18140 KB |
Output is correct |
34 |
Correct |
457 ms |
18128 KB |
Output is correct |
35 |
Correct |
449 ms |
17788 KB |
Output is correct |
36 |
Correct |
410 ms |
17884 KB |
Output is correct |
37 |
Correct |
422 ms |
17988 KB |
Output is correct |
38 |
Correct |
418 ms |
18068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
10240 KB |
Output is correct |
2 |
Correct |
14 ms |
10240 KB |
Output is correct |
3 |
Correct |
14 ms |
10240 KB |
Output is correct |
4 |
Correct |
15 ms |
10232 KB |
Output is correct |
5 |
Correct |
15 ms |
10240 KB |
Output is correct |
6 |
Correct |
16 ms |
10240 KB |
Output is correct |
7 |
Correct |
15 ms |
10240 KB |
Output is correct |
8 |
Correct |
15 ms |
10240 KB |
Output is correct |
9 |
Correct |
16 ms |
10240 KB |
Output is correct |
10 |
Correct |
16 ms |
10240 KB |
Output is correct |
11 |
Correct |
15 ms |
10240 KB |
Output is correct |
12 |
Correct |
16 ms |
10240 KB |
Output is correct |
13 |
Correct |
15 ms |
10240 KB |
Output is correct |
14 |
Correct |
14 ms |
10240 KB |
Output is correct |
15 |
Correct |
15 ms |
10240 KB |
Output is correct |
16 |
Correct |
12 ms |
9600 KB |
Output is correct |
17 |
Correct |
12 ms |
9472 KB |
Output is correct |
18 |
Correct |
12 ms |
9600 KB |
Output is correct |
19 |
Correct |
12 ms |
9600 KB |
Output is correct |
20 |
Correct |
13 ms |
9472 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
430 ms |
17884 KB |
Output is correct |
2 |
Correct |
463 ms |
17912 KB |
Output is correct |
3 |
Correct |
421 ms |
18040 KB |
Output is correct |
4 |
Correct |
438 ms |
17884 KB |
Output is correct |
5 |
Correct |
421 ms |
17912 KB |
Output is correct |
6 |
Correct |
454 ms |
18168 KB |
Output is correct |
7 |
Correct |
462 ms |
18188 KB |
Output is correct |
8 |
Correct |
451 ms |
18200 KB |
Output is correct |
9 |
Correct |
437 ms |
18296 KB |
Output is correct |
10 |
Correct |
437 ms |
18244 KB |
Output is correct |
11 |
Correct |
435 ms |
18172 KB |
Output is correct |
12 |
Correct |
433 ms |
18168 KB |
Output is correct |
13 |
Correct |
459 ms |
18120 KB |
Output is correct |
14 |
Correct |
461 ms |
18212 KB |
Output is correct |
15 |
Correct |
458 ms |
18260 KB |
Output is correct |
16 |
Correct |
87 ms |
11612 KB |
Output is correct |
17 |
Correct |
98 ms |
11768 KB |
Output is correct |
18 |
Correct |
93 ms |
11684 KB |
Output is correct |
19 |
Correct |
92 ms |
11796 KB |
Output is correct |
20 |
Correct |
89 ms |
11768 KB |
Output is correct |
21 |
Correct |
595 ms |
17460 KB |
Output is correct |
22 |
Correct |
584 ms |
17528 KB |
Output is correct |
23 |
Correct |
586 ms |
17436 KB |
Output is correct |
24 |
Correct |
613 ms |
17296 KB |
Output is correct |
25 |
Correct |
629 ms |
17424 KB |
Output is correct |
26 |
Correct |
673 ms |
17384 KB |
Output is correct |
27 |
Correct |
764 ms |
17420 KB |
Output is correct |
28 |
Correct |
647 ms |
17144 KB |
Output is correct |
29 |
Correct |
664 ms |
17304 KB |
Output is correct |
30 |
Correct |
489 ms |
18168 KB |
Output is correct |
31 |
Correct |
476 ms |
18320 KB |
Output is correct |
32 |
Correct |
441 ms |
18296 KB |
Output is correct |
33 |
Correct |
441 ms |
18140 KB |
Output is correct |
34 |
Correct |
457 ms |
18128 KB |
Output is correct |
35 |
Correct |
449 ms |
17788 KB |
Output is correct |
36 |
Correct |
410 ms |
17884 KB |
Output is correct |
37 |
Correct |
422 ms |
17988 KB |
Output is correct |
38 |
Correct |
418 ms |
18068 KB |
Output is correct |
39 |
Correct |
14 ms |
10240 KB |
Output is correct |
40 |
Correct |
14 ms |
10240 KB |
Output is correct |
41 |
Correct |
14 ms |
10240 KB |
Output is correct |
42 |
Correct |
15 ms |
10232 KB |
Output is correct |
43 |
Correct |
15 ms |
10240 KB |
Output is correct |
44 |
Correct |
16 ms |
10240 KB |
Output is correct |
45 |
Correct |
15 ms |
10240 KB |
Output is correct |
46 |
Correct |
15 ms |
10240 KB |
Output is correct |
47 |
Correct |
16 ms |
10240 KB |
Output is correct |
48 |
Correct |
16 ms |
10240 KB |
Output is correct |
49 |
Correct |
15 ms |
10240 KB |
Output is correct |
50 |
Correct |
16 ms |
10240 KB |
Output is correct |
51 |
Correct |
15 ms |
10240 KB |
Output is correct |
52 |
Correct |
14 ms |
10240 KB |
Output is correct |
53 |
Correct |
15 ms |
10240 KB |
Output is correct |
54 |
Correct |
12 ms |
9600 KB |
Output is correct |
55 |
Correct |
12 ms |
9472 KB |
Output is correct |
56 |
Correct |
12 ms |
9600 KB |
Output is correct |
57 |
Correct |
12 ms |
9600 KB |
Output is correct |
58 |
Correct |
13 ms |
9472 KB |
Output is correct |
59 |
Correct |
714 ms |
18156 KB |
Output is correct |
60 |
Correct |
742 ms |
18164 KB |
Output is correct |
61 |
Correct |
719 ms |
18296 KB |
Output is correct |
62 |
Correct |
724 ms |
18188 KB |
Output is correct |
63 |
Correct |
725 ms |
18044 KB |
Output is correct |
64 |
Correct |
797 ms |
18272 KB |
Output is correct |
65 |
Correct |
816 ms |
18392 KB |
Output is correct |
66 |
Correct |
800 ms |
18316 KB |
Output is correct |
67 |
Correct |
816 ms |
18220 KB |
Output is correct |
68 |
Correct |
788 ms |
18296 KB |
Output is correct |
69 |
Correct |
804 ms |
18268 KB |
Output is correct |
70 |
Correct |
741 ms |
18396 KB |
Output is correct |
71 |
Correct |
765 ms |
18280 KB |
Output is correct |
72 |
Correct |
804 ms |
18340 KB |
Output is correct |
73 |
Correct |
761 ms |
18332 KB |
Output is correct |
74 |
Correct |
133 ms |
11640 KB |
Output is correct |
75 |
Correct |
133 ms |
11716 KB |
Output is correct |
76 |
Correct |
135 ms |
11768 KB |
Output is correct |
77 |
Correct |
128 ms |
11704 KB |
Output is correct |
78 |
Correct |
128 ms |
11768 KB |
Output is correct |