# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
110596 | Pro_ktmr | Boat (APIO16_boat) | C++14 | 2037 ms | 12420 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"
using namespace std;
#define LL long long
#define PB push_back
#define MP make_pair
const LL MOD = 1000000007;
//累乗 O(log N)
long long power(long long x, int N){
if(N == 1) return x;
long long tmp = power(x, N/2);
if(N%2 == 0) return tmp * tmp % MOD;
else return tmp * tmp % MOD * x % MOD;
}
//逆元 O(log x)
long long inverse(long long x){
return power(x, MOD-2);
}
//コンビネーション
struct Combination{
private:
int N;
vector<long long> fact, inv;
public:
void init(int n){ //初期化する O(N)
N = n;
fact.push_back(1);
inv.push_back(1);
for(long long i=1; i<=N; i++){
fact.push_back(fact.back()*i%MOD);
inv.push_back(inverse(fact.back()));
}
}
long long comb(int n, int k){ //nCkを求める O(1)
return fact[n] * inv[k] % MOD * inv[n-k] % MOD;
}
};
Combination comb;
int N, a[500], b[500], A[500], B[500];
vector<int> v;
vector<int> ls[1000];
LL memo[1000][501];
LL culc(LL range, int num){
if(num == 1) return range;
LL ret = 0;
LL tmp1 = range;
LL tmp2 = range-1;
for(int i=0; i<=num-2; i++){ //中のを何個使うか
if(tmp2 == 0) break;
tmp1 = tmp1 * tmp2 % MOD;
tmp1 = tmp1 * inverse(i+2) % MOD;
tmp2--;
ret += tmp1 * comb.comb(num-2, i) % MOD;
}
return ret % MOD;
}
//a:=区間、b:=学校
LL dp[1000][500][2];
LL solve(int a, int b, int ok){
if(a == v.size()-1) return 1;
if(ok == 0 && b == N) return 0;
if(ok == 1 && b == N) return 1;
if(dp[a][b][ok] != -1) return dp[a][b][ok];
if(ok == 0){
dp[a][b][ok] = solve(a, b+1, 0);
if(A[b] <= a && a < B[b]){
int idx = lower_bound(ls[a].begin(), ls[a].end(), b) - ls[a].begin();
for(int i=idx; i<ls[a].size()-1; i++){
dp[a][b][ok] += memo[a][i-idx+1]*solve(a+1, ls[a][i]+1, 1);
dp[a][b][ok] %= MOD;
}
}
}
else{
dp[a][b][ok] = solve(a+1, b, 1);
dp[a][b][ok] += solve(a, b, 0);
dp[a][b][ok] %= MOD;
}
return dp[a][b][ok];
}
int main(){
scanf("%d", &N);
for(int i=0; i<N; i++){
scanf("%d%d", a+i, b+i);
b[i]++;
v.PB(a[i]);
v.PB(b[i]);
}
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
for(int i=0; i<N; i++){
A[i] = lower_bound(v.begin(), v.end(), a[i]) - v.begin();
B[i] = lower_bound(v.begin(), v.end(), b[i]) - v.begin();
for(int j=A[i]; j<B[i]; j++) ls[j].PB(i);
}
comb.init(N);
for(int i=0; i<v.size(); i++){
ls[i].PB(N);
for(int j=0; j<N; j++) dp[i][j][0] = -1;
for(int j=0; j<N; j++) dp[i][j][1] = -1;
if(i != v.size()-1) for(int j=1; j<=ls[i].size(); j++) memo[i][j] = culc(v[i+1]-v[i], j);
}
printf("%d\n", (solve(0, 0, 1)-1+MOD)%MOD);
}
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... |