結果

問題 No.243 出席番号(2)
ユーザー rsk0315rsk0315
提出日時 2019-08-16 00:50:13
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 47 ms / 2,000 ms
コード長 2,932 bytes
コンパイル時間 728 ms
コンパイル使用メモリ 58,788 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-19 19:42:13
合計ジャッジ時間 2,868 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <cstdio>
#include <cstdint>
#include <vector>
#include <algorithm>
#include <numeric>
#include <utility>

template <typename Tp>
Tp gcd(Tp a, Tp b, Tp& x, Tp& y) {
  x = Tp(0);
  y = Tp(1);
  for (Tp u = y, v = x; a;) {
    Tp q = b/a;
    std::swap(x -= q*u, u);
    std::swap(y -= q*v, v);
    std::swap(b -= q*a, a);
  }
  return b;
}

template <typename Tp>
Tp modinv(Tp a, Tp mod) {
  Tp x, y;
  gcd(a, mod, x, y);
  x %= mod;
  if (x < 0) x += mod;
  return x;
}

template <typename Tp>
Tp modadd(Tp a, Tp b, Tp mod) {
  a += b % mod;
  if (a < 0) a += mod;
  if (a >= mod) a -= mod;
  return a;
}

template <typename Tp>
Tp modadd(std::initializer_list<Tp> const& adds, Tp mod) {
  Tp res = 0;
  for (auto const& add: adds) {
    res += add % mod;
    if (res < 0) res += mod;
    if (res >= mod) res -= mod;
  }
  return res;
}

template <typename Tp>
Tp modsub(Tp a, Tp b, Tp mod) {
  a -= b % mod;
  if (a < 0) a += mod;
  if (a >= mod) a -= mod;
  return a;
}

template <typename Tp>
Tp modmul(std::initializer_list<Tp> const& muls, Tp mod) {
  Tp res = 1;
  for (auto const& mul: muls) (res *= mul) %= mod;
  return res;
}

template <typename Tp>
Tp modpow(Tp base, intmax_t iexp, Tp mod) {
  Tp res = 1;
  for (Tp dbl = base; iexp; iexp >>= 1) {
    if (iexp & 1) res = res * dbl % mod;
    dbl = dbl * dbl % mod;
  }
  return res;
}

class modfactorial {
  std::vector<intmax_t> fact, fact_inv;
  intmax_t mod;

public:
  modfactorial(intmax_t N, intmax_t mod): mod(mod) {
    fact.resize(N+1);
    fact_inv.resize(N+1);
    fact[0] = 1;
    for (intmax_t i = 1; i <= N; ++i)
      fact[i] = fact[i-1] * i % mod;

    fact_inv[N] = modinv(fact[N], mod);
    for (intmax_t i = N; i--;)
      fact_inv[i] = fact_inv[i+1] * (i+1) % mod;
  }

  intmax_t operator ()(intmax_t k) const {
    return fact[k];
  }

  intmax_t inverse(intmax_t k) const {
    return fact_inv[k];
  }
};

constexpr intmax_t  operator ""_jd(unsigned long long n) { return n; }
constexpr uintmax_t operator ""_ju(unsigned long long n) { return n; }
constexpr size_t    operator ""_zu(unsigned long long n) { return n; }
// constexpr ptrdiff_t operator ""_td(unsigned long long n) { return n; }

constexpr intmax_t mod = 1e9+7;

int main() {
  size_t n;
  scanf("%zu", &n);

  std::vector<int> b(n);
  for (size_t i = 0; i < n; ++i) {
    size_t a;
    scanf("%zu", &a);
    if (a >= n) continue;
    ++b[a];
  }

  std::vector<intmax_t> dp{1};
  for (size_t i = 0; i < n; ++i) {
    std::vector<intmax_t> tmp(i+2);
    for (size_t j = 0; j <= i; ++j) {
      (tmp[j] += dp[j]) %= mod;
      (tmp[j+1] += dp[j] * b[i]) %= mod;
    }
    dp = std::move(tmp);
  }

  modfactorial mf(n+1, mod);
  for (size_t i = 0; i <= n; ++i) {
    (dp[i] *= mf(n-i)) %= mod;
  }

  intmax_t res = 0;
  for (size_t i = 0; i <= n; ++i) {
    intmax_t cur = dp[i];
    if (i % 2) cur = (mod-cur) % mod;
    (res += cur) %= mod;
  }
  printf("%jd\n", res);
}
0