結果

問題 No.2522 Fall in love, Girls!
ユーザー titiatitia
提出日時 2023-11-08 01:49:39
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 2,102 bytes
コンパイル時間 300 ms
コンパイル使用メモリ 81,872 KB
実行使用メモリ 247,824 KB
最終ジャッジ日時 2023-11-08 01:49:46
合計ジャッジ時間 6,363 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 124 ms
219,548 KB
testcase_01 AC 132 ms
215,200 KB
testcase_02 AC 128 ms
215,200 KB
testcase_03 AC 912 ms
247,824 KB
testcase_04 AC 130 ms
215,200 KB
testcase_05 AC 125 ms
215,200 KB
testcase_06 TLE -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

N,M,K=map(int,input().split())

mod=998244353

FACT=[1]
for i in range(1,2*10**6):
    FACT.append(FACT[-1]*i%mod)

E=[list(map(int,input().split())) for i in range(K)]

DEG=[-1]*(N+1)
EDGE=[[] for i in range(N+1)]

LIST=[]
for x,y in E:
    LIST.append(x)
    LIST.append(y)

    DEG[x]=max(0,DEG[x])
    DEG[y]=max(0,DEG[y])
    DEG[y]+=1
    EDGE[x].append(y)

LIST=sorted(set(LIST))
D={LIST[i]:i for i in range(len(LIST))}

ind_main=0
ind_sub=0

other_main=N-M
other_sub=M

for l in LIST:
    if l<=M:
        ind_sub+=1
        other_sub-=1
    else:
        ind_main+=1
        other_main-=1

Q=[]
for i in range(N+1):
    if DEG[i]==0:
        Q.append(i)

NEED=[0]*(len(LIST))

while Q:
    x=Q.pop()
    c=D[x]

    for to in EDGE[x]:
        DEG[to]-=1
        NEED[D[to]]|=((1<<c)|(NEED[D[x]]))

        if DEG[to]==0:
            Q.append(to)

if max(DEG)>0:
    print(0)
    exit()

DP=[0]*(1<<len(LIST))
DP[0]=1

DP2=[0]*(1<<len(LIST)) # startでindのものを使う。
DP2[0]=1
for i in range((1<<len(LIST))):
    for to in range(len(LIST)):
        if (1<<to) & i !=0:
            continue
        if (NEED[to] & i) == NEED[to]:
            DP[i|(1<<to)]+=DP[i]
            DP[i|(1<<to)]%=mod

            if i==0:
                if LIST[to]>M:
                    DP2[i|(1<<to)]+=DP2[i]
            else:
                DP2[i|(1<<to)]+=DP2[i]
            DP2[i|(1<<to)]%=mod

DP3=[0]*(other_main+other_sub+1)
DP3[0]=1

for i in range(len(LIST)):
    for j in range(other_main+other_sub,-1,-1):
        for k in range(j+1,other_main+other_sub+1):
            DP3[k]+=DP3[j]
            DP3[k]%=mod

ANS1=DP3[-1]*DP2[-1]*FACT[other_main+other_sub]%mod

if len(LIST)==0:
    ANS1=0
ANS2=0

if other_main>0:
    DP4=[0]*(other_main+other_sub)
    DP4[0]=other_main

    for i in range(len(LIST)+1):
        for j in range(other_main+other_sub-1,-1,-1):
            for k in range(j+1,other_main+other_sub):
                DP4[k]+=DP4[j]
                DP4[k]%=mod

    ANS2=DP4[-1]*DP[-1]*FACT[other_main+other_sub-1]%mod

print((ANS1+ANS2)%mod)
0