結果

問題 No.434 占い
ユーザー rlangevin
提出日時 2023-10-17 22:23:48
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 791 ms / 2,000 ms
コード長 1,831 bytes
コンパイル時間 644 ms
コンパイル使用メモリ 82,124 KB
実行使用メモリ 87,940 KB
最終ジャッジ日時 2024-09-17 19:37:49
合計ジャッジ時間 8,612 ms
ジャッジサーバーID
(参考情報)
judge4 / judge6
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 27
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

class SegmentTree:
    def __init__(self, size, f=lambda x, y : x * y % 9, default=1):
        self.size = 2**(size-1).bit_length() 
        self.default = default
        self.dat = [default]*(self.size*2) 
        self.f = f

    def update(self, i, x):
        i += self.size
        self.dat[i] = x
        while i > 0:
            i >>= 1
            self.dat[i] = self.f(self.dat[i*2], self.dat[i*2+1])

    def query(self, l, r):
        l += self.size
        r += self.size
        lres, rres = self.default, self.default
        while l < r:
            if l & 1:
                lres = self.f(lres, self.dat[l])
                l += 1

            if r & 1:
                r -= 1
                rres = self.f(self.dat[r], rres) 
            l >>= 1
            r >>= 1
        res = self.f(lres, rres)
        return res

def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])

    if temp!=1:
        arr.append([temp, 1])

    return arr

Q = int(input().rstrip())
T = SegmentTree(10**5+5)
for _ in range(Q):
    S = list(input().rstrip())
    S = list(map(int, S))
    if len(set(S)) == 1 and S[0] == 0:
        print(0)
        continue
    
    N = len(S) - 1
    L = [0] * (N + 1)
    ans = S[0]
    for i in range(1, N + 1):
        SS = set()
        for t in factorization(N - i + 1):
            SS.add(t[0])
            L[t[0]] += t[1]
        for t in factorization(i):
            SS.add(t[0])
            L[t[0]] -= t[1]
        for s in SS:
            T.update(s, pow(s, L[s], 9))
        ans += T.query(0, N + 1) * S[i]
        ans %= 9
    print(ans) if ans else print(9)
0