結果

問題 No.1392 Don't be together
ユーザー vwxyzvwxyz
提出日時 2023-04-29 03:01:59
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 845 ms / 2,000 ms
コード長 2,976 bytes
コンパイル時間 1,674 ms
コンパイル使用メモリ 86,444 KB
実行使用メモリ 77,528 KB
最終ジャッジ日時 2023-08-11 10:18:52
合計ジャッジ時間 9,251 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 71 ms
71,348 KB
testcase_01 AC 69 ms
71,136 KB
testcase_02 AC 67 ms
71,028 KB
testcase_03 AC 69 ms
71,052 KB
testcase_04 AC 69 ms
71,320 KB
testcase_05 AC 66 ms
71,036 KB
testcase_06 AC 316 ms
77,224 KB
testcase_07 AC 196 ms
76,716 KB
testcase_08 AC 370 ms
77,528 KB
testcase_09 AC 845 ms
77,440 KB
testcase_10 AC 133 ms
77,048 KB
testcase_11 AC 198 ms
77,344 KB
testcase_12 AC 129 ms
77,232 KB
testcase_13 AC 163 ms
77,268 KB
testcase_14 AC 194 ms
77,352 KB
testcase_15 AC 135 ms
77,276 KB
testcase_16 AC 193 ms
76,996 KB
testcase_17 AC 135 ms
77,320 KB
testcase_18 AC 176 ms
77,160 KB
testcase_19 AC 157 ms
77,140 KB
testcase_20 AC 131 ms
77,492 KB
testcase_21 AC 106 ms
77,132 KB
testcase_22 AC 144 ms
77,156 KB
testcase_23 AC 125 ms
77,044 KB
testcase_24 AC 101 ms
77,308 KB
testcase_25 AC 138 ms
77,348 KB
testcase_26 AC 101 ms
77,328 KB
testcase_27 AC 99 ms
77,112 KB
testcase_28 AC 145 ms
77,240 KB
testcase_29 AC 119 ms
77,328 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
readline=sys.stdin.readline
import math

def Extended_Euclid(n,m):
    stack=[]
    while m:
        stack.append((n,m))
        n,m=m,n%m
    if n>=0:
        x,y=1,0
    else:
        x,y=-1,0
    for i in range(len(stack)-1,-1,-1):
        n,m=stack[i]
        x,y=y,x-(n//m)*y
    return x,y

class MOD:
    def __init__(self,p,e=None):
        self.p=p
        self.e=e
        if self.e==None:
            self.mod=self.p
        else:
            self.mod=self.p**self.e

    def Pow(self,a,n):
        a%=self.mod
        if n>=0:
            return pow(a,n,self.mod)
        else:
            assert math.gcd(a,self.mod)==1
            x=Extended_Euclid(a,self.mod)[0]
            return pow(x,-n,self.mod)

    def Build_Fact(self,N):
        assert N>=0
        self.factorial=[1]
        if self.e==None:
            for i in range(1,N+1):
                self.factorial.append(self.factorial[-1]*i%self.mod)
        else:
            self.cnt=[0]*(N+1)
            for i in range(1,N+1):
                self.cnt[i]=self.cnt[i-1]
                ii=i
                while ii%self.p==0:
                    ii//=self.p
                    self.cnt[i]+=1
                self.factorial.append(self.factorial[-1]*ii%self.mod)
        self.factorial_inve=[None]*(N+1)
        self.factorial_inve[-1]=self.Pow(self.factorial[-1],-1)
        for i in range(N-1,-1,-1):
            ii=i+1
            while ii%self.p==0:
                ii//=self.p
            self.factorial_inve[i]=(self.factorial_inve[i+1]*ii)%self.mod

    def Fact(self,N):
        if N<0:
            return 0
        retu=self.factorial[N]
        if self.e!=None and self.cnt[N]:
            retu*=pow(self.p,self.cnt[N],self.mod)%self.mod
            retu%=self.mod
        return retu

    def Fact_Inve(self,N):
        if self.e!=None and self.cnt[N]:
            return None
        return self.factorial_inve[N]

    def Comb(self,N,K,divisible_count=False):
        if K<0 or K>N:
            return 0
        retu=self.factorial[N]*self.factorial_inve[K]%self.mod*self.factorial_inve[N-K]%self.mod
        if self.e!=None:
            cnt=self.cnt[N]-self.cnt[N-K]-self.cnt[K]
            if divisible_count:
                return retu,cnt
            else:
                retu*=pow(self.p,cnt,self.mod)
                retu%=self.mod
        return retu

N,M=map(int,readline().split())

P=list(map(int,readline().split()))
for i in range(N):
    P[i]-=1
seen=[False]*N
C=[]
for p in range(N):
    if seen[p]:
        continue
    C.append(0)
    while not seen[p]:
        C[-1]+=1
        seen[p]=True
        p=P[p]
dp=[0]*(M+1)
mod=998244353
MD=MOD(mod)
MD.Build_Fact(M)
for m in range(1,M+1):
    dp[m]=1
    for c in C:
        if c==1:
            dp[m]*=m
        else:
            dp[m]*=(pow(m-1,c,mod)+(-1)**c*(m-1))
        dp[m]%=mod
    for mm in range(1,m):
        dp[m]-=MD.Comb(m,mm)*dp[mm]%mod
        dp[m]%=mod
ans=dp[M]*MD.Fact_Inve(M)%mod
print(ans)
0