結果

問題 No.1001 注文の多い順列
ユーザー pekempeypekempey
提出日時 2020-02-28 21:55:17
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 587 ms / 2,000 ms
コード長 2,058 bytes
コンパイル時間 1,970 ms
コンパイル使用メモリ 180,208 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-03 20:09:24
合計ジャッジ時間 11,558 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 3 ms
4,380 KB
testcase_14 AC 4 ms
4,380 KB
testcase_15 AC 7 ms
4,380 KB
testcase_16 AC 4 ms
4,376 KB
testcase_17 AC 4 ms
4,376 KB
testcase_18 AC 476 ms
4,380 KB
testcase_19 AC 428 ms
4,380 KB
testcase_20 AC 277 ms
4,380 KB
testcase_21 AC 248 ms
4,380 KB
testcase_22 AC 579 ms
4,376 KB
testcase_23 AC 576 ms
4,376 KB
testcase_24 AC 572 ms
4,376 KB
testcase_25 AC 559 ms
4,376 KB
testcase_26 AC 535 ms
4,376 KB
testcase_27 AC 546 ms
4,376 KB
testcase_28 AC 545 ms
4,380 KB
testcase_29 AC 583 ms
4,380 KB
testcase_30 AC 558 ms
4,376 KB
testcase_31 AC 586 ms
4,376 KB
testcase_32 AC 568 ms
4,380 KB
testcase_33 AC 587 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repr(i, n) for (int i = (n) - 1; i >= 0; i--)
#define range(a) a.begin(), a.end()

constexpr int MOD = 1000000007;

class mint {
  int n;
public:
  mint(int n_ = 0) : n(n_) {}
  explicit operator int() { return 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 f(int n) {
  mint res = 1;
  rep(i, n) {
    res *= i + 1;
  }
  return res;
}

int main() {
  int N; cin >> N;
  vector<int> T(N), X(N);
  rep(i, N) {
    cin >> T[i] >> X[i];
    if (T[i] == 1) {
      X[i]--;
    }
  }
  vector<int> ord(N); rep(i, N) ord[i] = i;
  sort(ord.begin(), ord.end(), [&](int i, int j) {
    return X[i] < X[j];
    });
  vector<vector<mint>> dp(N + 1, vector<mint>(2));
  dp[0][0] = 1;
  rep(i_, N) {
    int i = ord[i_];
    vector<vector<mint>> ep(N + 1, vector<mint>(2));
    rep(j, X[i]) rep(k, 2) {
      if (T[i] == 0) {
        ep[j + 1][k] += dp[j][k] * (X[i] - j);
      } else {
        ep[j + 1][k ^ 1] += dp[j][k] * (X[i] - j);
      }
    }
    rep(j, N) rep(k, 2) {
      if (T[i] == 1) {
        ep[j][k] += dp[j][k];
      }
    }
    dp = ep;
  }
  mint ans;
  rep(i, N + 1) {
    ans += dp[i][0] * f(N - i);
    ans -= dp[i][1] * f(N - i);
  }
  cout << ans << endl;
}
0