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 = 1000000007;
int power(int n, int x) {
if (x == 0) {
return 1;
}
int res = power(n, x / 2);
res = (1ll * res * res) % MOD;
if (x & 1) {
res = (1ll * res * n) % MOD;
}
return res;
}
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); // inc[i] = inverse of integer i
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() + 1, 0));
vector<vector<int>> Choose(X.size() + 1, vector<int>(N + 1, 0));
for (int i = 1; i < X.size(); i++) {
Choose[i][0] = 1;
for (int j = 1; j <= N; j++) {
Choose[i][j] = (1ll * Choose[i][j - 1] * (L[i] - j + 1) % MOD) * inv[j] % MOD; // Choose j items from a total of L[i] items
}
}
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 = 1; j < X.size(); j++) {
DP[i][j] = DP[i][j - 1];
for (int k = i, cnt = 0, choose = 0; k > 0; k--) {
if (A[k] <= j && j <= B[k]) {
cnt++;
choose = (1ll * choose + Choose[j][cnt]) % MOD; // Equal to: if we fix the current item, how many ways of using already proccesed items in which all of them is in interval j
DP[i][j] = (1ll * DP[i][j] + (1ll * choose * DP[k - 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:46:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 1; i < X.size(); i++) {
~~^~~~~~~~~~
boat.cpp:53:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 1; i < X.size(); i++) {
~~^~~~~~~~~~
boat.cpp:60:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < X.size(); i++) {
~~^~~~~~~~~~
boat.cpp:66: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... |