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>
#define X first
#define Y second
#define all(x) begin(x), end(x)
#define FOR(i, a, b) for(int i = (a); i <= (b); i++)
#define FORD(i, b, a) for(int i = (b); i >= (a); i--)
#define REP(i, a, b) for (int i = (a); i < (b); i++)
#define mxx max_element
#define mnn min_element
#define SQR(x) (1LL * (x) * (x))
#define MASK(i) (1LL << (i))
#define Point Vector
#define left Left
#define right Right
#define div Div
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ld;
typedef pair<db, db> pdb;
typedef pair<ld, ld> pld;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
typedef pair<ll, ll> pll;
typedef pair<ll, pll> plll;
typedef pair<ll, int> pli;
typedef pair<ll, pii> plii;
template<class A, class B>
bool maximize(A& x, B y) {
if (x < y) return x = y, true; else return false;
}
template<class A, class B>
bool minimize(A& x, B y) {
if (x > y) return x = y, true; else return false;
}
/* END OF TEMPLATE */
const int N = 2e3 + 7;
const int mod = 1e9 + 7;
int n, m, a[N], b[N];
vector<int> E;
pii block[N];
int dp[2][2000][2000][2];
void add(int& x, int y) {
if ((x+=y) >= mod) x-=mod;
}
int Ckn[N][N];
long long fact[N], inv_fact[N];
long long mul(long long a, long long b) {
if (b == 0) return 1;
ll tmp = mul(a, b / 2);
tmp = tmp * tmp % mod;
if (b & 1) tmp = tmp * a % mod;
return tmp;
}
void buildCkn() {
fact[0] = 1;
for (int i = 1; i <= n; i++) fact[i] = fact[i - 1] * i % mod;
inv_fact[n] = mul(fact[n], mod - 2);
for (int i = n - 1; i > 0; i--) inv_fact[i] = inv_fact[i + 1] * (i + 1) % mod;
for (int i = 1; i <= m; i++) {
int sz = block[i].Y - block[i].X + 1;
ll tich = 1;
for (int j = sz; j > 0; j--) {
tich = tich * j % mod;
Ckn[i][sz - j + 1] = tich * inv_fact[sz - j + 1] % mod;
// cout<<sz<<" "<<sz - j + 1<<" "<< Ckn[i][sz - j + 1]<<"\n";
if (sz - j + 1 > n) break;
}
}
}
int cnt[N];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin>>n;
set<int> s;
for (int i = 1; i <= n; i++) {
cin>>a[i]>>b[i];
s.insert(a[i]);
s.insert(b[i]);
}
vector<int> E(s.begin(), s.end());
for (int i = 0; i < E.size(); i++) {
block[++m] = {E[i], E[i]};
if (i + 1 < E.size() && E[i] + 1 <= E[i + 1] - 1) block[++m] = {E[i] + 1, E[i + 1] - 1};
}
buildCkn();
for (int i = 1; i <= n; i++) {
for (int j = 0; j <= m; j++)
if (a[i] <= block[j].X && block[j].Y <= b[i]) cnt[j]++;
}
cnt[0] = n;
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= m; j++)
if (a[i] <= block[j].X && block[j].Y <= b[i]) cnt[j]--;
for (int j = 0; j <= m; j++)
for (int k = 0; k <= cnt[j]; k++)
for (int z = 0; z <= 1; z++) {
int& res = dp[i & 1][j][k][z];
res = 0;
if (i == 0) {
res = Ckn[j][k];
continue;
}
if (a[i] <= block[j].X && block[j].Y <= b[i]) add(res, dp[i & 1 ^ 1][j][k + 1][0]);
if (z == 0) add(res, dp[i & 1 ^ 1][j][k][0]);
if (j > 0 && k > 0) add(res, 1LL * dp[i & 1][j - 1][0][1] * Ckn[j][k] % mod); else
if (j > 0 && k == 0) add(res, dp[i & 1][j - 1][0][1]);
}
}
cout<<dp[n & 1][m][0][0];
return 0;
}
Compilation message (stderr)
boat.cpp: In function 'int main()':
boat.cpp:100:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
100 | for (int i = 0; i < E.size(); i++) {
| ~~^~~~~~~~~~
boat.cpp:102:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
102 | if (i + 1 < E.size() && E[i] + 1 <= E[i + 1] - 1) block[++m] = {E[i] + 1, E[i + 1] - 1};
| ~~~~~~^~~~~~~~~~
boat.cpp:122:81: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
122 | if (a[i] <= block[j].X && block[j].Y <= b[i]) add(res, dp[i & 1 ^ 1][j][k + 1][0]);
| ~~^~~
boat.cpp:123:47: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
123 | if (z == 0) add(res, dp[i & 1 ^ 1][j][k][0]);
| ~~^~~
# | 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... |