結果

問題 No.2381 Gift Exchange Party
ユーザー flygon
提出日時 2023-07-14 23:01:53
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 307 ms / 2,000 ms
コード長 1,086 bytes
コンパイル時間 721 ms
コンパイル使用メモリ 82,432 KB
実行使用メモリ 250,112 KB
最終ジャッジ日時 2024-09-16 08:09:56
合計ジャッジ時間 6,332 ms
ジャッジサーバーID
(参考情報)
judge3 / judge6
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 22
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
sys.setrecursionlimit(5*10**5)
input = sys.stdin.readline
from collections import defaultdict, deque, Counter
from heapq import heappop, heappush
from bisect import bisect_left, bisect_right
from math import gcd

n,p = map(int,input().split())
all = 1
mod = 998244353
for i in range(1,n+1):
    all *= i
    all %= mod
if p > n:
    print(all-1)
    exit()

e = 1
for i in range(1, p):
    e *= i
    e %= mod

# nCk
def com(n, mod):
    fact = [1, 1]
    factinv = [1, 1]
    inv = [0, 1]
    for i in range(2, n+1):
        fact.append((fact[-1]*i) % mod)
        inv.append((-inv[mod % i]*(mod//i)) % mod)
        factinv.append((factinv[-1]*inv[-1]) % mod)
    return fact, factinv

f,fi = com(10**6, mod)

def npr(n, r):
    if n < r:return 1
    return f[n] * fi[n-r] % mod
def ncr(n, r):
    if n < r:return 1
    return f[n] * fi[n-r] % mod * fi[r] % mod

# 長さ1とpのループしかないとき

mx = n//p
now = 1
all -= 1

for i in range(1,mx+1):
    now *= ncr(p*i,p)
    now *= e
    now %= mod
    all -= ncr(n,p*i) * now * fi[i]
    all %= mod

print(all)

0