#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define sz(a) (int)(a.size())
#define all(a) a.begin(),a.end()
#define lb lower_bound
#define ub upper_bound
#define owo ios_base::sync_with_stdio(0);cin.tie(0);
#define MOD (ll)(1e9+7)
#define INF (ll)(1e18)
#define debug(...) fprintf(stderr, __VA_ARGS__),fflush(stderr)
#define time__(d) for(long blockTime = 0; (blockTime == 0 ? (blockTime=clock()) != 0 : false);\
debug("%s time : %.4fs\n", d, (double)(clock() - blockTime) / CLOCKS_PER_SEC))
typedef long long int ll;
typedef long double ld;
typedef pair<ll,ll> PII;
typedef pair<int,int> pii;
typedef vector<vector<int>> vii;
typedef vector<vector<ll>> VII;
ll gcd(ll a,ll b){if(!b)return a;else return gcd(b,a%b);}
ll A[550];
ll B[550];
ll len[1050];
ll inv[551];
ll dp[550][1050];
int main()
{
inv[1] = 1;
for (int i=2;i<550;i++) inv[i] = (ll)inv[MOD % i] * (MOD - MOD/i) % MOD;
int n;
cin>>n;
vector<ll>coords;
for(int i=1;i<=n;i++){
cin>>A[i];coords.pb(A[i]);
cin>>B[i];coords.pb(B[i]+1);
}
sort(all(coords));
coords.resize(unique(all(coords))-coords.begin());
for(int i=1;i<=n;i++){
A[i] = ub(all(coords),A[i])-coords.begin();
B[i] = ub(all(coords),B[i])-coords.begin();
}
for(int i=1;i<sz(coords);i++)len[i] = coords[i] - coords[i-1];
for(int i=0;i<sz(coords);i++)dp[0][i] = 1;
for(int i=1;i<=n;i++){
for(int j=A[i];j<=B[i];j++){
dp[i][j] = (dp[i-1][j-1]*len[j])%MOD;
ll choose = len[j]-1;
ll cnt = 1;
for(int k=i-1;k>0;k--){
if(A[k] <= j && j <= B[k]){
cnt++;
choose = (choose*(len[j]+cnt-2))%MOD;
choose = (choose*inv[cnt])%MOD;
if(!choose)break;
dp[i][j]=(dp[i][j]+dp[k-1][j-1]*choose)%MOD;
}
}
}
dp[i][0] = 1;
for(int j=1;j<sz(coords);j++){
dp[i][j]=(dp[i][j]+dp[i-1][j]+dp[i][j-1]-dp[i-1][j-1]+MOD)%MOD;
}
}
cout<<(dp[n][sz(coords)-1]+MOD-1)%MOD<<'\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
4480 KB |
Output is correct |
2 |
Correct |
7 ms |
4480 KB |
Output is correct |
3 |
Correct |
7 ms |
4480 KB |
Output is correct |
4 |
Correct |
7 ms |
4480 KB |
Output is correct |
5 |
Correct |
7 ms |
4480 KB |
Output is correct |
6 |
Correct |
7 ms |
4480 KB |
Output is correct |
7 |
Correct |
7 ms |
4480 KB |
Output is correct |
8 |
Correct |
7 ms |
4480 KB |
Output is correct |
9 |
Correct |
7 ms |
4480 KB |
Output is correct |
10 |
Correct |
7 ms |
4480 KB |
Output is correct |
11 |
Correct |
7 ms |
4480 KB |
Output is correct |
12 |
Correct |
7 ms |
4480 KB |
Output is correct |
13 |
Correct |
7 ms |
4480 KB |
Output is correct |
14 |
Correct |
7 ms |
4480 KB |
Output is correct |
15 |
Correct |
7 ms |
4480 KB |
Output is correct |
16 |
Correct |
3 ms |
3072 KB |
Output is correct |
17 |
Correct |
4 ms |
3072 KB |
Output is correct |
18 |
Correct |
3 ms |
3072 KB |
Output is correct |
19 |
Correct |
4 ms |
3072 KB |
Output is correct |
20 |
Correct |
3 ms |
3072 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
4480 KB |
Output is correct |
2 |
Correct |
7 ms |
4480 KB |
Output is correct |
3 |
Correct |
7 ms |
4480 KB |
Output is correct |
4 |
Correct |
7 ms |
4480 KB |
Output is correct |
5 |
Correct |
7 ms |
4480 KB |
Output is correct |
6 |
Correct |
7 ms |
4480 KB |
Output is correct |
7 |
Correct |
7 ms |
4480 KB |
Output is correct |
8 |
Correct |
7 ms |
4480 KB |
Output is correct |
9 |
Correct |
7 ms |
4480 KB |
Output is correct |
10 |
Correct |
7 ms |
4480 KB |
Output is correct |
11 |
Correct |
7 ms |
4480 KB |
Output is correct |
12 |
Correct |
7 ms |
4480 KB |
Output is correct |
13 |
Correct |
7 ms |
4480 KB |
Output is correct |
14 |
Correct |
7 ms |
4480 KB |
Output is correct |
15 |
Correct |
7 ms |
4480 KB |
Output is correct |
16 |
Correct |
3 ms |
3072 KB |
Output is correct |
17 |
Correct |
4 ms |
3072 KB |
Output is correct |
18 |
Correct |
3 ms |
3072 KB |
Output is correct |
19 |
Correct |
4 ms |
3072 KB |
Output is correct |
20 |
Correct |
3 ms |
3072 KB |
Output is correct |
21 |
Correct |
180 ms |
4464 KB |
Output is correct |
22 |
Correct |
168 ms |
4432 KB |
Output is correct |
23 |
Correct |
153 ms |
4600 KB |
Output is correct |
24 |
Correct |
175 ms |
4420 KB |
Output is correct |
25 |
Correct |
174 ms |
4472 KB |
Output is correct |
26 |
Correct |
364 ms |
4472 KB |
Output is correct |
27 |
Correct |
362 ms |
4472 KB |
Output is correct |
28 |
Correct |
381 ms |
4472 KB |
Output is correct |
29 |
Correct |
380 ms |
4528 KB |
Output is correct |
30 |
Correct |
7 ms |
4608 KB |
Output is correct |
31 |
Correct |
7 ms |
4480 KB |
Output is correct |
32 |
Correct |
7 ms |
4480 KB |
Output is correct |
33 |
Correct |
7 ms |
4480 KB |
Output is correct |
34 |
Correct |
8 ms |
4480 KB |
Output is correct |
35 |
Correct |
7 ms |
4480 KB |
Output is correct |
36 |
Correct |
8 ms |
4480 KB |
Output is correct |
37 |
Correct |
8 ms |
4480 KB |
Output is correct |
38 |
Correct |
8 ms |
4480 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
896 KB |
Output is correct |
2 |
Correct |
2 ms |
896 KB |
Output is correct |
3 |
Correct |
3 ms |
896 KB |
Output is correct |
4 |
Correct |
3 ms |
1024 KB |
Output is correct |
5 |
Correct |
3 ms |
896 KB |
Output is correct |
6 |
Correct |
5 ms |
896 KB |
Output is correct |
7 |
Correct |
5 ms |
896 KB |
Output is correct |
8 |
Correct |
5 ms |
896 KB |
Output is correct |
9 |
Correct |
5 ms |
896 KB |
Output is correct |
10 |
Correct |
5 ms |
896 KB |
Output is correct |
11 |
Correct |
3 ms |
896 KB |
Output is correct |
12 |
Correct |
3 ms |
896 KB |
Output is correct |
13 |
Correct |
3 ms |
896 KB |
Output is correct |
14 |
Correct |
3 ms |
896 KB |
Output is correct |
15 |
Correct |
3 ms |
896 KB |
Output is correct |
16 |
Correct |
2 ms |
896 KB |
Output is correct |
17 |
Correct |
2 ms |
896 KB |
Output is correct |
18 |
Correct |
2 ms |
896 KB |
Output is correct |
19 |
Correct |
2 ms |
896 KB |
Output is correct |
20 |
Correct |
3 ms |
896 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
4480 KB |
Output is correct |
2 |
Correct |
7 ms |
4480 KB |
Output is correct |
3 |
Correct |
7 ms |
4480 KB |
Output is correct |
4 |
Correct |
7 ms |
4480 KB |
Output is correct |
5 |
Correct |
7 ms |
4480 KB |
Output is correct |
6 |
Correct |
7 ms |
4480 KB |
Output is correct |
7 |
Correct |
7 ms |
4480 KB |
Output is correct |
8 |
Correct |
7 ms |
4480 KB |
Output is correct |
9 |
Correct |
7 ms |
4480 KB |
Output is correct |
10 |
Correct |
7 ms |
4480 KB |
Output is correct |
11 |
Correct |
7 ms |
4480 KB |
Output is correct |
12 |
Correct |
7 ms |
4480 KB |
Output is correct |
13 |
Correct |
7 ms |
4480 KB |
Output is correct |
14 |
Correct |
7 ms |
4480 KB |
Output is correct |
15 |
Correct |
7 ms |
4480 KB |
Output is correct |
16 |
Correct |
3 ms |
3072 KB |
Output is correct |
17 |
Correct |
4 ms |
3072 KB |
Output is correct |
18 |
Correct |
3 ms |
3072 KB |
Output is correct |
19 |
Correct |
4 ms |
3072 KB |
Output is correct |
20 |
Correct |
3 ms |
3072 KB |
Output is correct |
21 |
Correct |
180 ms |
4464 KB |
Output is correct |
22 |
Correct |
168 ms |
4432 KB |
Output is correct |
23 |
Correct |
153 ms |
4600 KB |
Output is correct |
24 |
Correct |
175 ms |
4420 KB |
Output is correct |
25 |
Correct |
174 ms |
4472 KB |
Output is correct |
26 |
Correct |
364 ms |
4472 KB |
Output is correct |
27 |
Correct |
362 ms |
4472 KB |
Output is correct |
28 |
Correct |
381 ms |
4472 KB |
Output is correct |
29 |
Correct |
380 ms |
4528 KB |
Output is correct |
30 |
Correct |
7 ms |
4608 KB |
Output is correct |
31 |
Correct |
7 ms |
4480 KB |
Output is correct |
32 |
Correct |
7 ms |
4480 KB |
Output is correct |
33 |
Correct |
7 ms |
4480 KB |
Output is correct |
34 |
Correct |
8 ms |
4480 KB |
Output is correct |
35 |
Correct |
7 ms |
4480 KB |
Output is correct |
36 |
Correct |
8 ms |
4480 KB |
Output is correct |
37 |
Correct |
8 ms |
4480 KB |
Output is correct |
38 |
Correct |
8 ms |
4480 KB |
Output is correct |
39 |
Correct |
3 ms |
896 KB |
Output is correct |
40 |
Correct |
2 ms |
896 KB |
Output is correct |
41 |
Correct |
3 ms |
896 KB |
Output is correct |
42 |
Correct |
3 ms |
1024 KB |
Output is correct |
43 |
Correct |
3 ms |
896 KB |
Output is correct |
44 |
Correct |
5 ms |
896 KB |
Output is correct |
45 |
Correct |
5 ms |
896 KB |
Output is correct |
46 |
Correct |
5 ms |
896 KB |
Output is correct |
47 |
Correct |
5 ms |
896 KB |
Output is correct |
48 |
Correct |
5 ms |
896 KB |
Output is correct |
49 |
Correct |
3 ms |
896 KB |
Output is correct |
50 |
Correct |
3 ms |
896 KB |
Output is correct |
51 |
Correct |
3 ms |
896 KB |
Output is correct |
52 |
Correct |
3 ms |
896 KB |
Output is correct |
53 |
Correct |
3 ms |
896 KB |
Output is correct |
54 |
Correct |
2 ms |
896 KB |
Output is correct |
55 |
Correct |
2 ms |
896 KB |
Output is correct |
56 |
Correct |
2 ms |
896 KB |
Output is correct |
57 |
Correct |
2 ms |
896 KB |
Output is correct |
58 |
Correct |
3 ms |
896 KB |
Output is correct |
59 |
Correct |
234 ms |
4600 KB |
Output is correct |
60 |
Correct |
213 ms |
4472 KB |
Output is correct |
61 |
Correct |
200 ms |
4476 KB |
Output is correct |
62 |
Correct |
237 ms |
4600 KB |
Output is correct |
63 |
Correct |
221 ms |
4472 KB |
Output is correct |
64 |
Correct |
535 ms |
4560 KB |
Output is correct |
65 |
Correct |
537 ms |
4600 KB |
Output is correct |
66 |
Correct |
530 ms |
4472 KB |
Output is correct |
67 |
Correct |
533 ms |
4600 KB |
Output is correct |
68 |
Correct |
536 ms |
4600 KB |
Output is correct |
69 |
Correct |
202 ms |
4472 KB |
Output is correct |
70 |
Correct |
207 ms |
4480 KB |
Output is correct |
71 |
Correct |
203 ms |
4600 KB |
Output is correct |
72 |
Correct |
214 ms |
4472 KB |
Output is correct |
73 |
Correct |
219 ms |
4472 KB |
Output is correct |
74 |
Correct |
28 ms |
3192 KB |
Output is correct |
75 |
Correct |
25 ms |
3064 KB |
Output is correct |
76 |
Correct |
27 ms |
3064 KB |
Output is correct |
77 |
Correct |
27 ms |
3064 KB |
Output is correct |
78 |
Correct |
28 ms |
3064 KB |
Output is correct |