#include <bits/stdc++.h>
using namespace std;
#define int long long
int modulo = 1e9 + 7;
int n;
int l[505],r[505];
int dpcur[1005][505], dpant[1005][505],smant[1005];
int fact[1005],invfact[1005];
int f[1005][505],invf[1005][505];
int lgpow(int x,int y)
{
int z = 1;
while (y != 0)
{
if (y % 2 == 1)
z = z * x % modulo;
x = x * x % modulo;
y /= 2;
}
return z;
}
int ways(int x, int y)
{
if (x < y)
return 0;
int rr = 1;
for (int i = x - y + 1; i <= x; i++)
rr = rr * i % modulo;
rr *= invfact[y];
rr %= modulo;
return rr;
}
signed main()
{
fact[0] = invfact[0] = 1;
for (int i = 1; i <= 1000; i++)
fact[i] = i * fact[i - 1] % modulo, invfact[i] = lgpow(fact[i],modulo - 2);
cin >> n;
for (int i = 1; i <= n; i++)
cin >> l[i] >> r[i];
set<int> timpi;
for (int i = 1; i <= n; i++)
timpi.insert(l[i]),timpi.insert(r[i] + 1);
vector<int> t;
for (auto it : timpi)
t.push_back(it);
vector<pair<int,int>> intv;
for (int i = 0; i < t.size() - 1; i++)
intv.push_back({t[i],t[i + 1] - 1});
for (int j = 0; j < intv.size(); j++)
{
for (int q = 1; q <= n; q++)
{
f[j][q] = ways(intv[j].second - intv[j].first + 1,q);
invf[j][q] = lgpow(f[j][q],modulo - 2);
}
}
for (int i = 1; i <= n; i++)
{
for (int j = 0; j < intv.size(); j++)
{
for (int q = 1; q <= i; q++)
{
dpcur[j][q] = dpant[j][q];
if (r[i] < intv[j].first or l[i] > intv[j].first)
continue;
if (q != 1)
{
int adg = dpant[j][q - 1] * f[j][q] % modulo * invf[j][q - 1] % modulo;
dpcur[j][q] += adg;
dpcur[j][q] %= modulo;
}
else
{
int cf = intv[j].second - intv[j].first + 1;
int sm = 1;///nu mai e nimic inainte
for (int jp = 0; jp < j; jp++)
sm += smant[jp];
sm %= modulo;
sm *= cf;
sm %= modulo;
dpcur[j][q] += sm;
dpcur[j][q] %= modulo;
}
}
}
for (int j = 0; j < intv.size(); j++)
{
smant[j] = 0;
for (int q = 1; q <= i; q++)
dpant[j][q] = dpcur[j][q],smant[j] += dpcur[j][q];
}
}
int ans = 0;
for (int j = 0; j < intv.size(); j++)
{
for (int q = 1; q <= n; q++)
ans += dpant[j][q];
}
ans %= modulo;
cout << ans;
return 0;
}
/**
Exista O(N) intervale relevante de nr barci
dp[i][j][q] = in cate moduri pot lua primii i jucatori, astfel incat ultimul luat sa trimita barci in intervalul j, de fapt q trimitand asa
dp[i][j][q] vine din:
- dp[i - 1][j][q] (omul i sta pe bara), coef = 1
- daca q != 1, dp[i - 1][j][q - 1], coef = ways(q, len[j]) / ways(q - 1, len[j]) unde consider 0 / 0 = 0
- daca q = 1, dp[i - 1][j'][q'], coef = len[j]
Deci pentru majoritatea starilor (tehnic N^3) am o singura tranzitie, pentru N^2 insa am N^2 tranzitii si dau in N^4
Dar tranzitia aia e coef * suma(dp[i - 1][j'][q']) cu j' < j si orice q' (pot chiar sa parcurg j' ca sa nu imi tin sume dubioase ca e N^3)
ways(x, len) = C(len,x)
**/
Compilation message
boat.cpp: In function 'int main()':
boat.cpp:55:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
55 | for (int i = 0; i < t.size() - 1; i++)
| ~~^~~~~~~~~~~~~~
boat.cpp:57:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
57 | for (int j = 0; j < intv.size(); j++)
| ~~^~~~~~~~~~~~~
boat.cpp:67:27: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
67 | for (int j = 0; j < intv.size(); j++)
| ~~^~~~~~~~~~~~~
boat.cpp:94:27: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
94 | for (int j = 0; j < intv.size(); j++)
| ~~^~~~~~~~~~~~~
boat.cpp:102:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
102 | for (int j = 0; j < intv.size(); j++)
| ~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
655 ms |
16196 KB |
Output is correct |
2 |
Correct |
638 ms |
16336 KB |
Output is correct |
3 |
Correct |
694 ms |
16488 KB |
Output is correct |
4 |
Correct |
658 ms |
16468 KB |
Output is correct |
5 |
Correct |
641 ms |
16212 KB |
Output is correct |
6 |
Correct |
667 ms |
16212 KB |
Output is correct |
7 |
Correct |
656 ms |
16108 KB |
Output is correct |
8 |
Correct |
652 ms |
16240 KB |
Output is correct |
9 |
Correct |
665 ms |
16208 KB |
Output is correct |
10 |
Correct |
703 ms |
16588 KB |
Output is correct |
11 |
Correct |
651 ms |
16212 KB |
Output is correct |
12 |
Correct |
650 ms |
16180 KB |
Output is correct |
13 |
Correct |
676 ms |
16468 KB |
Output is correct |
14 |
Correct |
676 ms |
16332 KB |
Output is correct |
15 |
Correct |
662 ms |
16116 KB |
Output is correct |
16 |
Correct |
112 ms |
3164 KB |
Output is correct |
17 |
Correct |
147 ms |
3412 KB |
Output is correct |
18 |
Correct |
117 ms |
3152 KB |
Output is correct |
19 |
Correct |
120 ms |
3492 KB |
Output is correct |
20 |
Correct |
120 ms |
3344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
655 ms |
16196 KB |
Output is correct |
2 |
Correct |
638 ms |
16336 KB |
Output is correct |
3 |
Correct |
694 ms |
16488 KB |
Output is correct |
4 |
Correct |
658 ms |
16468 KB |
Output is correct |
5 |
Correct |
641 ms |
16212 KB |
Output is correct |
6 |
Correct |
667 ms |
16212 KB |
Output is correct |
7 |
Correct |
656 ms |
16108 KB |
Output is correct |
8 |
Correct |
652 ms |
16240 KB |
Output is correct |
9 |
Correct |
665 ms |
16208 KB |
Output is correct |
10 |
Correct |
703 ms |
16588 KB |
Output is correct |
11 |
Correct |
651 ms |
16212 KB |
Output is correct |
12 |
Correct |
650 ms |
16180 KB |
Output is correct |
13 |
Correct |
676 ms |
16468 KB |
Output is correct |
14 |
Correct |
676 ms |
16332 KB |
Output is correct |
15 |
Correct |
662 ms |
16116 KB |
Output is correct |
16 |
Correct |
112 ms |
3164 KB |
Output is correct |
17 |
Correct |
147 ms |
3412 KB |
Output is correct |
18 |
Correct |
117 ms |
3152 KB |
Output is correct |
19 |
Correct |
120 ms |
3492 KB |
Output is correct |
20 |
Correct |
120 ms |
3344 KB |
Output is correct |
21 |
Correct |
638 ms |
14860 KB |
Output is correct |
22 |
Correct |
614 ms |
15136 KB |
Output is correct |
23 |
Correct |
584 ms |
14788 KB |
Output is correct |
24 |
Correct |
580 ms |
14676 KB |
Output is correct |
25 |
Correct |
621 ms |
14672 KB |
Output is correct |
26 |
Correct |
675 ms |
14396 KB |
Output is correct |
27 |
Correct |
681 ms |
14676 KB |
Output is correct |
28 |
Correct |
648 ms |
14164 KB |
Output is correct |
29 |
Correct |
648 ms |
14164 KB |
Output is correct |
30 |
Correct |
608 ms |
16208 KB |
Output is correct |
31 |
Correct |
615 ms |
16140 KB |
Output is correct |
32 |
Correct |
622 ms |
16212 KB |
Output is correct |
33 |
Correct |
617 ms |
16192 KB |
Output is correct |
34 |
Correct |
615 ms |
16212 KB |
Output is correct |
35 |
Correct |
600 ms |
16204 KB |
Output is correct |
36 |
Correct |
582 ms |
16072 KB |
Output is correct |
37 |
Correct |
594 ms |
16084 KB |
Output is correct |
38 |
Correct |
614 ms |
16208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
3420 KB |
Output is correct |
2 |
Correct |
18 ms |
3416 KB |
Output is correct |
3 |
Correct |
15 ms |
3420 KB |
Output is correct |
4 |
Correct |
15 ms |
3420 KB |
Output is correct |
5 |
Correct |
16 ms |
3656 KB |
Output is correct |
6 |
Correct |
16 ms |
3660 KB |
Output is correct |
7 |
Correct |
20 ms |
3664 KB |
Output is correct |
8 |
Correct |
17 ms |
3420 KB |
Output is correct |
9 |
Correct |
18 ms |
3416 KB |
Output is correct |
10 |
Correct |
16 ms |
3420 KB |
Output is correct |
11 |
Correct |
15 ms |
3428 KB |
Output is correct |
12 |
Correct |
15 ms |
3420 KB |
Output is correct |
13 |
Correct |
15 ms |
3596 KB |
Output is correct |
14 |
Correct |
14 ms |
3416 KB |
Output is correct |
15 |
Correct |
18 ms |
3416 KB |
Output is correct |
16 |
Correct |
9 ms |
2140 KB |
Output is correct |
17 |
Correct |
7 ms |
2140 KB |
Output is correct |
18 |
Correct |
8 ms |
2292 KB |
Output is correct |
19 |
Correct |
9 ms |
2136 KB |
Output is correct |
20 |
Correct |
9 ms |
2140 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
655 ms |
16196 KB |
Output is correct |
2 |
Correct |
638 ms |
16336 KB |
Output is correct |
3 |
Correct |
694 ms |
16488 KB |
Output is correct |
4 |
Correct |
658 ms |
16468 KB |
Output is correct |
5 |
Correct |
641 ms |
16212 KB |
Output is correct |
6 |
Correct |
667 ms |
16212 KB |
Output is correct |
7 |
Correct |
656 ms |
16108 KB |
Output is correct |
8 |
Correct |
652 ms |
16240 KB |
Output is correct |
9 |
Correct |
665 ms |
16208 KB |
Output is correct |
10 |
Correct |
703 ms |
16588 KB |
Output is correct |
11 |
Correct |
651 ms |
16212 KB |
Output is correct |
12 |
Correct |
650 ms |
16180 KB |
Output is correct |
13 |
Correct |
676 ms |
16468 KB |
Output is correct |
14 |
Correct |
676 ms |
16332 KB |
Output is correct |
15 |
Correct |
662 ms |
16116 KB |
Output is correct |
16 |
Correct |
112 ms |
3164 KB |
Output is correct |
17 |
Correct |
147 ms |
3412 KB |
Output is correct |
18 |
Correct |
117 ms |
3152 KB |
Output is correct |
19 |
Correct |
120 ms |
3492 KB |
Output is correct |
20 |
Correct |
120 ms |
3344 KB |
Output is correct |
21 |
Correct |
638 ms |
14860 KB |
Output is correct |
22 |
Correct |
614 ms |
15136 KB |
Output is correct |
23 |
Correct |
584 ms |
14788 KB |
Output is correct |
24 |
Correct |
580 ms |
14676 KB |
Output is correct |
25 |
Correct |
621 ms |
14672 KB |
Output is correct |
26 |
Correct |
675 ms |
14396 KB |
Output is correct |
27 |
Correct |
681 ms |
14676 KB |
Output is correct |
28 |
Correct |
648 ms |
14164 KB |
Output is correct |
29 |
Correct |
648 ms |
14164 KB |
Output is correct |
30 |
Correct |
608 ms |
16208 KB |
Output is correct |
31 |
Correct |
615 ms |
16140 KB |
Output is correct |
32 |
Correct |
622 ms |
16212 KB |
Output is correct |
33 |
Correct |
617 ms |
16192 KB |
Output is correct |
34 |
Correct |
615 ms |
16212 KB |
Output is correct |
35 |
Correct |
600 ms |
16204 KB |
Output is correct |
36 |
Correct |
582 ms |
16072 KB |
Output is correct |
37 |
Correct |
594 ms |
16084 KB |
Output is correct |
38 |
Correct |
614 ms |
16208 KB |
Output is correct |
39 |
Correct |
15 ms |
3420 KB |
Output is correct |
40 |
Correct |
18 ms |
3416 KB |
Output is correct |
41 |
Correct |
15 ms |
3420 KB |
Output is correct |
42 |
Correct |
15 ms |
3420 KB |
Output is correct |
43 |
Correct |
16 ms |
3656 KB |
Output is correct |
44 |
Correct |
16 ms |
3660 KB |
Output is correct |
45 |
Correct |
20 ms |
3664 KB |
Output is correct |
46 |
Correct |
17 ms |
3420 KB |
Output is correct |
47 |
Correct |
18 ms |
3416 KB |
Output is correct |
48 |
Correct |
16 ms |
3420 KB |
Output is correct |
49 |
Correct |
15 ms |
3428 KB |
Output is correct |
50 |
Correct |
15 ms |
3420 KB |
Output is correct |
51 |
Correct |
15 ms |
3596 KB |
Output is correct |
52 |
Correct |
14 ms |
3416 KB |
Output is correct |
53 |
Correct |
18 ms |
3416 KB |
Output is correct |
54 |
Correct |
9 ms |
2140 KB |
Output is correct |
55 |
Correct |
7 ms |
2140 KB |
Output is correct |
56 |
Correct |
8 ms |
2292 KB |
Output is correct |
57 |
Correct |
9 ms |
2136 KB |
Output is correct |
58 |
Correct |
9 ms |
2140 KB |
Output is correct |
59 |
Correct |
1269 ms |
16352 KB |
Output is correct |
60 |
Correct |
1275 ms |
16496 KB |
Output is correct |
61 |
Correct |
1259 ms |
16208 KB |
Output is correct |
62 |
Correct |
1305 ms |
16268 KB |
Output is correct |
63 |
Correct |
1256 ms |
16100 KB |
Output is correct |
64 |
Correct |
1326 ms |
16216 KB |
Output is correct |
65 |
Correct |
1333 ms |
16208 KB |
Output is correct |
66 |
Correct |
1323 ms |
16208 KB |
Output is correct |
67 |
Correct |
1354 ms |
16184 KB |
Output is correct |
68 |
Correct |
1326 ms |
16340 KB |
Output is correct |
69 |
Correct |
1046 ms |
16208 KB |
Output is correct |
70 |
Correct |
1086 ms |
16208 KB |
Output is correct |
71 |
Correct |
1131 ms |
16208 KB |
Output is correct |
72 |
Correct |
1095 ms |
16208 KB |
Output is correct |
73 |
Correct |
1131 ms |
16212 KB |
Output is correct |
74 |
Correct |
166 ms |
3152 KB |
Output is correct |
75 |
Correct |
168 ms |
3408 KB |
Output is correct |
76 |
Correct |
176 ms |
3632 KB |
Output is correct |
77 |
Correct |
168 ms |
3172 KB |
Output is correct |
78 |
Correct |
168 ms |
3152 KB |
Output is correct |