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;
const int MOD = 1e9 + 7;
int power(int n, int x) {
if (x == 0) {
return 1;
}
int res = power(n, x / 2);
return (long long) res * res % MOD * ((x & 1) ? n : 1) % MOD;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
int N;
cin >> N;
vector<int> A(N + 1);
vector<int> B(N + 1);
vector<int> X;
vector<int> L;
vector<int> inv(N + 1);
for (int i = 1; i <= N; i++) {
cin >> A[i] >> B[i];
X.emplace_back(A[i]);
X.emplace_back(B[i] + 1);
inv[i] = power(i, MOD - 2); // Fermatt's Little Theorem
}
sort(begin(X), end(X));
X.resize(unique(begin(X), end(X)) - begin(X));
for (int i = 1; i <= N; i++) {
A[i] = upper_bound(begin(X), end(X), A[i]) - begin(X);
B[i] = upper_bound(begin(X), end(X), B[i]) - begin(X);
}
L.resize(X.size());
for (int i = 1; i < X.size(); i++) {
L[i] = X[i] - X[i - 1];
}
vector<vector<int>> DP(N + 1, vector<int>(X.size(), 0));
for (int i = 0; i < X.size(); i++) {
DP[0][i] = 1;
}
for (int i = 1; i <= N; i++) {
DP[i][0] = 1;
for (int j = A[i]; j <= B[i]; j++) {
DP[i][j] = ((long long) L[j] * DP[i - 1][j - 1]) % MOD;
int cnt = 1, choose = L[j];
for (int k = i; k > 0; k--) {
if (A[k] <= j && j <= B[k]) {
cnt++;
choose = (long long) choose * (L[j] - cnt + 1) % MOD * inv[cnt] % MOD;
DP[i][j] = (long long) DP[i][j] + (long long) choose * DP[k - 1][j - 1] % MOD;
}
}
}
for (int j = 1; j < X.size(); j++) {
DP[i][j] = ((long long) DP[i][j] + DP[i - 1][j] + DP[i][j - 1] - DP[i - 1][j - 1] + MOD) % MOD;
}
}
cout << (DP[N][X.size() - 1] - 1 + MOD) % MOD << "\n";
return 0;
}
Compilation message (stderr)
boat.cpp: In function 'int main()':
boat.cpp:42:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 1; i < X.size(); i++) {
~~^~~~~~~~~~
boat.cpp:48:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < X.size(); i++) {
~~^~~~~~~~~~
boat.cpp:65:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int j = 1; j < X.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... |