Submission #46746

# Submission time Handle Problem Language Result Execution time Memory
46746 2018-04-22T16:49:20 Z Trans Boat (APIO16_boat) C++14
100 / 100
1096 ms 10748 KB
#include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define repd(i, a, b) for (int i = (a) - 1; i >= b; i--)
#define pb push_back
#define fi first
#define se second
#define sz(a) ((int)a.size())

typedef long long ll;

const int mod = 1e9 + 7;
const int maxn = 1e3 + 5;

int n, a[maxn], b[maxn];
ll precal[maxn][maxn], inv[maxn];
int has[maxn];
map<int, int> mm;
ll dp[maxn][maxn], f[maxn];
vector<ll> vi;

ll power_mod(ll a, ll b) {
  ll res = 1;
  while (b) {
    if (b % 2) res = res * a % mod;
    a = a * a % mod;
    b >>= 1;
  }
  return res;
}

int main() {
  rep(i, 1, maxn) {
    inv[i] = power_mod(i, mod - 2);
  }

  cin >> n;

  // mm[0];
  rep(i, 0, n) {
    cin >> a[i] >> b[i];
    mm[a[i] - 1]++;
    mm[b[i]]++;
  }

  int cnt = 0;
  for (auto it = mm.begin(); it != mm.end(); it++) {
    it->se = cnt++;
    vi.pb(it->fi);
  }

  rep(i, 0, n) {
    int hi = mm[b[i]] + 1;
    int lo = mm[a[i] - 1] + 1;
    rep(j, lo, hi) {
      has[j]++;
    }
  }

  rep(i, 1, sz(vi)) {
    int range = vi[i] - vi[i - 1];
    if (range < 2) continue;
    rep(j, 1, has[i] + 1) {
      // if (j > range) {
      //   precal[i][j] = precal[i][j - 1];
      //   continue;
      // }
      ll cur1 = 1; // Cij
      ll cur2 = (ll) range * (range - 1) / 2; // Cirange
      cur2 %= mod;
      ll res = cur1 * cur2 % mod;
      int tar = min(range - 2, j - 2);
      rep(k, 1, tar + 1) {
        cur1 = cur1 * (ll) (j  - k - 1) % mod * inv[k] % mod;
        cur2 = cur2 * (ll) (range - k - 1) % mod * inv[k + 2] % mod;
        res = (res + cur1 * cur2 % mod) % mod;
      }
      precal[i][j] = res;
    }
  }

  // return 0;

  // cout << precal[3][3] << ' ' << precal[3][2] << endl;

  ll ans = 0;
  rep(i, 0, n) {
    int hi = mm[b[i]] + 1;
    int lo = mm[a[i] - 1] + 1;
    dp[i][0] = 1; // ntc
    repd(k, hi, lo) {
      ll ways = (vi[k] - vi[k - 1]) * (i ? dp[i - 1][k - 1] : 1) % mod;
      int tot = 1;
      repd(j, i, 0) {
        if (b[j] < vi[k - 1] + 1 || a[j] > vi[k]) continue;
        tot++;
        // cout << i << ' ' << k << ' ' << vi[k] - vi[k - 1] << ' ' << tot << endl;
        assert(tot <= has[k]);
        ways += (((j ? dp[j - 1][k - 1] : 1) * precal[k][tot]) % mod);
        if (precal[k][tot] == 0) break;
        ways %= mod;
      }
      // cout << i << ' ' << k << ' ' << ways << endl;
      dp[i][k] = ways;
    }
    rep(k, 1, cnt) {
      dp[i][k] = (dp[i][k] + f[k]) % mod;
      // if (i == 2) cout << f[k] << endl;
      f[k] = dp[i][k];
      dp[i][k] = (dp[i][k] + dp[i][k - 1]) % mod;
      ans = dp[i][k];
    }
  }

  // cout << ans << endl;
  cout << ((ans - 1 + mod) % mod) << '\n';
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 11 ms 4472 KB Output is correct
2 Correct 13 ms 4472 KB Output is correct
3 Correct 11 ms 4536 KB Output is correct
4 Correct 10 ms 4536 KB Output is correct
5 Correct 11 ms 4536 KB Output is correct
6 Correct 10 ms 4536 KB Output is correct
7 Correct 10 ms 4540 KB Output is correct
8 Correct 11 ms 4540 KB Output is correct
9 Correct 10 ms 4552 KB Output is correct
10 Correct 10 ms 4560 KB Output is correct
11 Correct 10 ms 4560 KB Output is correct
12 Correct 10 ms 4692 KB Output is correct
13 Correct 10 ms 4692 KB Output is correct
14 Correct 10 ms 4692 KB Output is correct
15 Correct 10 ms 4692 KB Output is correct
16 Correct 5 ms 4692 KB Output is correct
17 Correct 5 ms 4692 KB Output is correct
18 Correct 6 ms 4692 KB Output is correct
19 Correct 5 ms 4692 KB Output is correct
20 Correct 5 ms 4692 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 4472 KB Output is correct
2 Correct 13 ms 4472 KB Output is correct
3 Correct 11 ms 4536 KB Output is correct
4 Correct 10 ms 4536 KB Output is correct
5 Correct 11 ms 4536 KB Output is correct
6 Correct 10 ms 4536 KB Output is correct
7 Correct 10 ms 4540 KB Output is correct
8 Correct 11 ms 4540 KB Output is correct
9 Correct 10 ms 4552 KB Output is correct
10 Correct 10 ms 4560 KB Output is correct
11 Correct 10 ms 4560 KB Output is correct
12 Correct 10 ms 4692 KB Output is correct
13 Correct 10 ms 4692 KB Output is correct
14 Correct 10 ms 4692 KB Output is correct
15 Correct 10 ms 4692 KB Output is correct
16 Correct 5 ms 4692 KB Output is correct
17 Correct 5 ms 4692 KB Output is correct
18 Correct 6 ms 4692 KB Output is correct
19 Correct 5 ms 4692 KB Output is correct
20 Correct 5 ms 4692 KB Output is correct
21 Correct 201 ms 8584 KB Output is correct
22 Correct 159 ms 8584 KB Output is correct
23 Correct 151 ms 8584 KB Output is correct
24 Correct 163 ms 8584 KB Output is correct
25 Correct 163 ms 8668 KB Output is correct
26 Correct 280 ms 8668 KB Output is correct
27 Correct 274 ms 8804 KB Output is correct
28 Correct 279 ms 8804 KB Output is correct
29 Correct 310 ms 8932 KB Output is correct
30 Correct 13 ms 8932 KB Output is correct
31 Correct 13 ms 8932 KB Output is correct
32 Correct 13 ms 8932 KB Output is correct
33 Correct 14 ms 8932 KB Output is correct
34 Correct 13 ms 8932 KB Output is correct
35 Correct 14 ms 8932 KB Output is correct
36 Correct 13 ms 8932 KB Output is correct
37 Correct 14 ms 8932 KB Output is correct
38 Correct 13 ms 8932 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 8932 KB Output is correct
2 Correct 6 ms 8932 KB Output is correct
3 Correct 6 ms 8932 KB Output is correct
4 Correct 7 ms 8932 KB Output is correct
5 Correct 8 ms 8932 KB Output is correct
6 Correct 10 ms 8932 KB Output is correct
7 Correct 10 ms 8932 KB Output is correct
8 Correct 10 ms 8932 KB Output is correct
9 Correct 11 ms 8932 KB Output is correct
10 Correct 10 ms 8932 KB Output is correct
11 Correct 7 ms 8932 KB Output is correct
12 Correct 5 ms 8932 KB Output is correct
13 Correct 7 ms 8932 KB Output is correct
14 Correct 6 ms 8932 KB Output is correct
15 Correct 6 ms 8932 KB Output is correct
16 Correct 6 ms 8932 KB Output is correct
17 Correct 4 ms 8932 KB Output is correct
18 Correct 4 ms 8932 KB Output is correct
19 Correct 4 ms 8932 KB Output is correct
20 Correct 4 ms 8932 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 4472 KB Output is correct
2 Correct 13 ms 4472 KB Output is correct
3 Correct 11 ms 4536 KB Output is correct
4 Correct 10 ms 4536 KB Output is correct
5 Correct 11 ms 4536 KB Output is correct
6 Correct 10 ms 4536 KB Output is correct
7 Correct 10 ms 4540 KB Output is correct
8 Correct 11 ms 4540 KB Output is correct
9 Correct 10 ms 4552 KB Output is correct
10 Correct 10 ms 4560 KB Output is correct
11 Correct 10 ms 4560 KB Output is correct
12 Correct 10 ms 4692 KB Output is correct
13 Correct 10 ms 4692 KB Output is correct
14 Correct 10 ms 4692 KB Output is correct
15 Correct 10 ms 4692 KB Output is correct
16 Correct 5 ms 4692 KB Output is correct
17 Correct 5 ms 4692 KB Output is correct
18 Correct 6 ms 4692 KB Output is correct
19 Correct 5 ms 4692 KB Output is correct
20 Correct 5 ms 4692 KB Output is correct
21 Correct 201 ms 8584 KB Output is correct
22 Correct 159 ms 8584 KB Output is correct
23 Correct 151 ms 8584 KB Output is correct
24 Correct 163 ms 8584 KB Output is correct
25 Correct 163 ms 8668 KB Output is correct
26 Correct 280 ms 8668 KB Output is correct
27 Correct 274 ms 8804 KB Output is correct
28 Correct 279 ms 8804 KB Output is correct
29 Correct 310 ms 8932 KB Output is correct
30 Correct 13 ms 8932 KB Output is correct
31 Correct 13 ms 8932 KB Output is correct
32 Correct 13 ms 8932 KB Output is correct
33 Correct 14 ms 8932 KB Output is correct
34 Correct 13 ms 8932 KB Output is correct
35 Correct 14 ms 8932 KB Output is correct
36 Correct 13 ms 8932 KB Output is correct
37 Correct 14 ms 8932 KB Output is correct
38 Correct 13 ms 8932 KB Output is correct
39 Correct 6 ms 8932 KB Output is correct
40 Correct 6 ms 8932 KB Output is correct
41 Correct 6 ms 8932 KB Output is correct
42 Correct 7 ms 8932 KB Output is correct
43 Correct 8 ms 8932 KB Output is correct
44 Correct 10 ms 8932 KB Output is correct
45 Correct 10 ms 8932 KB Output is correct
46 Correct 10 ms 8932 KB Output is correct
47 Correct 11 ms 8932 KB Output is correct
48 Correct 10 ms 8932 KB Output is correct
49 Correct 7 ms 8932 KB Output is correct
50 Correct 5 ms 8932 KB Output is correct
51 Correct 7 ms 8932 KB Output is correct
52 Correct 6 ms 8932 KB Output is correct
53 Correct 6 ms 8932 KB Output is correct
54 Correct 6 ms 8932 KB Output is correct
55 Correct 4 ms 8932 KB Output is correct
56 Correct 4 ms 8932 KB Output is correct
57 Correct 4 ms 8932 KB Output is correct
58 Correct 4 ms 8932 KB Output is correct
59 Correct 485 ms 9992 KB Output is correct
60 Correct 405 ms 9992 KB Output is correct
61 Correct 393 ms 9992 KB Output is correct
62 Correct 478 ms 9992 KB Output is correct
63 Correct 450 ms 9992 KB Output is correct
64 Correct 1096 ms 10656 KB Output is correct
65 Correct 979 ms 10656 KB Output is correct
66 Correct 1002 ms 10704 KB Output is correct
67 Correct 978 ms 10748 KB Output is correct
68 Correct 1002 ms 10748 KB Output is correct
69 Correct 401 ms 10748 KB Output is correct
70 Correct 373 ms 10748 KB Output is correct
71 Correct 380 ms 10748 KB Output is correct
72 Correct 399 ms 10748 KB Output is correct
73 Correct 405 ms 10748 KB Output is correct
74 Correct 57 ms 10748 KB Output is correct
75 Correct 72 ms 10748 KB Output is correct
76 Correct 57 ms 10748 KB Output is correct
77 Correct 60 ms 10748 KB Output is correct
78 Correct 61 ms 10748 KB Output is correct