답안 #46726

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
46726 2018-04-22T16:00:45 Z Trans Boat (APIO16_boat) C++14
58 / 100
2000 ms 13124 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];
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, 1, sz(vi)) {
    int range = vi[i] - vi[i - 1];
    if (range < 2) continue;
    rep(j, 1, n + 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;
    }
  }

  // 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;
        ways += (((j ? dp[j - 1][k - 1] : 1) * precal[k][tot]) % mod);
        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;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 961 ms 8440 KB Output is correct
2 Correct 935 ms 8576 KB Output is correct
3 Correct 945 ms 8576 KB Output is correct
4 Correct 935 ms 8640 KB Output is correct
5 Correct 917 ms 8640 KB Output is correct
6 Correct 925 ms 8760 KB Output is correct
7 Correct 910 ms 8760 KB Output is correct
8 Correct 940 ms 8776 KB Output is correct
9 Correct 919 ms 8848 KB Output is correct
10 Correct 949 ms 9028 KB Output is correct
11 Correct 951 ms 9028 KB Output is correct
12 Correct 939 ms 9028 KB Output is correct
13 Correct 964 ms 9028 KB Output is correct
14 Correct 927 ms 9028 KB Output is correct
15 Correct 939 ms 9028 KB Output is correct
16 Correct 179 ms 9028 KB Output is correct
17 Correct 245 ms 9028 KB Output is correct
18 Correct 173 ms 9028 KB Output is correct
19 Correct 187 ms 9028 KB Output is correct
20 Correct 186 ms 9028 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 961 ms 8440 KB Output is correct
2 Correct 935 ms 8576 KB Output is correct
3 Correct 945 ms 8576 KB Output is correct
4 Correct 935 ms 8640 KB Output is correct
5 Correct 917 ms 8640 KB Output is correct
6 Correct 925 ms 8760 KB Output is correct
7 Correct 910 ms 8760 KB Output is correct
8 Correct 940 ms 8776 KB Output is correct
9 Correct 919 ms 8848 KB Output is correct
10 Correct 949 ms 9028 KB Output is correct
11 Correct 951 ms 9028 KB Output is correct
12 Correct 939 ms 9028 KB Output is correct
13 Correct 964 ms 9028 KB Output is correct
14 Correct 927 ms 9028 KB Output is correct
15 Correct 939 ms 9028 KB Output is correct
16 Correct 179 ms 9028 KB Output is correct
17 Correct 245 ms 9028 KB Output is correct
18 Correct 173 ms 9028 KB Output is correct
19 Correct 187 ms 9028 KB Output is correct
20 Correct 186 ms 9028 KB Output is correct
21 Correct 280 ms 10880 KB Output is correct
22 Correct 271 ms 10888 KB Output is correct
23 Correct 264 ms 10888 KB Output is correct
24 Correct 272 ms 11304 KB Output is correct
25 Correct 277 ms 11304 KB Output is correct
26 Correct 350 ms 11304 KB Output is correct
27 Correct 383 ms 11304 KB Output is correct
28 Correct 340 ms 11304 KB Output is correct
29 Correct 349 ms 11304 KB Output is correct
30 Correct 896 ms 12760 KB Output is correct
31 Correct 892 ms 12792 KB Output is correct
32 Correct 890 ms 12792 KB Output is correct
33 Correct 924 ms 12792 KB Output is correct
34 Correct 957 ms 12792 KB Output is correct
35 Correct 890 ms 12792 KB Output is correct
36 Correct 882 ms 12792 KB Output is correct
37 Correct 913 ms 12792 KB Output is correct
38 Correct 881 ms 12792 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 12792 KB Output is correct
2 Correct 19 ms 12792 KB Output is correct
3 Correct 19 ms 12792 KB Output is correct
4 Correct 19 ms 12792 KB Output is correct
5 Correct 20 ms 12792 KB Output is correct
6 Correct 20 ms 12792 KB Output is correct
7 Correct 20 ms 12792 KB Output is correct
8 Correct 21 ms 12792 KB Output is correct
9 Correct 27 ms 12792 KB Output is correct
10 Correct 25 ms 12792 KB Output is correct
11 Correct 19 ms 12792 KB Output is correct
12 Correct 19 ms 12792 KB Output is correct
13 Correct 20 ms 12792 KB Output is correct
14 Correct 21 ms 12792 KB Output is correct
15 Correct 20 ms 12792 KB Output is correct
16 Correct 10 ms 12792 KB Output is correct
17 Correct 10 ms 12792 KB Output is correct
18 Correct 10 ms 12792 KB Output is correct
19 Correct 12 ms 12792 KB Output is correct
20 Correct 11 ms 12792 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 961 ms 8440 KB Output is correct
2 Correct 935 ms 8576 KB Output is correct
3 Correct 945 ms 8576 KB Output is correct
4 Correct 935 ms 8640 KB Output is correct
5 Correct 917 ms 8640 KB Output is correct
6 Correct 925 ms 8760 KB Output is correct
7 Correct 910 ms 8760 KB Output is correct
8 Correct 940 ms 8776 KB Output is correct
9 Correct 919 ms 8848 KB Output is correct
10 Correct 949 ms 9028 KB Output is correct
11 Correct 951 ms 9028 KB Output is correct
12 Correct 939 ms 9028 KB Output is correct
13 Correct 964 ms 9028 KB Output is correct
14 Correct 927 ms 9028 KB Output is correct
15 Correct 939 ms 9028 KB Output is correct
16 Correct 179 ms 9028 KB Output is correct
17 Correct 245 ms 9028 KB Output is correct
18 Correct 173 ms 9028 KB Output is correct
19 Correct 187 ms 9028 KB Output is correct
20 Correct 186 ms 9028 KB Output is correct
21 Correct 280 ms 10880 KB Output is correct
22 Correct 271 ms 10888 KB Output is correct
23 Correct 264 ms 10888 KB Output is correct
24 Correct 272 ms 11304 KB Output is correct
25 Correct 277 ms 11304 KB Output is correct
26 Correct 350 ms 11304 KB Output is correct
27 Correct 383 ms 11304 KB Output is correct
28 Correct 340 ms 11304 KB Output is correct
29 Correct 349 ms 11304 KB Output is correct
30 Correct 896 ms 12760 KB Output is correct
31 Correct 892 ms 12792 KB Output is correct
32 Correct 890 ms 12792 KB Output is correct
33 Correct 924 ms 12792 KB Output is correct
34 Correct 957 ms 12792 KB Output is correct
35 Correct 890 ms 12792 KB Output is correct
36 Correct 882 ms 12792 KB Output is correct
37 Correct 913 ms 12792 KB Output is correct
38 Correct 881 ms 12792 KB Output is correct
39 Correct 19 ms 12792 KB Output is correct
40 Correct 19 ms 12792 KB Output is correct
41 Correct 19 ms 12792 KB Output is correct
42 Correct 19 ms 12792 KB Output is correct
43 Correct 20 ms 12792 KB Output is correct
44 Correct 20 ms 12792 KB Output is correct
45 Correct 20 ms 12792 KB Output is correct
46 Correct 21 ms 12792 KB Output is correct
47 Correct 27 ms 12792 KB Output is correct
48 Correct 25 ms 12792 KB Output is correct
49 Correct 19 ms 12792 KB Output is correct
50 Correct 19 ms 12792 KB Output is correct
51 Correct 20 ms 12792 KB Output is correct
52 Correct 21 ms 12792 KB Output is correct
53 Correct 20 ms 12792 KB Output is correct
54 Correct 10 ms 12792 KB Output is correct
55 Correct 10 ms 12792 KB Output is correct
56 Correct 10 ms 12792 KB Output is correct
57 Correct 12 ms 12792 KB Output is correct
58 Correct 11 ms 12792 KB Output is correct
59 Execution timed out 2082 ms 13124 KB Time limit exceeded
60 Halted 0 ms 0 KB -