結果

問題 No.434 占い
ユーザー H3PO4H3PO4
提出日時 2023-07-15 10:46:18
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 617 ms / 2,000 ms
コード長 856 bytes
コンパイル時間 559 ms
コンパイル使用メモリ 10,744 KB
実行使用メモリ 9,348 KB
最終ジャッジ日時 2023-10-15 01:32:08
合計ジャッジ時間 4,864 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
7,780 KB
testcase_01 AC 15 ms
7,880 KB
testcase_02 AC 15 ms
7,796 KB
testcase_03 AC 15 ms
7,852 KB
testcase_04 AC 15 ms
7,884 KB
testcase_05 AC 15 ms
7,840 KB
testcase_06 AC 20 ms
7,880 KB
testcase_07 AC 16 ms
7,828 KB
testcase_08 AC 28 ms
8,428 KB
testcase_09 AC 27 ms
8,356 KB
testcase_10 AC 28 ms
7,976 KB
testcase_11 AC 32 ms
7,764 KB
testcase_12 AC 76 ms
8,356 KB
testcase_13 AC 28 ms
8,376 KB
testcase_14 AC 27 ms
8,236 KB
testcase_15 AC 146 ms
9,292 KB
testcase_16 AC 145 ms
8,376 KB
testcase_17 AC 142 ms
8,232 KB
testcase_18 AC 145 ms
7,900 KB
testcase_19 AC 191 ms
7,912 KB
testcase_20 AC 617 ms
8,224 KB
testcase_21 AC 146 ms
9,304 KB
testcase_22 AC 147 ms
8,296 KB
testcase_23 AC 15 ms
7,796 KB
testcase_24 AC 132 ms
8,484 KB
testcase_25 AC 16 ms
8,344 KB
testcase_26 AC 78 ms
8,664 KB
testcase_27 AC 145 ms
8,276 KB
testcase_28 AC 148 ms
8,280 KB
testcase_29 AC 216 ms
9,348 KB
testcase_30 AC 523 ms
8,600 KB
権限があれば一括ダウンロードができます

ソースコード

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