結果

問題 No.2983 Christmas Color Grid (Advent Calender ver.)
ユーザー 👑 p-adic
提出日時 2024-11-21 15:41:05
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 907 ms / 3,340 ms
コード長 771 bytes
コンパイル時間 273 ms
コンパイル使用メモリ 82,156 KB
実行使用メモリ 84,800 KB
最終ジャッジ日時 2024-11-21 15:41:16
合計ジャッジ時間 9,257 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 64
権限があれば一括ダウンロードができます

ソースコード

diff #

R=range
H,W,K,M=map(int,input().split())
HW=H*W
p=1<<HW
found=[0]*((p+31)>>5)
count=[0]*(HW+1)
dfs=[]
for h in R(H):
	for w in R(W):
		i=h*W+w
		s=1<<i
		found[s>>5]|=1<<(s&31)
		bs=0
		for dx,dy in zip([-1,0,1,0],[0,-1,0,1]):
			if 0<=h+dx<H and 0<=w+dy<W:
				j=i+dx*W+dy
				bs|=(1<<j)
		dfs+=[[s,bs]]
while dfs:
	s,bs=dfs.pop()
	c=s.bit_count()
	count[c]+=1<<(HW-c-bs.bit_count())
	for i in R(HW):
		if(bs>>i)&1<1:continue
		h,w=i//W,i%W
		t=s|(1<<i)
		if(found[t>>5]>>(t&31))&1:continue;
		found[t>>5]|=1<<(t&31)
		bt=bs^(1<<i)
		for dx,dy in zip([-1,0,1,0],[0,-1,0,1]):
			if 0<=h+dx<H and 0<=w+dy<W:
				j=i+dx*W+dy
				if(s>>j)&1<1:bt|=(1<<j)
		dfs+=[[t,bt]]
print(sum(pow(c,K,M)*count[c]for c in R(1,HW+1))*sum(pow(HW-n+1,-1,M)for n in R(1,HW+1))*pow(2,-HW,M)%M)
0