// Om Namah Shivaya
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template<typename T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long int ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL)
#define pb push_back
#define endl '\n'
#define sz(a) a.size()
#define setbits(x) __builtin_popcountll(x)
#define ff first
#define ss second
#define conts continue
#define ceil2(x, y) ((x + y - 1) / (y))
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define yes cout << "Yes" << endl
#define no cout << "No" << endl
#define rep(i, n) for(int i = 0; i < n; ++i)
#define rep1(i, n) for(int i = 1; i <= n; ++i)
#define rev(i, s, e) for(int i = s; i >= e; --i)
#define trav(i, a) for(auto &i : a)
template<typename T>
void amin(T &a, T b) {
a = min(a, b);
}
template<typename T>
void amax(T &a, T b) {
a = max(a, b);
}
#ifdef LOCAL
#include "debug.h"
#else
#define debug(x) 42
#endif
/*
*/
const int MOD = 1e9 + 7;
const int N = 500 + 5;
const int inf1 = int(1e9) + 5;
const ll inf2 = ll(1e18) + 5;
ll fact[N], ifact[N];
ll bexp(ll a, ll b) {
a %= MOD;
if (a == 0) return 0;
ll res = 1;
while (b) {
if (b & 1) res = res * a % MOD;
a = a * a % MOD;
b >>= 1;
}
return res;
}
ll invmod(ll a) {
return bexp(a, MOD - 2);
}
ll ncr(ll n, ll r) {
if (n < 0 or r < 0 or n < r) return 0;
return fact[n] * ifact[r] % MOD * ifact[n - r] % MOD;
}
ll npr(ll n, ll r) {
if (n < 0 or r < 0 or n < r) return 0;
return fact[n] * ifact[n - r] % MOD;
}
void precalc(ll n) {
fact[0] = 1;
rep1(i, n) fact[i] = fact[i - 1] * i % MOD;
ifact[n] = invmod(fact[n]);
rev(i, n - 1, 0) ifact[i] = ifact[i + 1] * (i + 1) % MOD;
}
ll choose[N*4][N], choose_ways[N*4][N], dp[N][N*4];
void solve(int test_case)
{
precalc(N-1);
ll n; cin >> n;
vector<pll> a(n+5);
rep1(i,n) cin >> a[i].ff >> a[i].ss;
vector<ll> b;
rep1(i,n) b.pb(a[i].ff), b.pb(a[i].ss);
sort(all(b));
b.resize(unique(all(b))-b.begin());
vector<pll> ranges;
ranges.pb({0,0});
rep(i,sz(b)){
ranges.pb({b[i],b[i]});
if(i+1 < sz(b)){
if(b[i]+1 <= b[i+1]-1){
ranges.pb({b[i]+1,b[i+1]-1});
}
}
}
ll m = sz(ranges);
// ll choose[m+5][n+5];
// memset(choose,0,sizeof choose);
rep(i,m){
auto [l,r] = ranges[i];
ll len = r-l+1;
choose[i][0] = 1;
ll curr = 1;
rep1(r,n){
if(len < r) break;
curr = curr * (len-r+1) % MOD;
choose[i][r] = curr * ifact[r] % MOD;
}
}
auto isect = [&](pll &p1, pll &p2){
return max(p1.ff,p2.ff) <= min(p1.ss,p2.ss);
};
// ll choose_ways[m+5][n+5];
// memset(choose_ways,0,sizeof choose_ways);
rep(i,m){
choose_ways[i][0] = 1;
choose_ways[i][1] = choose[i][1];
for(int j = 2; j <= n; ++j){
// endpoints fixed i.e 2 of these j guys are always chosen
// decide pick/ignore for rest j-2 guys
rep(more,j-2+1){
ll ways = ncr(j-2,more)*choose[i][more+2];
choose_ways[i][j] += ways;
choose_ways[i][j] %= MOD;
}
}
}
// ll dp[n+5][m+5];
// memset(dp,0,sizeof dp);
rep(j,m) dp[0][j] = 1;
rep1(i,n){
dp[i][0] = 1;
rep1(j,m-1){
dp[i][j] = (dp[i-1][j]+dp[i][j-1]-dp[i-1][j-1]+MOD)%MOD;
if(!isect(a[i],ranges[j])) conts;
dp[i][j] += choose[j][1]*dp[i-1][j-1];
dp[i][j] %= MOD;
ll good = 1;
rev(k,i-1,1){
// i and k fixed
// few more in between
if(isect(a[k],ranges[j])){
good++;
dp[i][j] += choose_ways[j][good]*dp[k-1][j-1];
dp[i][j] %= MOD;
}
}
}
}
ll ans = (dp[n][m-1]-1+MOD)%MOD;
cout << ans << endl;
}
int main()
{
fastio;
int t = 1;
// cin >> t;
rep1(i, t) {
solve(i);
}
return 0;
}
Compilation message
boat.cpp: In function 'void solve(int)':
boat.cpp:30:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
30 | #define rep(i, n) for(int i = 0; i < n; ++i)
| ^
boat.cpp:118:5: note: in expansion of macro 'rep'
118 | rep(i,sz(b)){
| ^~~
boat.cpp:120:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
120 | if(i+1 < sz(b)){
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
657 ms |
14124 KB |
Output is correct |
2 |
Correct |
624 ms |
14084 KB |
Output is correct |
3 |
Correct |
624 ms |
14104 KB |
Output is correct |
4 |
Correct |
628 ms |
14152 KB |
Output is correct |
5 |
Correct |
654 ms |
14092 KB |
Output is correct |
6 |
Correct |
620 ms |
14108 KB |
Output is correct |
7 |
Correct |
636 ms |
14200 KB |
Output is correct |
8 |
Correct |
733 ms |
14268 KB |
Output is correct |
9 |
Correct |
623 ms |
14148 KB |
Output is correct |
10 |
Correct |
613 ms |
14120 KB |
Output is correct |
11 |
Correct |
705 ms |
14080 KB |
Output is correct |
12 |
Correct |
725 ms |
14076 KB |
Output is correct |
13 |
Correct |
753 ms |
14104 KB |
Output is correct |
14 |
Correct |
653 ms |
14076 KB |
Output is correct |
15 |
Correct |
616 ms |
14152 KB |
Output is correct |
16 |
Correct |
114 ms |
4300 KB |
Output is correct |
17 |
Correct |
130 ms |
4560 KB |
Output is correct |
18 |
Correct |
113 ms |
4404 KB |
Output is correct |
19 |
Correct |
120 ms |
4552 KB |
Output is correct |
20 |
Correct |
114 ms |
4504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
657 ms |
14124 KB |
Output is correct |
2 |
Correct |
624 ms |
14084 KB |
Output is correct |
3 |
Correct |
624 ms |
14104 KB |
Output is correct |
4 |
Correct |
628 ms |
14152 KB |
Output is correct |
5 |
Correct |
654 ms |
14092 KB |
Output is correct |
6 |
Correct |
620 ms |
14108 KB |
Output is correct |
7 |
Correct |
636 ms |
14200 KB |
Output is correct |
8 |
Correct |
733 ms |
14268 KB |
Output is correct |
9 |
Correct |
623 ms |
14148 KB |
Output is correct |
10 |
Correct |
613 ms |
14120 KB |
Output is correct |
11 |
Correct |
705 ms |
14080 KB |
Output is correct |
12 |
Correct |
725 ms |
14076 KB |
Output is correct |
13 |
Correct |
753 ms |
14104 KB |
Output is correct |
14 |
Correct |
653 ms |
14076 KB |
Output is correct |
15 |
Correct |
616 ms |
14152 KB |
Output is correct |
16 |
Correct |
114 ms |
4300 KB |
Output is correct |
17 |
Correct |
130 ms |
4560 KB |
Output is correct |
18 |
Correct |
113 ms |
4404 KB |
Output is correct |
19 |
Correct |
120 ms |
4552 KB |
Output is correct |
20 |
Correct |
114 ms |
4504 KB |
Output is correct |
21 |
Correct |
1197 ms |
21484 KB |
Output is correct |
22 |
Correct |
1190 ms |
21532 KB |
Output is correct |
23 |
Correct |
1211 ms |
21288 KB |
Output is correct |
24 |
Correct |
1226 ms |
21476 KB |
Output is correct |
25 |
Correct |
1181 ms |
21200 KB |
Output is correct |
26 |
Correct |
1353 ms |
20644 KB |
Output is correct |
27 |
Correct |
1445 ms |
20740 KB |
Output is correct |
28 |
Correct |
1291 ms |
20712 KB |
Output is correct |
29 |
Correct |
1300 ms |
20768 KB |
Output is correct |
30 |
Correct |
1181 ms |
22996 KB |
Output is correct |
31 |
Correct |
1139 ms |
22804 KB |
Output is correct |
32 |
Correct |
1141 ms |
22888 KB |
Output is correct |
33 |
Correct |
1158 ms |
22888 KB |
Output is correct |
34 |
Correct |
1133 ms |
22784 KB |
Output is correct |
35 |
Correct |
638 ms |
14080 KB |
Output is correct |
36 |
Correct |
674 ms |
14228 KB |
Output is correct |
37 |
Correct |
619 ms |
14160 KB |
Output is correct |
38 |
Correct |
632 ms |
14072 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
4180 KB |
Output is correct |
2 |
Correct |
17 ms |
4180 KB |
Output is correct |
3 |
Correct |
17 ms |
4152 KB |
Output is correct |
4 |
Correct |
18 ms |
4140 KB |
Output is correct |
5 |
Correct |
18 ms |
4116 KB |
Output is correct |
6 |
Correct |
14 ms |
4156 KB |
Output is correct |
7 |
Correct |
18 ms |
4152 KB |
Output is correct |
8 |
Correct |
14 ms |
4180 KB |
Output is correct |
9 |
Correct |
20 ms |
4200 KB |
Output is correct |
10 |
Correct |
19 ms |
4180 KB |
Output is correct |
11 |
Correct |
17 ms |
4180 KB |
Output is correct |
12 |
Correct |
17 ms |
4140 KB |
Output is correct |
13 |
Correct |
17 ms |
4176 KB |
Output is correct |
14 |
Correct |
14 ms |
4212 KB |
Output is correct |
15 |
Correct |
18 ms |
4164 KB |
Output is correct |
16 |
Correct |
8 ms |
2192 KB |
Output is correct |
17 |
Correct |
6 ms |
2132 KB |
Output is correct |
18 |
Correct |
7 ms |
2228 KB |
Output is correct |
19 |
Correct |
8 ms |
2132 KB |
Output is correct |
20 |
Correct |
6 ms |
2260 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
657 ms |
14124 KB |
Output is correct |
2 |
Correct |
624 ms |
14084 KB |
Output is correct |
3 |
Correct |
624 ms |
14104 KB |
Output is correct |
4 |
Correct |
628 ms |
14152 KB |
Output is correct |
5 |
Correct |
654 ms |
14092 KB |
Output is correct |
6 |
Correct |
620 ms |
14108 KB |
Output is correct |
7 |
Correct |
636 ms |
14200 KB |
Output is correct |
8 |
Correct |
733 ms |
14268 KB |
Output is correct |
9 |
Correct |
623 ms |
14148 KB |
Output is correct |
10 |
Correct |
613 ms |
14120 KB |
Output is correct |
11 |
Correct |
705 ms |
14080 KB |
Output is correct |
12 |
Correct |
725 ms |
14076 KB |
Output is correct |
13 |
Correct |
753 ms |
14104 KB |
Output is correct |
14 |
Correct |
653 ms |
14076 KB |
Output is correct |
15 |
Correct |
616 ms |
14152 KB |
Output is correct |
16 |
Correct |
114 ms |
4300 KB |
Output is correct |
17 |
Correct |
130 ms |
4560 KB |
Output is correct |
18 |
Correct |
113 ms |
4404 KB |
Output is correct |
19 |
Correct |
120 ms |
4552 KB |
Output is correct |
20 |
Correct |
114 ms |
4504 KB |
Output is correct |
21 |
Correct |
1197 ms |
21484 KB |
Output is correct |
22 |
Correct |
1190 ms |
21532 KB |
Output is correct |
23 |
Correct |
1211 ms |
21288 KB |
Output is correct |
24 |
Correct |
1226 ms |
21476 KB |
Output is correct |
25 |
Correct |
1181 ms |
21200 KB |
Output is correct |
26 |
Correct |
1353 ms |
20644 KB |
Output is correct |
27 |
Correct |
1445 ms |
20740 KB |
Output is correct |
28 |
Correct |
1291 ms |
20712 KB |
Output is correct |
29 |
Correct |
1300 ms |
20768 KB |
Output is correct |
30 |
Correct |
1181 ms |
22996 KB |
Output is correct |
31 |
Correct |
1139 ms |
22804 KB |
Output is correct |
32 |
Correct |
1141 ms |
22888 KB |
Output is correct |
33 |
Correct |
1158 ms |
22888 KB |
Output is correct |
34 |
Correct |
1133 ms |
22784 KB |
Output is correct |
35 |
Correct |
638 ms |
14080 KB |
Output is correct |
36 |
Correct |
674 ms |
14228 KB |
Output is correct |
37 |
Correct |
619 ms |
14160 KB |
Output is correct |
38 |
Correct |
632 ms |
14072 KB |
Output is correct |
39 |
Correct |
13 ms |
4180 KB |
Output is correct |
40 |
Correct |
17 ms |
4180 KB |
Output is correct |
41 |
Correct |
17 ms |
4152 KB |
Output is correct |
42 |
Correct |
18 ms |
4140 KB |
Output is correct |
43 |
Correct |
18 ms |
4116 KB |
Output is correct |
44 |
Correct |
14 ms |
4156 KB |
Output is correct |
45 |
Correct |
18 ms |
4152 KB |
Output is correct |
46 |
Correct |
14 ms |
4180 KB |
Output is correct |
47 |
Correct |
20 ms |
4200 KB |
Output is correct |
48 |
Correct |
19 ms |
4180 KB |
Output is correct |
49 |
Correct |
17 ms |
4180 KB |
Output is correct |
50 |
Correct |
17 ms |
4140 KB |
Output is correct |
51 |
Correct |
17 ms |
4176 KB |
Output is correct |
52 |
Correct |
14 ms |
4212 KB |
Output is correct |
53 |
Correct |
18 ms |
4164 KB |
Output is correct |
54 |
Correct |
8 ms |
2192 KB |
Output is correct |
55 |
Correct |
6 ms |
2132 KB |
Output is correct |
56 |
Correct |
7 ms |
2228 KB |
Output is correct |
57 |
Correct |
8 ms |
2132 KB |
Output is correct |
58 |
Correct |
6 ms |
2260 KB |
Output is correct |
59 |
Correct |
1486 ms |
24112 KB |
Output is correct |
60 |
Correct |
1716 ms |
24000 KB |
Output is correct |
61 |
Correct |
1472 ms |
24024 KB |
Output is correct |
62 |
Correct |
1486 ms |
24044 KB |
Output is correct |
63 |
Correct |
1452 ms |
23992 KB |
Output is correct |
64 |
Correct |
1747 ms |
24064 KB |
Output is correct |
65 |
Correct |
1649 ms |
24104 KB |
Output is correct |
66 |
Correct |
1701 ms |
24032 KB |
Output is correct |
67 |
Correct |
1798 ms |
24060 KB |
Output is correct |
68 |
Correct |
1694 ms |
24168 KB |
Output is correct |
69 |
Correct |
1529 ms |
24020 KB |
Output is correct |
70 |
Correct |
1611 ms |
24032 KB |
Output is correct |
71 |
Correct |
1481 ms |
24052 KB |
Output is correct |
72 |
Correct |
1438 ms |
23996 KB |
Output is correct |
73 |
Correct |
1497 ms |
24048 KB |
Output is correct |
74 |
Correct |
154 ms |
4740 KB |
Output is correct |
75 |
Correct |
164 ms |
4724 KB |
Output is correct |
76 |
Correct |
158 ms |
4640 KB |
Output is correct |
77 |
Correct |
162 ms |
4620 KB |
Output is correct |
78 |
Correct |
153 ms |
4656 KB |
Output is correct |