結果

問題 No.243 出席番号(2)
ユーザー lam6er
提出日時 2025-04-16 16:23:11
言語 PyPy3
(7.3.15)
結果
MLE  
実行時間 -
コード長 825 bytes
コンパイル時間 270 ms
コンパイル使用メモリ 82,348 KB
実行使用メモリ 75,292 KB
最終ジャッジ日時 2025-04-16 16:24:13
合計ジャッジ時間 3,124 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 6 MLE * 24
権限があれば一括ダウンロードができます

ソースコード

diff #

MOD = 10**9 + 7

N = int(input())
A = [int(input()) for _ in range(N)]

from collections import defaultdict

freq = defaultdict(int)
for a in A:
    freq[a] += 1

# Precompute factorials modulo MOD
fact = [1] * (N + 1)
for i in range(1, N + 1):
    fact[i] = fact[i-1] * i % MOD

# Initialize DP array
dp = [0] * (N + 2)  # Extra space to avoid index issues
dp[0] = 1
current_max_degree = 0

for x in freq:
    c = freq[x]
    # Iterate from current_max_degree down to 0
    for k in range(current_max_degree, -1, -1):
        dp[k + 1] = (dp[k + 1] + dp[k] * c) % MOD
    current_max_degree += 1

# Calculate the answer
ans = 0
for k in range(0, current_max_degree + 1):
    if k > N:
        continue
    sign = -1 if k % 2 else 1
    term = (sign * dp[k] * fact[N - k]) % MOD
    ans = (ans + term) % MOD

print(ans % MOD)
0