This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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];
int fact[1005],invfact[1005];
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)
{
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 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] * ways(intv[j].second - intv[j].first + 1,q) % modulo * lgpow(ways(intv[j].second - intv[j].first + 1,q - 1),modulo - 2) % 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++)
for (int qp = 0; qp < i; qp++)
sm += dpant[jp][qp];
sm %= modulo;
sm *= cf;
sm %= modulo;
dpcur[j][q] += sm;
dpcur[j][q] %= modulo;
}
}
}
for (int j = 0; j < intv.size(); j++)
{
for (int q = 1; q <= i; q++)
dpant[j][q] = 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 (stderr)
boat.cpp: In function 'int main()':
boat.cpp:52: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]
52 | for (int i = 0; i < t.size() - 1; i++)
| ~~^~~~~~~~~~~~~~
boat.cpp:56: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]
56 | for (int j = 0; j < intv.size(); j++)
| ~~^~~~~~~~~~~~~
boat.cpp:84: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]
84 | for (int j = 0; j < intv.size(); j++)
| ~~^~~~~~~~~~~~~
boat.cpp:91: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]
91 | for (int j = 0; j < intv.size(); j++)
| ~~^~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |