# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
315982 | phathnv | Boat (APIO16_boat) | C++11 | 12 ms | 8448 KiB |
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 mp make_pair
#define X first
#define Y second
#define taskname "Boat"
using namespace std;
typedef long long ll;
typedef pair <int, int> ii;
const int N = 501;
const int MOD = 1e9 + 7;
int n, a[N], b[N];
int nX, x[2 * N], c[2 * N][N], inv[N], dp[2][N * 2][N];
void readInput(){
cin >> n;
for(int i = 1; i <= n; i++)
cin >> a[i] >> b[i];
}
void compress(){
for(int i = 1; i <= n; i++){
x[++nX] = a[i];
x[++nX] = ++b[i];
}
sort(x + 1, x + 1 + nX);
nX = unique(x + 1, x + 1 + nX) - (x + 1);
for(int i = 1; i <= n; i++){
a[i] = lower_bound(x + 1, x + 1 + nX, a[i]) - x;
b[i] = lower_bound(x + 1, x + 1 + nX, b[i]) - x;
}
}
int powMod(int x, int k){
if (k == 0)
return 1;
int res = powMod(x, k >> 1);
res = (ll) res * res % MOD;
if (k & 1)
res = (ll) res * x % MOD;
return res;
}
void prepare(){
for(int i = 1; i <= n; i++)
b[i]--;
inv[0] = 1;
for(int i = 1; i <= n; i++)
inv[i] = powMod(i, MOD - 2);
}
void add(int &x, int y){
x += y;
x -= (x >= MOD) * MOD;
}
void solve(){
for(int i = 0; i < n; i++){
int id = i & 1;
for(int j = 0; j < nX; j++){
int len = x[j + 1] - x[j];
for(int k = 0; k <= len; k++)
dp[id ^ 1][j][k] = 0;
}
int sum = 1;
for(int j = 1; j < nX; j++){
int len = x[j + 1] - x[j];
if (a[i + 1] <= j && j <= b[i + 1])
add(dp[id ^ 1][j][1], (ll) sum * len % MOD);
for(int k = 1; k <= len; k++){
add(sum, dp[id][j][k]);
add(dp[id ^ 1][j][k], dp[i][j][k]);
if (a[i + 1] <= j && j <= b[i + 1] && k < len)
add(dp[id ^ 1][j][k + 1], (ll) dp[id][j][k] * (len - k) % MOD * inv[k + 1] % MOD);
}
}
}
int res = 0;
for(int j = 1; j < nX; j++){
int len = x[j + 1] - x[j];
for(int k = 1; k <= len; k++)
add(res, dp[n & 1][j][k]);
}
cout << res;
}
int main(){
if (fopen(taskname".inp", "r")){
freopen(taskname".inp", "r", stdin);
freopen(taskname".out", "w", stdout);
}
readInput();
compress();
prepare();
solve();
return 0;
}
Compilation message (stderr)
# | 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... |