結果

問題 No.1001 注文の多い順列
ユーザー niuezniuez
提出日時 2020-02-28 22:17:47
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 125 ms / 2,000 ms
コード長 3,720 bytes
コンパイル時間 1,899 ms
コンパイル使用メモリ 170,504 KB
実行使用メモリ 51,632 KB
最終ジャッジ日時 2023-08-03 20:42:45
合計ジャッジ時間 4,746 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
4,716 KB
testcase_01 AC 4 ms
4,660 KB
testcase_02 AC 4 ms
4,716 KB
testcase_03 AC 4 ms
4,636 KB
testcase_04 AC 4 ms
4,720 KB
testcase_05 AC 4 ms
4,716 KB
testcase_06 AC 4 ms
4,564 KB
testcase_07 AC 4 ms
4,564 KB
testcase_08 AC 4 ms
4,644 KB
testcase_09 AC 4 ms
4,636 KB
testcase_10 AC 4 ms
4,556 KB
testcase_11 AC 4 ms
4,580 KB
testcase_12 AC 4 ms
4,608 KB
testcase_13 AC 5 ms
4,964 KB
testcase_14 AC 5 ms
5,584 KB
testcase_15 AC 6 ms
6,000 KB
testcase_16 AC 5 ms
5,488 KB
testcase_17 AC 5 ms
5,592 KB
testcase_18 AC 64 ms
29,908 KB
testcase_19 AC 60 ms
28,536 KB
testcase_20 AC 40 ms
20,992 KB
testcase_21 AC 38 ms
20,148 KB
testcase_22 AC 76 ms
34,488 KB
testcase_23 AC 74 ms
33,780 KB
testcase_24 AC 73 ms
33,520 KB
testcase_25 AC 72 ms
32,976 KB
testcase_26 AC 125 ms
49,884 KB
testcase_27 AC 121 ms
48,612 KB
testcase_28 AC 115 ms
46,616 KB
testcase_29 AC 28 ms
17,840 KB
testcase_30 AC 29 ms
18,012 KB
testcase_31 AC 30 ms
18,696 KB
testcase_32 AC 25 ms
16,692 KB
testcase_33 AC 125 ms
51,632 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
#define rep(i,s,e) for(i64 (i) = (s);(i) < (e);(i)++)
#define all(x) x.begin(),x.end()

template<class T>
static inline std::vector<T> ndvec(size_t&& n, T val) noexcept {
  return std::vector<T>(n, std::forward<T>(val));
}

template<class... Tail>
static inline auto ndvec(size_t&& n, Tail&&... tail) noexcept {
  return std::vector<decltype(ndvec(std::forward<Tail>(tail)...))>(n, ndvec(std::forward<Tail>(tail)...));
}

#include <bits/stdc++.h>
using namespace std;
using i64 = long long;

template<i64 M>
constexpr i64 euinv(i64 val) {
    i64 a = M, b = val;
    i64 x = 0, u = 1;
    while (b) {
        i64 t = a / b;
        swap(a -= t * b, b);
        swap(x -= t * u, u);
    }
    return x < 0 ? x + M : x;
}

template<i64 M>
struct modint {
  i64 a;
  constexpr modint(const i64 x = 0) noexcept: a((x % M + M) % M) {}
  constexpr i64 value() const noexcept { return a; }
  constexpr modint inv() const noexcept { return modint(euinv<M>(a)); }
  constexpr modint pow(i64 r) const noexcept {
    modint ans(1);
    modint aa = *this;
    while(r) {
      if(r & 1) {
        ans *= aa;
      }
      aa *= aa;
      r >>= 1;
    }
    return ans;
  }
  constexpr modint& operator+=(const modint r) noexcept {
    a += r.a;
    if(a >= M) a -= M;
    return *this;
  }
  constexpr modint& operator=(const i64 r) {
    a = (r % M + M) % M;
    return *this;
  }
  constexpr modint& operator-=(const modint r) noexcept {
    a -= r.a;
    if(a < 0) a += M;
    return *this;
  }
  constexpr modint& operator*=(const modint r) noexcept {
    a = a * r.a % M;
    return *this;
  }
  constexpr modint& operator/=(modint r) noexcept {
    i64 ex = M - 2;
    while(ex) {
      if(ex & 1) {
        *this *= r;
      }
      r *= r;
      ex >>= 1;
    }
    return *this;
  }

  constexpr modint operator+(const modint r) const {
    return modint(*this) += r;
  }
  constexpr modint operator-(const modint r) const {
    return modint(*this) -= r;
  }
  constexpr modint operator*(const modint r) const {
    return modint(*this) *= r;
  }
  constexpr modint operator/(const modint r) const {
    return modint(*this) /= r;
  }

  constexpr bool operator!=(const modint r) const {
    return this->value() != r.value();
  }

};

template<const i64 M>
std::ostream& operator<<(std::ostream& os, const modint<M>& m) {
  os << m.value();
  return os;
}

using fp = modint<(i64)(1e9 + 7)>;


fp dp[3010][6020];

int main() {
  i64 N;
  cin >> N;
  vector<i64> T(N), X(N);
  rep(i,0,N) {
    cin >> T[i] >> X[i];
  }
  vector<i64> en(N + 1);
  vector<i64> st(N + 1);
  rep(i,0,N) {
    if(T[i] == 1) st[X[i]]++;
    else en[X[i]]++;
  }
  rep(i,0,N) {
    st[i + 1] += st[i];
    en[i + 1] += en[i];
  }

  vector<fp> fact(100000);
  vector<fp> invf(100000);
  fact[0] = fp(1);
  rep(i,1,fact.size()) {
    fact[i] = fact[i - 1] * fp(i);
  }
  invf.back() = fp(1) / fact.back();
  for(i64 i = invf.size() - 1; i --> 0;) {
    invf[i] = invf[i + 1] * fp(i + 1);
  }

  dp[0][0] = fp(1);
  rep(i,0,N) {
    rep(j,0,N + 1) {
      if(j > st[i + 1]) continue;
      // j <= en[i + 1]
      if(st[i + 1] - j >= 1) {
        // use (i + 1)
        i64 remain = i - j - en[i];
        if(remain >= en[i + 1] - en[i]) {
          i64 r = en[i + 1] - en[i];
          dp[i + 1][j + 1] += dp[i][j] * (st[i + 1] - j) * fact[remain] * invf[remain - r];
        }
      }
      {
        // dont use (i + 1)
        i64 remain = i + 1 - j - en[i];
        if(remain >= en[i + 1] - en[i]) {
          i64 r = en[i + 1] - en[i];
          dp[i + 1][j] += dp[i][j] * fact[remain] * invf[remain - r];
        }
      }
    }
  }

  cout << dp[N][st.back()] << endl;
}
0