結果

問題 No.907 Continuous Kadomatu
ユーザー risujirohrisujiroh
提出日時 2019-10-11 23:38:33
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 76 ms / 2,000 ms
コード長 4,171 bytes
コンパイル時間 2,869 ms
コンパイル使用メモリ 184,264 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-16 20:24:10
合計ジャッジ時間 4,177 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 10 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 4 ms
4,376 KB
testcase_08 AC 8 ms
4,376 KB
testcase_09 AC 5 ms
4,376 KB
testcase_10 AC 17 ms
4,380 KB
testcase_11 AC 24 ms
4,376 KB
testcase_12 AC 57 ms
4,376 KB
testcase_13 AC 64 ms
4,376 KB
testcase_14 AC 72 ms
4,380 KB
testcase_15 AC 69 ms
4,380 KB
testcase_16 AC 69 ms
4,380 KB
testcase_17 AC 76 ms
4,376 KB
testcase_18 AC 71 ms
4,376 KB
testcase_19 AC 75 ms
4,376 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 3 ms
4,380 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 73 ms
4,380 KB
testcase_24 AC 73 ms
4,376 KB
testcase_25 AC 1 ms
4,376 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 1 ms
4,380 KB
testcase_28 AC 1 ms
4,380 KB
testcase_29 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using lint = long long;
template<class T = int> using V = vector<T>;
template<class T = int> using VV = V< V<T> >;

template<unsigned P> struct ModInt {
  using M = ModInt;
  unsigned v;
  constexpr ModInt() : v(0) {}
  constexpr ModInt(unsigned _v, int) : v(_v) {}
  template<class Z> ModInt(const Z& a) : v((a < 0 ? P - -a % P : a) % P) {}
  static constexpr unsigned p() { return P; }
  M operator+() const { return *this; }
  M operator-() const { return {v ? P - v : 0, 0}; }
  explicit operator bool() const noexcept { return v; }
  bool operator!() const noexcept { return !(bool)*this; }
  M& operator*=(M r) { v = (uint64_t)v * r.v % P; return *this; }
  M& operator/=(M r) { return *this *= r.inv(); }
  M& operator+=(M r) { if ((v += r.v) >= P) v -= P; return *this; }
  M& operator-=(M r) { if ((v += P - r.v) >= P) v -= P; return *this; }
  M inv() const {
    int a = v, b = P, x = 1, u = 0;
    while (b) {
      int q = a / b;
      swap(a -= q * b, b);
      swap(x -= q * u, u);
    }
    assert(a == 1);
    return x;
  }
  template<class Z> M pow(Z n) const {
    if (n < 0) return pow(-n).inv();
    M res = 1;
    for (M a = *this; n; a *= a, n >>= 1) if (n & 1) res *= a;
    return res;
  }
  friend M operator*(M l, M r) { return M(l) *= r; }
  friend M operator/(M l, M r) { return M(l) /= r; }
  friend M operator+(M l, M r) { return M(l) += r; }
  friend M operator-(M l, M r) { return M(l) -= r; }
  friend ostream& operator<<(ostream& os, M r) { return os << r.v; }
  friend bool operator==(M l, M r) { return l.v == r.v; }
  friend bool operator!=(M l, M r) { return !(l == r); }
};
using Mint = ModInt<(unsigned)1e9 + 7>;
V<Mint> fact, ifact, inv, powB;
void init(int n, int B = 2) {
  fact.resize(n + 1);
  fact[0] = 1;
  for (int i = 1; i <= n; ++i) {
    fact[i] = i * fact[i - 1];
  }
  ifact.resize(n + 1);
  ifact[n] = 1 / fact[n];
  for (int i = n; i > 0; --i) {
    ifact[i - 1] = i * ifact[i];
  }
  inv.resize(n + 1);
  inv[1] = 1;
  for (int i = 2; i <= n; ++i) {
    int q = Mint::p() / i;
    inv[i] = -q * inv[Mint::p() - i * q];
  }
  powB.resize(n + 1);
  powB[0] = 1;
  for (int i = 0; i < n; ++i) {
    powB[i + 1] = powB[i] * B;
  }
}
Mint comb(int n, int r) {
  if (r < 0 or r > n) return 0;
  return fact[n] * ifact[r] * ifact[n - r];
}

int main() {
  cin.tie(nullptr); ios::sync_with_stdio(false);
  int n; cin >> n;
  V<> a(n), b(n); for (int i = 0; i < n; ++i) cin >> a[i] >> b[i];
  auto v = a;
  v.insert(end(v), begin(b), end(b));
  sort(begin(v), end(v));
  v.erase(unique(begin(v), end(v)), end(v));
  int m = v.size() - 1;
  init(n);

  auto len = [](int l0, int r0, int l1, int r1) -> Mint {
    return max(min(r0, r1) - max(l0, l1), 0);
  };
  VV<Mint> p(n, V<Mint>(m + 1));
  for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) {
    p[i][j] = len(a[i], b[i], v[j], v[j + 1]) / (b[i] - a[i]);
  }

  V<Mint> pr(n + 1, 1), ipr(n + 1, 1);
  for (int i = 1; i < n; ++i) {
    pr[i + 1] = 0;
    for (int k = 0; k <= i; ++k) {
      pr[i + 1] += comb(i, k) * pr[k] * pr[i - k];
    }
    pr[i + 1] /= 2;
  }
  for (int i = 1; i <= n; ++i) {
    pr[i] *= ifact[i];
    ipr[i] = 1 / pr[i];
  }

  VV<Mint> dp(m, V<Mint>(n + 1));
  for (int j = 0; j < m; ++j) {
    dp[j][1] = p[0][j];
  }
  for (int i = 1; i < n; ++i) {
    V<Mint> sum(m + 1);
    for (int j = 0; j < m; ++j) for (int k = 1; k <= i; ++k) {
      sum[j] += dp[j][k];
    }
    for (int j = m - 1; j >= 0; --j) {
      sum[j] += sum[j + 1];
    }
    VV<Mint> ndp(m, V<Mint>(n + 1));
    if (i & 1) {
      for (int j = 0; j < m; ++j) {
        ndp[j][1] = p[i][j] * (sum[0] - sum[j]);
        for (int k = 2; k <= i + 1; ++k) {
          ndp[j][k] = p[i][j] * dp[j][k - 1] * ipr[k - 1] * pr[k];
        }
      }
    } else {
      for (int j = 0; j < m; ++j) {
        ndp[j][1] = p[i][j] * (sum[j + 1] - sum[m]);
        for (int k = 2; k <= i + 1; ++k) {
          ndp[j][k] = p[i][j] * dp[j][k - 1] * ipr[k - 1] * pr[k];
        }
      }
    }
    swap(dp, ndp);
  }
  Mint res;
  for (int j = 0; j < m; ++j) for (int k = 1; k <= n; ++k) {
    res += dp[j][k];
  }
  cout << res << '\n';
}
0