結果

問題 No.907 Continuous Kadomatu
ユーザー pekempeypekempey
提出日時 2019-07-15 20:56:59
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 31 ms / 2,000 ms
コード長 3,391 bytes
コンパイル時間 1,019 ms
コンパイル使用メモリ 86,144 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-03 16:05:18
合計ジャッジ時間 2,240 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

const int MOD = 1000000007;

struct mint {
  int n;
  mint(int n_ = 0) : n(n_) {}
  friend mint operator-(mint a) { return -a.n + MOD * (a.n != 0); }
  friend mint operator+(mint a, mint b) { int x = a.n + b.n; return x - (x >= MOD) * MOD; }
  friend mint operator-(mint a, mint b) { int x = a.n - b.n; return x + (x < 0) * MOD; }
  friend mint operator*(mint a, mint b) { return (long long)a.n * b.n % MOD; }
  friend mint &operator+=(mint &a, mint b) { return a = a + b; }
  friend mint &operator-=(mint &a, mint b) { return a = a - b; }
  friend mint &operator*=(mint &a, mint b) { return a = a * b; }
  friend bool operator==(mint a, mint b) { return a.n == b.n; }
  friend bool operator!=(mint a, mint b) { return a.n != b.n; }
  friend istream &operator>>(istream &i, mint &a) { return i >> a.n; }
  friend ostream &operator<<(ostream &o, mint a) { return o << a.n; }
};

mint modinv(mint n) {
  int a = n.n;
  int b = MOD;
  int s = 1;
  int t = 0;
  while (b != 0) {
    int q = a / b;
    a -= q * b;
    s -= q * t;
    swap(a, b);
    swap(s, t);
  }
  if (s < 0) s += MOD;
  return s;
}

vector<mint> F_{1, 1}, R_{1, 1}, I_{0, 1};

void check_fact(int n) {
  for (int i = I_.size(); i <= n; i++) {
    I_.push_back(I_[MOD % i] * (MOD - MOD / i));
    F_.push_back(F_[i - 1] * i);
    R_.push_back(R_[i - 1] * I_[i]);
  }
}

mint I(int n) { check_fact(abs(n)); return n >= 0 ? I_[n] : -I_[-n]; }
mint F(int n) { check_fact(n); return n < 0 ? 0 : F_[n]; }
mint R(int n) { check_fact(n); return n < 0 ? 0 : R_[n]; }
mint C(int n, int r) { return F(n) * R(n - r) * R(r); }
mint P(int n, int r) { return F(n) * R(n - r); }
mint H(int n, int r) { return n == 0 ? (r == 0) : C(n + r - 1, r); }


int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<int> a(n), b(n), c;
  vector<mint> inv(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i];
    c.push_back(a[i]);
    c.push_back(b[i]);
    inv[i] = modinv(b[i] - a[i]);
  }
  c.push_back((int)1e9 + 1);
  c.push_back((int)1e9 + 2);
  sort(c.begin(), c.end());
  c.erase(unique(c.begin(), c.end()), c.end());
  const int m = c.size() - 1;
  vector<mint> f(n+1);
  f[0] = 1;
  f[1] = 1;
  for (int i = 2; i <= n; i++) {
    for (int j = 1; j <= i-1; j += 2) {
      f[i] += f[j] * f[i-1-j] * C(i-1, j);
    }
  }
  for (int i = 2; i <= n; i++) {
    f[i] *= R(i);
  }
  vector<vector<mint>> dp(n+1, vector<mint>(m));
  auto index = [&](int x) -> int {
    return lower_bound(c.begin(), c.end(), x) - c.begin();
  };
  for (int i = 0; i < n; i++) {
    a[i] = index(a[i]);
    b[i] = index(b[i]);
  }
  dp[0][m-1] = 1;
  for (int i = 0; i < n; i++) {
    if (i % 2 == 0) {
      mint s = 0;
      for (int j = m-1; j >= 0; j--) {
        mint ns = s + dp[i][j];
        dp[i][j] = s;
        s = ns;
      }
    } else {
      mint s = 0;
      for (int j = 0; j < m; j++) {
        mint ns = s + dp[i][j];
        dp[i][j] = s;
        s = ns;
      }
    }
    for (int j = 0; j < m; j++) {
      mint p = 1;
      for (int k = i; k < n; k++) {
        if (a[k] <= j && j < b[k]) {
          p *= (c[j+1]-c[j])*inv[k];
        } else break;
        dp[k+1][j] += dp[i][j] * p * f[k-i+1];
      }
    }
  }
  mint ans = 0;
  for (int j = 0; j < m; j++) {
    ans += dp[n][j];
  }
  cout << ans << endl;
}

0