#include<bits/stdc++.h>
using namespace std;
#define int long long
const int MOD = 1e9+7;
const int MAXV = 2000;
int put(int a, int exp)
{
if(exp==0)
return 1;
if(exp%2==0)
return put(a*a%MOD,exp/2);
else
return put(a*a%MOD,exp/2)*a%MOD;
}
int n,cate;
int le[505],ri[505];
map<int,int> mp,nrm;
int invnrm[MAXV+5],nr[MAXV+5];
int dp[MAXV+5][505];
int sumdp[MAXV+5];
int prec_inv[505];
signed main()
{
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>le[i]>>ri[i];
mp[le[i]]++;
mp[ri[i]]++;
prec_inv[i] = put(i,MOD-2);
}
for(auto it:mp)
{
if(it.second)
{
nrm[it.first]=++cate;
invnrm[cate]=it.first;
cate++;
}
}
for(int i=1;i<=cate;i+=2)
{
nr[i]=1;
nr[i+1]=invnrm[i+2]-invnrm[i]-1;
}
dp[0][0]=1;
sumdp[0]=1;
for(int ult=1;ult<cate;ult++)
sumdp[ult] = sumdp[ult-1];
for(int i=1;i<=n;i++)
{
le[i] = nrm[le[i]];
ri[i] = nrm[ri[i]];
for(int ult=ri[i];ult>=le[i];ult--)
{
for(int cnt=min(i,nr[ult]);cnt>1;cnt--)
{
dp[ult][cnt]= (dp[ult][cnt] + dp[ult][cnt-1] * (nr[ult]-cnt+1)%MOD * prec_inv[cnt])%MOD;
}
dp[ult][1] = (dp[ult][1] + sumdp[ult-1]*nr[ult])%MOD;
/*for(int j=0;j<ult;j++)
for(int orice=0;orice<i;orice++)
dp[ult][1] = (dp[ult][1] + dp[j][orice]*nr[ult])%MOD;*/
}
sumdp[0]=1;
for(int ult=1;ult<cate;ult++)
{
sumdp[ult] = sumdp[ult-1];
for(int cnt=1;cnt<=i;cnt++)
sumdp[ult] = (sumdp[ult] + dp[ult][cnt])%MOD;
}
}
int rez=0;
for(int ult=1;ult<=cate;ult++)
for(int cnt=1;cnt<=n;cnt++)
rez = (rez + dp[ult][cnt])%MOD;
cout<<rez;
return 0;
}
/**
impartim intervalul [1,1e9] in intervale relevante, o sa avem maxim 1000 intervale relevante
dp[i][ult][cnt] = in cate moduri pot primele i scoli barci, a.i. ultimele cnt numere sa fie aflate in intervalul ult
daca i poate sa aleaga intervalul ult
dp[i][ult][1] = dp[i-1][ult][1] + sum(dp[i-1][orice<ult][orice])*nr[ult]
pt cnt>=2 dp[i][ult][cnt] = dp[i-1][ult][cnt] + dp[i-1][ult][cnt-1] / comb(nr[ult],cnt-1) * comb(nr[ult],cnt)
pt cnt>=2 dp[i][ult][cnt] = dp[i-1][ult][cnt] + dp[i-1][ult][cnt-1] * (nr[ult]-cnt+1) / cnt
(nr[ult]-cnt+1) / cnt
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
497 ms |
2456 KB |
Output is correct |
2 |
Correct |
499 ms |
2384 KB |
Output is correct |
3 |
Correct |
496 ms |
2388 KB |
Output is correct |
4 |
Correct |
496 ms |
2388 KB |
Output is correct |
5 |
Correct |
497 ms |
2612 KB |
Output is correct |
6 |
Correct |
496 ms |
2388 KB |
Output is correct |
7 |
Correct |
495 ms |
2388 KB |
Output is correct |
8 |
Correct |
495 ms |
2384 KB |
Output is correct |
9 |
Correct |
494 ms |
2468 KB |
Output is correct |
10 |
Correct |
499 ms |
2384 KB |
Output is correct |
11 |
Correct |
495 ms |
2388 KB |
Output is correct |
12 |
Correct |
495 ms |
2348 KB |
Output is correct |
13 |
Correct |
497 ms |
2492 KB |
Output is correct |
14 |
Correct |
501 ms |
2488 KB |
Output is correct |
15 |
Correct |
499 ms |
2464 KB |
Output is correct |
16 |
Correct |
87 ms |
800 KB |
Output is correct |
17 |
Correct |
94 ms |
816 KB |
Output is correct |
18 |
Correct |
90 ms |
820 KB |
Output is correct |
19 |
Correct |
94 ms |
848 KB |
Output is correct |
20 |
Correct |
91 ms |
836 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
497 ms |
2456 KB |
Output is correct |
2 |
Correct |
499 ms |
2384 KB |
Output is correct |
3 |
Correct |
496 ms |
2388 KB |
Output is correct |
4 |
Correct |
496 ms |
2388 KB |
Output is correct |
5 |
Correct |
497 ms |
2612 KB |
Output is correct |
6 |
Correct |
496 ms |
2388 KB |
Output is correct |
7 |
Correct |
495 ms |
2388 KB |
Output is correct |
8 |
Correct |
495 ms |
2384 KB |
Output is correct |
9 |
Correct |
494 ms |
2468 KB |
Output is correct |
10 |
Correct |
499 ms |
2384 KB |
Output is correct |
11 |
Correct |
495 ms |
2388 KB |
Output is correct |
12 |
Correct |
495 ms |
2348 KB |
Output is correct |
13 |
Correct |
497 ms |
2492 KB |
Output is correct |
14 |
Correct |
501 ms |
2488 KB |
Output is correct |
15 |
Correct |
499 ms |
2464 KB |
Output is correct |
16 |
Correct |
87 ms |
800 KB |
Output is correct |
17 |
Correct |
94 ms |
816 KB |
Output is correct |
18 |
Correct |
90 ms |
820 KB |
Output is correct |
19 |
Correct |
94 ms |
848 KB |
Output is correct |
20 |
Correct |
91 ms |
836 KB |
Output is correct |
21 |
Correct |
915 ms |
7876 KB |
Output is correct |
22 |
Correct |
916 ms |
7832 KB |
Output is correct |
23 |
Correct |
899 ms |
7692 KB |
Output is correct |
24 |
Correct |
914 ms |
7776 KB |
Output is correct |
25 |
Correct |
898 ms |
7668 KB |
Output is correct |
26 |
Correct |
875 ms |
7508 KB |
Output is correct |
27 |
Correct |
887 ms |
7444 KB |
Output is correct |
28 |
Correct |
873 ms |
7324 KB |
Output is correct |
29 |
Correct |
873 ms |
7248 KB |
Output is correct |
30 |
Correct |
949 ms |
8016 KB |
Output is correct |
31 |
Correct |
948 ms |
7864 KB |
Output is correct |
32 |
Correct |
939 ms |
8016 KB |
Output is correct |
33 |
Correct |
953 ms |
8016 KB |
Output is correct |
34 |
Correct |
934 ms |
8016 KB |
Output is correct |
35 |
Correct |
502 ms |
4440 KB |
Output is correct |
36 |
Correct |
507 ms |
4304 KB |
Output is correct |
37 |
Correct |
502 ms |
4688 KB |
Output is correct |
38 |
Correct |
498 ms |
4436 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
1880 KB |
Output is correct |
2 |
Correct |
10 ms |
1884 KB |
Output is correct |
3 |
Correct |
10 ms |
1884 KB |
Output is correct |
4 |
Correct |
10 ms |
1884 KB |
Output is correct |
5 |
Correct |
10 ms |
1884 KB |
Output is correct |
6 |
Correct |
11 ms |
1880 KB |
Output is correct |
7 |
Correct |
11 ms |
1884 KB |
Output is correct |
8 |
Correct |
11 ms |
2032 KB |
Output is correct |
9 |
Correct |
11 ms |
1884 KB |
Output is correct |
10 |
Correct |
11 ms |
2036 KB |
Output is correct |
11 |
Correct |
10 ms |
1884 KB |
Output is correct |
12 |
Correct |
10 ms |
2072 KB |
Output is correct |
13 |
Correct |
11 ms |
1880 KB |
Output is correct |
14 |
Correct |
11 ms |
2064 KB |
Output is correct |
15 |
Correct |
10 ms |
1884 KB |
Output is correct |
16 |
Correct |
5 ms |
1116 KB |
Output is correct |
17 |
Correct |
4 ms |
1116 KB |
Output is correct |
18 |
Correct |
5 ms |
1116 KB |
Output is correct |
19 |
Correct |
5 ms |
1160 KB |
Output is correct |
20 |
Correct |
5 ms |
1116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
497 ms |
2456 KB |
Output is correct |
2 |
Correct |
499 ms |
2384 KB |
Output is correct |
3 |
Correct |
496 ms |
2388 KB |
Output is correct |
4 |
Correct |
496 ms |
2388 KB |
Output is correct |
5 |
Correct |
497 ms |
2612 KB |
Output is correct |
6 |
Correct |
496 ms |
2388 KB |
Output is correct |
7 |
Correct |
495 ms |
2388 KB |
Output is correct |
8 |
Correct |
495 ms |
2384 KB |
Output is correct |
9 |
Correct |
494 ms |
2468 KB |
Output is correct |
10 |
Correct |
499 ms |
2384 KB |
Output is correct |
11 |
Correct |
495 ms |
2388 KB |
Output is correct |
12 |
Correct |
495 ms |
2348 KB |
Output is correct |
13 |
Correct |
497 ms |
2492 KB |
Output is correct |
14 |
Correct |
501 ms |
2488 KB |
Output is correct |
15 |
Correct |
499 ms |
2464 KB |
Output is correct |
16 |
Correct |
87 ms |
800 KB |
Output is correct |
17 |
Correct |
94 ms |
816 KB |
Output is correct |
18 |
Correct |
90 ms |
820 KB |
Output is correct |
19 |
Correct |
94 ms |
848 KB |
Output is correct |
20 |
Correct |
91 ms |
836 KB |
Output is correct |
21 |
Correct |
915 ms |
7876 KB |
Output is correct |
22 |
Correct |
916 ms |
7832 KB |
Output is correct |
23 |
Correct |
899 ms |
7692 KB |
Output is correct |
24 |
Correct |
914 ms |
7776 KB |
Output is correct |
25 |
Correct |
898 ms |
7668 KB |
Output is correct |
26 |
Correct |
875 ms |
7508 KB |
Output is correct |
27 |
Correct |
887 ms |
7444 KB |
Output is correct |
28 |
Correct |
873 ms |
7324 KB |
Output is correct |
29 |
Correct |
873 ms |
7248 KB |
Output is correct |
30 |
Correct |
949 ms |
8016 KB |
Output is correct |
31 |
Correct |
948 ms |
7864 KB |
Output is correct |
32 |
Correct |
939 ms |
8016 KB |
Output is correct |
33 |
Correct |
953 ms |
8016 KB |
Output is correct |
34 |
Correct |
934 ms |
8016 KB |
Output is correct |
35 |
Correct |
502 ms |
4440 KB |
Output is correct |
36 |
Correct |
507 ms |
4304 KB |
Output is correct |
37 |
Correct |
502 ms |
4688 KB |
Output is correct |
38 |
Correct |
498 ms |
4436 KB |
Output is correct |
39 |
Correct |
10 ms |
1880 KB |
Output is correct |
40 |
Correct |
10 ms |
1884 KB |
Output is correct |
41 |
Correct |
10 ms |
1884 KB |
Output is correct |
42 |
Correct |
10 ms |
1884 KB |
Output is correct |
43 |
Correct |
10 ms |
1884 KB |
Output is correct |
44 |
Correct |
11 ms |
1880 KB |
Output is correct |
45 |
Correct |
11 ms |
1884 KB |
Output is correct |
46 |
Correct |
11 ms |
2032 KB |
Output is correct |
47 |
Correct |
11 ms |
1884 KB |
Output is correct |
48 |
Correct |
11 ms |
2036 KB |
Output is correct |
49 |
Correct |
10 ms |
1884 KB |
Output is correct |
50 |
Correct |
10 ms |
2072 KB |
Output is correct |
51 |
Correct |
11 ms |
1880 KB |
Output is correct |
52 |
Correct |
11 ms |
2064 KB |
Output is correct |
53 |
Correct |
10 ms |
1884 KB |
Output is correct |
54 |
Correct |
5 ms |
1116 KB |
Output is correct |
55 |
Correct |
4 ms |
1116 KB |
Output is correct |
56 |
Correct |
5 ms |
1116 KB |
Output is correct |
57 |
Correct |
5 ms |
1160 KB |
Output is correct |
58 |
Correct |
5 ms |
1116 KB |
Output is correct |
59 |
Correct |
1131 ms |
8504 KB |
Output is correct |
60 |
Correct |
1118 ms |
8504 KB |
Output is correct |
61 |
Correct |
1122 ms |
8500 KB |
Output is correct |
62 |
Correct |
1135 ms |
8284 KB |
Output is correct |
63 |
Correct |
1140 ms |
8484 KB |
Output is correct |
64 |
Correct |
1188 ms |
8272 KB |
Output is correct |
65 |
Correct |
1194 ms |
8276 KB |
Output is correct |
66 |
Correct |
1188 ms |
8264 KB |
Output is correct |
67 |
Correct |
1182 ms |
8500 KB |
Output is correct |
68 |
Correct |
1202 ms |
8420 KB |
Output is correct |
69 |
Correct |
1097 ms |
8280 KB |
Output is correct |
70 |
Correct |
1094 ms |
8280 KB |
Output is correct |
71 |
Correct |
1097 ms |
8284 KB |
Output is correct |
72 |
Correct |
1099 ms |
8276 KB |
Output is correct |
73 |
Correct |
1099 ms |
8504 KB |
Output is correct |
74 |
Correct |
114 ms |
1116 KB |
Output is correct |
75 |
Correct |
114 ms |
1116 KB |
Output is correct |
76 |
Correct |
114 ms |
1116 KB |
Output is correct |
77 |
Correct |
113 ms |
1116 KB |
Output is correct |
78 |
Correct |
113 ms |
1116 KB |
Output is correct |