結果

問題 No.434 占い
ユーザー tktk_snsntktk_snsn
提出日時 2021-09-04 13:15:53
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 375 ms / 2,000 ms
コード長 1,441 bytes
コンパイル時間 97 ms
コンパイル使用メモリ 11,140 KB
実行使用メモリ 14,384 KB
最終ジャッジ日時 2023-08-22 20:35:07
合計ジャッジ時間 6,716 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 104 ms
13,948 KB
testcase_01 AC 103 ms
13,956 KB
testcase_02 AC 102 ms
13,912 KB
testcase_03 AC 105 ms
14,104 KB
testcase_04 AC 105 ms
13,956 KB
testcase_05 AC 106 ms
14,144 KB
testcase_06 AC 108 ms
14,208 KB
testcase_07 AC 107 ms
14,136 KB
testcase_08 AC 113 ms
13,880 KB
testcase_09 AC 113 ms
14,100 KB
testcase_10 AC 110 ms
14,108 KB
testcase_11 AC 115 ms
14,056 KB
testcase_12 AC 130 ms
14,156 KB
testcase_13 AC 111 ms
13,980 KB
testcase_14 AC 112 ms
13,956 KB
testcase_15 AC 170 ms
14,080 KB
testcase_16 AC 171 ms
13,892 KB
testcase_17 AC 167 ms
13,956 KB
testcase_18 AC 171 ms
14,004 KB
testcase_19 AC 199 ms
14,352 KB
testcase_20 AC 375 ms
14,204 KB
testcase_21 AC 169 ms
14,044 KB
testcase_22 AC 171 ms
13,932 KB
testcase_23 AC 105 ms
13,964 KB
testcase_24 AC 165 ms
13,972 KB
testcase_25 AC 109 ms
14,028 KB
testcase_26 AC 142 ms
14,020 KB
testcase_27 AC 178 ms
14,088 KB
testcase_28 AC 176 ms
14,020 KB
testcase_29 AC 210 ms
14,308 KB
testcase_30 AC 344 ms
14,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

"""\
参考
https://yukicoder.me/submissions/438049
"""
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)


def euler_phi(N):
    """
    Euler totient function
    [1,N]の自然数のうちNと互いに素なものの個数
    """
    res = N
    for i in range(2, int(N ** 0.5) + 1):
        if N % i == 0:
            res -= res // i
            while N % i == 0:
                N //= i
    if N > 1:
        res -= res // N
    return res


def euler_phi_table(N):
    res = list(range(N))
    for i in range(2, N):
        if res[i] == i:
            for j in range(i, N, i):
                res[j] = res[j] // i * (i - 1)
    return res


U = 10 ** 5

fact = [1] * (U + 1)
fact_ord = [0] * (U + 1)
for n in range(1, U+1):
    e = 0
    m = n
    while m % 3 == 0:
        m //= 3
        e += 1
    fact[n] = fact[n-1] * m % 9
    fact_ord[n] = fact_ord[n-1] + e

phi = euler_phi(9)
inv = [pow(x, phi - 1, 9) for x in fact]


def comb(n, k):
    e = fact_ord[n] - fact_ord[k] - fact_ord[n-k]
    if e >= 2:
        return 0
    res = fact[n] * inv[k] * inv[n-k]
    if e == 1:
        return res * 3 % 9
    return res % 9


T = int(input())
for _ in range(T):
    S = input().rstrip()
    if all(s == "0" for s in S):
        print(0)
        continue
    res = 0
    n = len(S) - 1
    for i, s in enumerate(S):
        res += int(s) * comb(n, i)
        res %= 9
    if res == 0:
        res = 9
    print(res)
0