結果

問題 No.243 出席番号(2)
ユーザー Min_25Min_25
提出日時 2016-04-23 20:49:19
言語 Python2
(2.7.18)
結果
AC  
実行時間 42 ms / 2,000 ms
コード長 1,726 bytes
コンパイル時間 87 ms
コンパイル使用メモリ 7,040 KB
実行使用メモリ 7,296 KB
最終ジャッジ日時 2024-04-15 04:20:18
合計ジャッジ時間 1,562 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 10 ms
6,400 KB
testcase_01 AC 11 ms
6,144 KB
testcase_02 AC 11 ms
6,400 KB
testcase_03 AC 15 ms
6,400 KB
testcase_04 AC 13 ms
6,400 KB
testcase_05 AC 13 ms
6,400 KB
testcase_06 AC 13 ms
6,400 KB
testcase_07 AC 13 ms
6,528 KB
testcase_08 AC 20 ms
6,656 KB
testcase_09 AC 17 ms
6,656 KB
testcase_10 AC 16 ms
6,400 KB
testcase_11 AC 14 ms
6,400 KB
testcase_12 AC 16 ms
6,528 KB
testcase_13 AC 25 ms
6,912 KB
testcase_14 AC 19 ms
6,784 KB
testcase_15 AC 17 ms
6,784 KB
testcase_16 AC 15 ms
6,656 KB
testcase_17 AC 22 ms
6,784 KB
testcase_18 AC 31 ms
6,944 KB
testcase_19 AC 22 ms
6,784 KB
testcase_20 AC 20 ms
6,784 KB
testcase_21 AC 20 ms
6,784 KB
testcase_22 AC 30 ms
6,940 KB
testcase_23 AC 32 ms
7,040 KB
testcase_24 AC 26 ms
6,912 KB
testcase_25 AC 21 ms
6,912 KB
testcase_26 AC 20 ms
6,784 KB
testcase_27 AC 42 ms
7,296 KB
testcase_28 AC 10 ms
6,272 KB
testcase_29 AC 11 ms
6,400 KB
testcase_30 AC 11 ms
6,400 KB
testcase_31 AC 11 ms
6,400 KB
testcase_32 AC 11 ms
6,400 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def ilog2(n):
  return 0 if n <= 0 else n.bit_length() - 1

def pack(pack, shamt):
  size = len(pack)
  while size > 1:
    npack = []
    for i in range(0, size - 1, 2):
      npack += [pack[i] | (pack[i+1] << shamt)]
    if size & 1:
      npack += [pack[-1]]
    pack, size, shamt = npack, (size + 1) >> 1, shamt << 1
  return pack[0]

def unpack(M, size, shamt):
  s, sizes = size, []
  while s > 1:
    sizes += [s]
    s = (s + 1) >> 1
  ret = [M]
  for size in sizes[::-1]:
    mask, nret = (1 << shamt) - 1, []
    for c in ret:
      nret += [c & mask, c >> shamt]
    ret, shamt = nret[:size], shamt >> 1
  return ret

def poly_mul_mod(f, g, mod):
  size = min(len(f), len(g))
  shift = ((mod - 1) ** 2 * size).bit_length()
  rsize = len(f) + len(g) - 1
  h = unpack(pack(f, shift) * pack(g, shift), rsize, shift * (1 << ilog2(rsize - 1)))
  return [int(x % mod) for x in h]

def prob243():
  from sys import stdin

  N = int(stdin.readline())
  MOD = 10 ** 9 + 7

  cnts = [0] * 5000
  for line in stdin:
    a = int(line)
    cnts[a] += 1

  rcnts = [0] * (N + 1)
  for i in range(N):
    if cnts[i]:
      rcnts[cnts[i]] += 1

  invs = [1] * (N + 2)
  for i in range(2, N + 2):
    invs[i] = invs[MOD % i] * (MOD - MOD // i) % MOD

  poly = [1]
  for i in range(1, N + 1):
    if rcnts[i]:
      f = []
      c = 1
      M = rcnts[i]
      for j in range(M + 1):
        f += [c % MOD]
        c = c * (M - j) % MOD * invs[j + 1] % MOD * i % MOD
      poly = f if len(poly) == 1 else poly_mul_mod(poly, f, MOD)

  facts = [1]
  for i in range(1, N + 1):
    facts += [facts[-1] * i % MOD]

  ans = 0
  for i in range(len(poly)):
    ans += (-1) ** i * poly[i] * facts[-1 - i] % MOD

  print(ans % MOD)

prob243()
0