#include <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
using namespace std;
typedef long long ll;
const int mod = 1e9 + 7;
void add(int & a, int b)
{
a += b;
if (a >= mod) a -= mod;
if (a < 0) a += mod;
}
int mul(int a, int b)
{
return 1ll * a * b % mod;
}
int power(int a, int b)
{
if (b == 0) return 1;
int res = power(a, b / 2);
if (b & 1) return 1ll * res * res % mod * a % mod;
return 1ll * res * res % mod;
}
int f[505][1005];
int N, a[505], b[505], inv[505], fac[505];
int C(int k, int n)
{
if (k > n) return 0;
return fac[n] * inv[k] % mod * inv[n - k] % mod;
}
signed main(void)
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
if (fopen("A.INP", "r")){
freopen("A.INP", "r", stdin);
freopen("A.OUT", "w", stdout);
}
cin >> N;
fac[0] = 1;
for (int i = 1; i < 505; ++i){
inv[i] = power(i, mod - 2);
fac[i] = mul(fac[i - 1], i);
}
vector<int> comp;
for (int i = 1; i <= N; ++i){
cin >> a[i] >> b[i];
comp.pb(a[i] - 1); comp.pb(b[i]);
}
sort(comp.begin(), comp.end()); comp.erase(unique(comp.begin(), comp.end()), comp.end());
for (int i = 0; i < (int) comp.size(); ++i){
f[0][i] = 1;
}
for (int i = 1; i <= N; ++i){
a[i] = lower_bound(comp.begin(), comp.end(), a[i]) - comp.begin();
b[i] = lower_bound(comp.begin(), comp.end(), b[i]) - comp.begin();
}
for(int i = 1 ; i <= N ; ++i){
assert(a[i]);
f[i][0] = 1;
for(int j = a[i] ; j <= b[i] ; ++j) {
f[i][j] = mul(f[i - 1][j - 1],comp[j] - comp[j - 1]);
int cnt = 1;
int Max = comp[j] - comp[j - 1];
int way = Max - 1;
for(int k = i - 1 ; k ; --k){
if (a[k] <= j && j <= b[k]) {
way = mul(way,Max++);
way = mul(way,inv[++cnt]);
add(f[i][j],mul(f[k - 1][j - 1],way));
}
}
}
for(int j = 1 ; j < comp.size() ; ++j){
add(f[i][j],f[i - 1][j]),
add(f[i][j],f[i][j - 1]),
add(f[i][j],-f[i - 1][j - 1]);
}
}
add(f[N][int(comp.size()) - 1], -1);
cout << f[N][int(comp.size()) - 1];
}
Compilation message
boat.cpp: In function 'int main()':
boat.cpp:84:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j = 1 ; j < comp.size() ; ++j){
~~^~~~~~~~~~~~~
boat.cpp:46:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
freopen("A.INP", "r", stdin);
~~~~~~~^~~~~~~~~~~~~~~~~~~~~
boat.cpp:47:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
freopen("A.OUT", "w", stdout);
~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
2296 KB |
Output is correct |
2 |
Correct |
13 ms |
2452 KB |
Output is correct |
3 |
Correct |
8 ms |
2296 KB |
Output is correct |
4 |
Correct |
7 ms |
2296 KB |
Output is correct |
5 |
Correct |
7 ms |
2296 KB |
Output is correct |
6 |
Correct |
7 ms |
2296 KB |
Output is correct |
7 |
Correct |
7 ms |
2296 KB |
Output is correct |
8 |
Correct |
8 ms |
2296 KB |
Output is correct |
9 |
Correct |
7 ms |
2424 KB |
Output is correct |
10 |
Correct |
7 ms |
2300 KB |
Output is correct |
11 |
Correct |
7 ms |
2296 KB |
Output is correct |
12 |
Correct |
7 ms |
2296 KB |
Output is correct |
13 |
Correct |
7 ms |
2296 KB |
Output is correct |
14 |
Correct |
14 ms |
2296 KB |
Output is correct |
15 |
Correct |
6 ms |
2296 KB |
Output is correct |
16 |
Correct |
5 ms |
2296 KB |
Output is correct |
17 |
Correct |
6 ms |
2296 KB |
Output is correct |
18 |
Correct |
3 ms |
2168 KB |
Output is correct |
19 |
Correct |
5 ms |
2296 KB |
Output is correct |
20 |
Correct |
5 ms |
2296 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
2296 KB |
Output is correct |
2 |
Correct |
13 ms |
2452 KB |
Output is correct |
3 |
Correct |
8 ms |
2296 KB |
Output is correct |
4 |
Correct |
7 ms |
2296 KB |
Output is correct |
5 |
Correct |
7 ms |
2296 KB |
Output is correct |
6 |
Correct |
7 ms |
2296 KB |
Output is correct |
7 |
Correct |
7 ms |
2296 KB |
Output is correct |
8 |
Correct |
8 ms |
2296 KB |
Output is correct |
9 |
Correct |
7 ms |
2424 KB |
Output is correct |
10 |
Correct |
7 ms |
2300 KB |
Output is correct |
11 |
Correct |
7 ms |
2296 KB |
Output is correct |
12 |
Correct |
7 ms |
2296 KB |
Output is correct |
13 |
Correct |
7 ms |
2296 KB |
Output is correct |
14 |
Correct |
14 ms |
2296 KB |
Output is correct |
15 |
Correct |
6 ms |
2296 KB |
Output is correct |
16 |
Correct |
5 ms |
2296 KB |
Output is correct |
17 |
Correct |
6 ms |
2296 KB |
Output is correct |
18 |
Correct |
3 ms |
2168 KB |
Output is correct |
19 |
Correct |
5 ms |
2296 KB |
Output is correct |
20 |
Correct |
5 ms |
2296 KB |
Output is correct |
21 |
Correct |
447 ms |
2372 KB |
Output is correct |
22 |
Correct |
431 ms |
2532 KB |
Output is correct |
23 |
Correct |
392 ms |
2420 KB |
Output is correct |
24 |
Correct |
429 ms |
2292 KB |
Output is correct |
25 |
Correct |
468 ms |
2460 KB |
Output is correct |
26 |
Correct |
737 ms |
2272 KB |
Output is correct |
27 |
Correct |
774 ms |
2448 KB |
Output is correct |
28 |
Correct |
710 ms |
2552 KB |
Output is correct |
29 |
Correct |
705 ms |
2232 KB |
Output is correct |
30 |
Correct |
8 ms |
2424 KB |
Output is correct |
31 |
Correct |
8 ms |
2296 KB |
Output is correct |
32 |
Correct |
8 ms |
2296 KB |
Output is correct |
33 |
Correct |
8 ms |
2296 KB |
Output is correct |
34 |
Correct |
8 ms |
2300 KB |
Output is correct |
35 |
Correct |
9 ms |
2424 KB |
Output is correct |
36 |
Correct |
10 ms |
2300 KB |
Output is correct |
37 |
Correct |
10 ms |
2296 KB |
Output is correct |
38 |
Correct |
9 ms |
2296 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
760 KB |
Output is correct |
2 |
Correct |
5 ms |
760 KB |
Output is correct |
3 |
Correct |
6 ms |
760 KB |
Output is correct |
4 |
Correct |
6 ms |
760 KB |
Output is correct |
5 |
Correct |
7 ms |
760 KB |
Output is correct |
6 |
Correct |
10 ms |
760 KB |
Output is correct |
7 |
Correct |
10 ms |
736 KB |
Output is correct |
8 |
Correct |
9 ms |
720 KB |
Output is correct |
9 |
Correct |
10 ms |
760 KB |
Output is correct |
10 |
Correct |
11 ms |
732 KB |
Output is correct |
11 |
Correct |
6 ms |
760 KB |
Output is correct |
12 |
Correct |
5 ms |
760 KB |
Output is correct |
13 |
Correct |
5 ms |
760 KB |
Output is correct |
14 |
Correct |
6 ms |
760 KB |
Output is correct |
15 |
Correct |
6 ms |
760 KB |
Output is correct |
16 |
Correct |
5 ms |
760 KB |
Output is correct |
17 |
Correct |
5 ms |
760 KB |
Output is correct |
18 |
Correct |
3 ms |
760 KB |
Output is correct |
19 |
Correct |
4 ms |
764 KB |
Output is correct |
20 |
Correct |
5 ms |
760 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
2296 KB |
Output is correct |
2 |
Correct |
13 ms |
2452 KB |
Output is correct |
3 |
Correct |
8 ms |
2296 KB |
Output is correct |
4 |
Correct |
7 ms |
2296 KB |
Output is correct |
5 |
Correct |
7 ms |
2296 KB |
Output is correct |
6 |
Correct |
7 ms |
2296 KB |
Output is correct |
7 |
Correct |
7 ms |
2296 KB |
Output is correct |
8 |
Correct |
8 ms |
2296 KB |
Output is correct |
9 |
Correct |
7 ms |
2424 KB |
Output is correct |
10 |
Correct |
7 ms |
2300 KB |
Output is correct |
11 |
Correct |
7 ms |
2296 KB |
Output is correct |
12 |
Correct |
7 ms |
2296 KB |
Output is correct |
13 |
Correct |
7 ms |
2296 KB |
Output is correct |
14 |
Correct |
14 ms |
2296 KB |
Output is correct |
15 |
Correct |
6 ms |
2296 KB |
Output is correct |
16 |
Correct |
5 ms |
2296 KB |
Output is correct |
17 |
Correct |
6 ms |
2296 KB |
Output is correct |
18 |
Correct |
3 ms |
2168 KB |
Output is correct |
19 |
Correct |
5 ms |
2296 KB |
Output is correct |
20 |
Correct |
5 ms |
2296 KB |
Output is correct |
21 |
Correct |
447 ms |
2372 KB |
Output is correct |
22 |
Correct |
431 ms |
2532 KB |
Output is correct |
23 |
Correct |
392 ms |
2420 KB |
Output is correct |
24 |
Correct |
429 ms |
2292 KB |
Output is correct |
25 |
Correct |
468 ms |
2460 KB |
Output is correct |
26 |
Correct |
737 ms |
2272 KB |
Output is correct |
27 |
Correct |
774 ms |
2448 KB |
Output is correct |
28 |
Correct |
710 ms |
2552 KB |
Output is correct |
29 |
Correct |
705 ms |
2232 KB |
Output is correct |
30 |
Correct |
8 ms |
2424 KB |
Output is correct |
31 |
Correct |
8 ms |
2296 KB |
Output is correct |
32 |
Correct |
8 ms |
2296 KB |
Output is correct |
33 |
Correct |
8 ms |
2296 KB |
Output is correct |
34 |
Correct |
8 ms |
2300 KB |
Output is correct |
35 |
Correct |
9 ms |
2424 KB |
Output is correct |
36 |
Correct |
10 ms |
2300 KB |
Output is correct |
37 |
Correct |
10 ms |
2296 KB |
Output is correct |
38 |
Correct |
9 ms |
2296 KB |
Output is correct |
39 |
Correct |
7 ms |
760 KB |
Output is correct |
40 |
Correct |
5 ms |
760 KB |
Output is correct |
41 |
Correct |
6 ms |
760 KB |
Output is correct |
42 |
Correct |
6 ms |
760 KB |
Output is correct |
43 |
Correct |
7 ms |
760 KB |
Output is correct |
44 |
Correct |
10 ms |
760 KB |
Output is correct |
45 |
Correct |
10 ms |
736 KB |
Output is correct |
46 |
Correct |
9 ms |
720 KB |
Output is correct |
47 |
Correct |
10 ms |
760 KB |
Output is correct |
48 |
Correct |
11 ms |
732 KB |
Output is correct |
49 |
Correct |
6 ms |
760 KB |
Output is correct |
50 |
Correct |
5 ms |
760 KB |
Output is correct |
51 |
Correct |
5 ms |
760 KB |
Output is correct |
52 |
Correct |
6 ms |
760 KB |
Output is correct |
53 |
Correct |
6 ms |
760 KB |
Output is correct |
54 |
Correct |
5 ms |
760 KB |
Output is correct |
55 |
Correct |
5 ms |
760 KB |
Output is correct |
56 |
Correct |
3 ms |
760 KB |
Output is correct |
57 |
Correct |
4 ms |
764 KB |
Output is correct |
58 |
Correct |
5 ms |
760 KB |
Output is correct |
59 |
Correct |
493 ms |
2484 KB |
Output is correct |
60 |
Correct |
454 ms |
2424 KB |
Output is correct |
61 |
Correct |
449 ms |
2432 KB |
Output is correct |
62 |
Correct |
574 ms |
2480 KB |
Output is correct |
63 |
Correct |
562 ms |
2340 KB |
Output is correct |
64 |
Correct |
848 ms |
2432 KB |
Output is correct |
65 |
Correct |
863 ms |
2588 KB |
Output is correct |
66 |
Correct |
873 ms |
2608 KB |
Output is correct |
67 |
Correct |
863 ms |
2260 KB |
Output is correct |
68 |
Correct |
859 ms |
2420 KB |
Output is correct |
69 |
Correct |
382 ms |
2392 KB |
Output is correct |
70 |
Correct |
386 ms |
2388 KB |
Output is correct |
71 |
Correct |
391 ms |
2452 KB |
Output is correct |
72 |
Correct |
408 ms |
2504 KB |
Output is correct |
73 |
Correct |
408 ms |
2332 KB |
Output is correct |
74 |
Correct |
88 ms |
2296 KB |
Output is correct |
75 |
Correct |
83 ms |
2296 KB |
Output is correct |
76 |
Correct |
88 ms |
2424 KB |
Output is correct |
77 |
Correct |
88 ms |
2300 KB |
Output is correct |
78 |
Correct |
91 ms |
2424 KB |
Output is correct |