結果

問題 No.2727 Tetrahedron Game
ユーザー MasKoaTSMasKoaTS
提出日時 2023-09-28 16:34:03
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 728 ms / 2,000 ms
コード長 2,105 bytes
コンパイル時間 182 ms
コンパイル使用メモリ 81,748 KB
実行使用メモリ 180,760 KB
最終ジャッジ日時 2023-10-17 23:48:55
合計ジャッジ時間 7,151 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 65 ms
70,508 KB
testcase_01 AC 725 ms
81,804 KB
testcase_02 AC 678 ms
85,600 KB
testcase_03 AC 632 ms
180,704 KB
testcase_04 AC 661 ms
180,680 KB
testcase_05 AC 689 ms
175,924 KB
testcase_06 AC 728 ms
180,760 KB
testcase_07 AC 604 ms
164,132 KB
testcase_08 AC 663 ms
178,636 KB
testcase_09 AC 717 ms
177,000 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline
MOD = 101
INV2, INV3 = pow(2, MOD - 2, MOD), pow(3, MOD - 2, MOD)


def det(A):
    ret = 0
    B = A[:] + [A[0]] + [A[1]]
    for i in range(3):
        ret += B[i][0] * B[i+1][1] * B[i+2][2]
        ret -= B[i][2] * B[i+1][1] * B[i+2][0]
    return ret


def solve(N, K, P, a, s):
    d = abs(det(P))
    if(d == 0):
        return 'D'
    div2, div3 = (d % 2 == 0), (d % 3 == 0)
    dp = [[[-1] * 4 for _ in [0] * MOD] for _ in [0] * (N + 1)]
    for j in range(MOD):
        for k in range(3):
            if(div2 and not(k & 1)):
                continue
            if(div3 and not((k >> 1) & 1)):
                continue
            dp[N][j][k] = 2
        dp[N][j][3] = int(j >= K)
    for i in range(N - 1, -1, -1):
        player_is_koa = (s[i] == 'K')
        val = a[i] + 1
        val_div2, val_div3 = (val % 2 == 0), (val % 3 == 0)
        for j in range(MOD):
            for k in range(4):
                hands = set([dp[i + 1][j * val % MOD][k | val_div2 | (val_div3 << 1)], dp[i + 1][j][k]])
                if(player_is_koa):
                    if(1 in hands):
                        dp[i][j][k] = 1
                    elif(2 in hands):
                        dp[i][j][k] = 2
                    elif(0 in hands):
                        dp[i][j][k] = 0
                    else:
                        dp[i][j][k] = -1
                else:
                    if(0 in hands):
                        dp[i][j][k] = 0
                    elif(2 in hands):
                        dp[i][j][k] = 2
                    elif(1 in hands):
                        dp[i][j][k] = 1
                    else:
                        dp[i][j][k] = -1
    stat_init = dp[0][d % MOD * INV2 * INV3 % MOD][div2 | (div3 << 1)]
    if(stat_init == 1):
        return 'K'
    elif(stat_init == 0):
        return 'P'
    return 'D'


t = int(input())
for _ in [0] * t:
    N, K = map(int, input().split())
    P = [list(map(int, input().split())) for _ in [0] * 3]
    a = list(map(int, input().split()))
    s = input()[:-1]
    print(solve(N, K, P, a, s))
0