結果

問題 No.1001 注文の多い順列
ユーザー krotonkroton
提出日時 2020-03-02 17:00:24
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 98 ms / 2,000 ms
コード長 3,905 bytes
コンパイル時間 1,207 ms
コンパイル使用メモリ 106,884 KB
実行使用メモリ 73,592 KB
最終ジャッジ日時 2023-08-04 00:12:26
合計ジャッジ時間 3,294 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,384 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 3 ms
4,376 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 2 ms
4,384 KB
testcase_18 AC 57 ms
61,380 KB
testcase_19 AC 52 ms
55,380 KB
testcase_20 AC 33 ms
36,908 KB
testcase_21 AC 30 ms
33,392 KB
testcase_22 AC 70 ms
73,540 KB
testcase_23 AC 69 ms
73,380 KB
testcase_24 AC 62 ms
73,060 KB
testcase_25 AC 66 ms
71,480 KB
testcase_26 AC 73 ms
70,364 KB
testcase_27 AC 73 ms
71,636 KB
testcase_28 AC 71 ms
71,948 KB
testcase_29 AC 54 ms
72,684 KB
testcase_30 AC 54 ms
69,640 KB
testcase_31 AC 57 ms
72,800 KB
testcase_32 AC 54 ms
73,592 KB
testcase_33 AC 98 ms
73,532 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <vector>
using namespace std;
using ll = long long;
#define fst first
#define snd second

/* clang-format off */
template <class T, size_t D> struct _vec { using type = vector<typename _vec<T, D - 1>::type>; };
template <class T> struct _vec<T, 0> { using type = T; };
template <class T, size_t D> using vec = typename _vec<T, D>::type;
template <class T> vector<T> make_v(size_t size, const T& init) { return vector<T>(size, init); }
template <class... Ts> auto make_v(size_t size, Ts... rest) { return vector<decltype(make_v(rest...))>(size, make_v(rest...)); }
template <class T> inline void chmin(T &a, const T& b) { if (b < a) a = b; }
template <class T> inline void chmax(T &a, const T& b) { if (b > a) a = b; }
/* clang-format on */

template <std::uint_fast64_t Modulus>
class modint {
  using u64 = std::uint_fast64_t;

 public:
  u64 a;

  constexpr modint(const u64 x = 0) noexcept
      : a(x % Modulus) {
  }
  constexpr u64 &value() noexcept {
    return a;
  }
  constexpr const u64 &value() const noexcept {
    return a;
  }
  constexpr modint operator+(const modint rhs) const noexcept {
    return modint(*this) += rhs;
  }
  constexpr modint operator-(const modint rhs) const noexcept {
    return modint(*this) -= rhs;
  }
  constexpr modint operator*(const modint rhs) const noexcept {
    return modint(*this) *= rhs;
  }
  constexpr modint operator/(const modint rhs) const noexcept {
    return modint(*this) /= rhs;
  }
  constexpr modint &operator+=(const modint rhs) noexcept {
    a += rhs.a;
    if (a >= Modulus) {
      a -= Modulus;
    }
    return *this;
  }
  constexpr modint &operator-=(const modint rhs) noexcept {
    if (a < rhs.a) {
      a += Modulus;
    }
    a -= rhs.a;
    return *this;
  }
  constexpr modint &operator*=(const modint rhs) noexcept {
    a = a * rhs.a % Modulus;
    return *this;
  }
  constexpr modint &operator/=(const modint rhs) noexcept {
    return *this *= ~rhs;
  }
  constexpr modint power(u64 exp) const noexcept {
    modint v = 1, x = *this;
    while (exp) {
      if (exp & 1) {
        v *= x;
      }
      x *= x;
      exp >>= 1;
    }
    return v;
  }
  constexpr modint operator~() const noexcept {
    return power(Modulus - 2);
  }
};

using mint = modint<1000000007>;

vector<mint> fact, factInv;
void initFact(int N) {
  fact.resize(N + 1);
  factInv.resize(N + 1);
  fact[0] = 1;
  for (int i = 1; i <= N; i++) fact[i] = fact[i - 1] * i;
  factInv[N] = ~fact[N];
  for (int i = N - 1; i >= 0; i--) factInv[i] = factInv[i + 1] * (i + 1);
}

int main() {
#ifdef DEBUG
  ifstream ifs("in.txt");
  cin.rdbuf(ifs.rdbuf());
#endif
  int N;
  while (cin >> N) {
    using pii = pair<int, int>;
    vector<pii> X(N);
    for (auto& x : X) {
      cin >> x.snd >> x.fst;
      if (x.snd == 1) {
        --x.fst;
      }
    }
    sort(X.begin(), X.end());
    vec<mint, 2> dp = make_v(N + 1, N + 1, mint(0));
    dp[0][0] = 1;
    int gre = 0;
    for (int i = 0; i < N; i++) {
      for (int j = 0; j <= gre; j++) {
        int used = j + (i - gre);
        int rem = max(0, X[i].fst - used);
        if (X[i].snd == 0) {
          dp[i + 1][j] += dp[i][j] * rem;
        } else {
          dp[i + 1][j + 1] += dp[i][j] * rem;
          dp[i + 1][j] += dp[i][j];
        }
      }
      if (X[i].snd == 1) {
        ++gre;
      }
    }
    mint res = 0;
    initFact(N + 10);
    for (int i = 0; i <= gre; i++) {
      mint way = dp[N][i] * fact[gre - i];
      if (i % 2 == 0) {
        res += way;
      } else {
        res -= way;
      }
    }
    cout << res.value() << endl;
  }
  return 0;
}
0