結果

問題 No.434 占い
ユーザー H3PO4
提出日時 2023-07-15 10:46:18
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 683 ms / 2,000 ms
コード長 856 bytes
コンパイル時間 267 ms
コンパイル使用メモリ 12,544 KB
実行使用メモリ 12,032 KB
最終ジャッジ日時 2024-09-16 19:03:18
合計ジャッジ時間 5,942 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 27
権限があれば一括ダウンロードができます

ソースコード

diff #

def mod9(x):
    assert x > 0
    ct = 0
    while True:
        div, mod = divmod(x, 3)
        if mod != 0:
            return ct, x % 9
        x = div
        ct += 1

def solve(S: str):
    if S.count("0") == len(S):
        return 0
    T = tuple(int(s) for s in S)
    L = len(T)
    ans = T[0]
    c_pow3 = 0
    c_mod3 = 1
    c_mod9 = 1
    for i in range(1, L):
        # *= (L-i)
        ct, mod = mod9(L - i)
        c_pow3 += ct
        c_mod9 *= mod
        c_mod9 %= 9

        # /= i
        ct, mod = mod9(i)
        c_pow3 -= ct
        c_mod9 *= pow(mod, -1, 9)
        c_mod9 %= 9

        assert c_pow3 >= 0

        if c_pow3 == 0:
            ans += T[i] * c_mod9
        elif c_pow3 == 1:
            ans += T[i] * c_mod9 * 3
    ans = (ans - 1) % 9 + 1
    return ans

T = int(input())
for _ in range(T):
    print(solve(input()))
0