# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
25201 | gabrielsimoes | Boat (APIO16_boat) | C++14 | 179 ms | 2048 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;
typedef long long ll;
const ll MOD = 1000000007;
const int MAXN = 501;
const int MAX = 1e6+10;
#define mod(a, b) (((a)*(b)) % MOD)
ll invpow(ll a, ll b = MOD-2) {
ll ret = 1;
ll prod = a;
for (int i = 0; i <= 30; i++) {
if ((1LL << i) & b)
ret = (ret * prod) % MOD;
prod = (prod*prod) % MOD;
}
return ret;
}
int n;
ll a[MAXN], b[MAXN];
vector<ll> v;
ll inv[MAXN], c[MAXN];
ll dp[MAXN], sum[MAXN];
int main()
{
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%lld %lld", &a[i], &b[i]);
b[i]++;
v.push_back(a[i]);
v.push_back(b[i]);
}
sort(v.begin(), v.end());
v.resize(unique(v.begin(), v.end()) - v.begin());
for (int i = 1; i <= n; i++)
inv[i] = invpow(i);
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 region = 1; region < v.size(); region++) {
ll L = v[region] - v[region-1];
c[0] = 1;
for (int i = 1; i <= n; i++)
c[i] = mod(mod(L + i - 2, inv[i]), c[i-1]);
c[1] = L;
sum[0] = 1;
for (int i = 1; i <= n; i++)
sum[i] = (dp[i-1] + sum[i-1]) % MOD;
for (int i = n-1; i >= 0; i--) {
if (region <= a[i] || region > b[i]) continue;
for (int k = i, cnt = 0; k >= 0; k--) {
if (region <= a[k] || region > b[k]) continue;
dp[i] += mod(c[++cnt], sum[k]);
}
dp[i] %= MOD;
}
}
ll ans = 0;
for (int i = 0; i < n; i++)
ans += dp[i];
printf("%lld\n", ans % 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... |