結果

問題 No.1403 調和の魔法陣
ユーザー 37zigen37zigen
提出日時 2020-08-21 06:19:51
言語 PyPy3
(7.3.13)
結果
AC  
実行時間 1,141 ms / 3,153 ms
コード長 2,848 bytes
コンパイル時間 391 ms
コンパイル使用メモリ 87,120 KB
実行使用メモリ 89,740 KB
最終ジャッジ日時 2023-10-12 22:35:15
合計ジャッジ時間 25,913 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 965 ms
85,920 KB
testcase_01 AC 1,005 ms
85,612 KB
testcase_02 AC 937 ms
85,808 KB
testcase_03 AC 1,018 ms
86,892 KB
testcase_04 AC 1,101 ms
89,740 KB
testcase_05 AC 1,073 ms
89,472 KB
testcase_06 AC 996 ms
86,800 KB
testcase_07 AC 1,088 ms
86,876 KB
testcase_08 AC 1,103 ms
86,928 KB
testcase_09 AC 1,124 ms
86,804 KB
testcase_10 AC 1,092 ms
86,572 KB
testcase_11 AC 1,122 ms
86,960 KB
testcase_12 AC 1,031 ms
86,968 KB
testcase_13 AC 1,038 ms
86,796 KB
testcase_14 AC 1,043 ms
86,964 KB
testcase_15 AC 1,114 ms
87,024 KB
testcase_16 AC 1,072 ms
86,772 KB
testcase_17 AC 1,141 ms
86,672 KB
testcase_18 AC 1,118 ms
87,008 KB
testcase_19 AC 1,129 ms
86,768 KB
testcase_20 AC 1,107 ms
86,696 KB
testcase_21 AC 1,071 ms
86,872 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

MOD=998244353
N=9
pows=[]

inv=[1,1]
for i in range(2,1000):
    inv.append(MOD-(MOD//i)*inv[MOD%i]%MOD)


xmax=81
dp=[[[0]*(xmax+1) for i in range(101)] for _ in range(101)]
dp2=[[] for _ in range(xmax+1)]

for X in range(0,xmax+1):
    for min_e in range(min(X + 1, N + 1)):
        for max_e in range(min_e, min(X + 1, N + 1)):
            for min_o in range(min(X + 1, N + 1)):
                for max_o in range(min_o, min(X+1, N + 1)):
                    for a00 in range(min(X + 1, N + 1)):
                        upper_o = min([N - a00 + min_e, X - a00 - max_o, N])
                        lower_o = max([-a00 + max_e, X - a00 - N - min_o, 0])
                        upper_e = min([N + a00 - max_e, a00 + min_o, N])
                        lower_e = max([a00 - min_e, -N + a00 + max_o, 0])
                        if upper_o - lower_o < 0 or upper_e - lower_e < 0:
                            continue
                        for pair0 in [[inv[max_e-min_e+1],max_e-min_e+1],[-2*inv[max_e-min_e],max_e-min_e],[inv[max(0,max_e-min_e-1)],max(0,max_e-min_e-1)]]:
                            for pair1 in [[1,max_o-min_o+1],[-2,max_o-min_o],[1,max(0,max_o-min_o-1)]]:
                                dp[(upper_o-lower_o+1)*(upper_e-lower_e+1)][pair0[1]*pair1[1]][X]+=inv[upper_e-lower_e+1]*pair0[0]%MOD*pair1[0]%MOD
                                dp[(upper_o-lower_o+1)*(upper_e-lower_e+1)][pair0[1]*pair1[1]][X]%=MOD
for X in range(xmax+1):
    for a in range(101):
        for b in range(101):
            if dp[a][b][X]!=0:
                dp2[X].append((a,b,dp[a][b][X]))

for n in range(101):
    a=[1]
    for i in range(301):
        a.append(a[-1]*n%MOD)
    pows.append(a)

def solvesub(W,H,X):
    ans=0
    if H==2:
        cnt=[0]*(2*N+1)
        for i in range(0,N+1):
            for j in range(0,N+1):
                cnt[i+j]+=1
        for a in range(2*N+1):
            b=X-a
            if b<0 or b>2*N:
                continue
            ans+=pow(cnt[a],W//2)*pow(cnt[b],(W+1)//2)
            ans%=MOD
        print(ans)
        return
    elif W==2:
        return solvesub(H,W,X)
    for v in dp2[X]:
        ans+=v[2]*pows[v[0]][W//2]%MOD*pows[v[1]][H//2]%MOD
        if ans>=MOD:
            ans-=MOD
    print(ans)

def solve(W,H,X):
    if W%3>H%3:
        W,H=H,W
    if (W%3==0 and H%3==0) or (W%3==0 and H%3==1) or (W%3==1 and H%3==1):
        print(int(X<=N))
    elif W%3==0 and H%3==2:
        d=min(N,X)-max(0,X-N)+1
        d=max(0,d)
        print(pow(d,W//3,MOD))
    elif W%3==1 and H%3==2:
        d=min(N,X)-max(0,X-N)+1
        d=max(0,d)
        print(pow(d,(W+2)//3,MOD))
    elif W%3==2 and H%3==2:
        solvesub((W+1)//3*2,(H+1)//3*2,X)
    else:
        raise Exception

def run():
    T=int(input())
    for t in range(T):
        W,H,X=map(int,input().split())
        solve(W,H,X)
run()
0