結果

問題 No.2381 Gift Exchange Party
ユーザー miya145592miya145592
提出日時 2023-07-14 22:56:49
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 238 ms / 2,000 ms
コード長 1,195 bytes
コンパイル時間 1,821 ms
コンパイル使用メモリ 86,888 KB
実行使用メモリ 145,456 KB
最終ジャッジ日時 2023-10-14 13:16:59
合計ジャッジ時間 5,936 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 74 ms
71,044 KB
testcase_01 AC 73 ms
71,084 KB
testcase_02 AC 117 ms
96,096 KB
testcase_03 AC 90 ms
83,228 KB
testcase_04 AC 116 ms
107,320 KB
testcase_05 AC 148 ms
137,232 KB
testcase_06 AC 112 ms
103,032 KB
testcase_07 AC 112 ms
102,844 KB
testcase_08 AC 138 ms
129,812 KB
testcase_09 AC 83 ms
76,032 KB
testcase_10 AC 101 ms
93,080 KB
testcase_11 AC 122 ms
111,624 KB
testcase_12 AC 87 ms
77,324 KB
testcase_13 AC 106 ms
95,988 KB
testcase_14 AC 140 ms
119,088 KB
testcase_15 AC 99 ms
90,652 KB
testcase_16 AC 102 ms
93,504 KB
testcase_17 AC 141 ms
129,468 KB
testcase_18 AC 113 ms
103,444 KB
testcase_19 AC 116 ms
106,980 KB
testcase_20 AC 238 ms
145,284 KB
testcase_21 AC 153 ms
145,456 KB
testcase_22 AC 238 ms
145,224 KB
testcase_23 AC 238 ms
145,408 KB
testcase_24 AC 107 ms
99,780 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def nPr(n, r, mod):
    if ( r<0 or r>n ):
        return 0
    return g1[n] * g2[n-r] % mod

def nCr(n, r, mod):
    if ( r<0 or r>n ):
        return 0
    r = min(r, n-r)
    return (g1[n] * g2[r] % mod) * g2[n-r] % mod

MOD = 998244353
N, P = map(int, input().split())

g1 = [1, 1] # 元テーブル
g2 = [1, 1] #逆元テーブル
inverse = [0, 1] #逆元テーブル計算用テーブル
fact = [1, 1]
fact_inv = [1, 1]

for i in range( 2, N + 1 ):
    g1.append( ( g1[-1] * i ) % MOD )
    inverse.append( ( -inverse[MOD % i] * (MOD//i) ) % MOD )
    g2.append( (g2[-1] * inverse[-1]) % MOD )
    fact.append( (fact[-1] * i) % MOD )
    fact_inv.append(fact_inv[-1] * inverse[-1] % MOD)

all = fact[N]

ng = 1
for i in range(1, N+1):
    if P*i>N:
        break
    tmp = 1
    tmp *= fact[N]
    tmp %= MOD
    tmp *= fact_inv[N-P*i]
    tmp %= MOD
    tmp *= pow(fact_inv[P], i, MOD)
    tmp %= MOD
    tmp *= pow(fact[P-1], i, MOD)
    tmp %= MOD
    #j = 0
    #while j<i:
    #    tmp *= nCr(N-P*j, P, MOD)
    #    tmp %= MOD
    #    tmp *= fact[P-1]
    #    tmp %= MOD
    #    j+=1
    tmp *= fact_inv[i]
    tmp %= MOD
    ng += tmp
    ng %= MOD

ans = (all-ng)%MOD
print(ans)
0