結果
問題 | No.434 占い |
ユーザー |
![]() |
提出日時 | 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 |
ソースコード
import sysinput = sys.stdin.readlineclass SegmentTree:def __init__(self, size, f=lambda x, y : x * y % 9, default=1):self.size = 2**(size-1).bit_length()self.default = defaultself.dat = [default]*(self.size*2)self.f = fdef update(self, i, x):i += self.sizeself.dat[i] = xwhile i > 0:i >>= 1self.dat[i] = self.f(self.dat[i*2], self.dat[i*2+1])def query(self, l, r):l += self.sizer += self.sizelres, rres = self.default, self.defaultwhile l < r:if l & 1:lres = self.f(lres, self.dat[l])l += 1if r & 1:r -= 1rres = self.f(self.dat[r], rres)l >>= 1r >>= 1res = self.f(lres, rres)return resdef factorization(n):arr = []temp = nfor i in range(2, int(-(-n**0.5//1))+1):if temp%i==0:cnt=0while temp%i==0:cnt+=1temp //= iarr.append([i, cnt])if temp!=1:arr.append([temp, 1])return arrQ = 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)continueN = len(S) - 1L = [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 %= 9print(ans) if ans else print(9)