結果

問題 No.1097 Remainder Operation
ユーザー yuly3
提出日時 2020-07-26 18:47:59
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 168 ms / 2,000 ms
コード長 805 bytes
コンパイル時間 158 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 119,936 KB
最終ジャッジ日時 2024-06-28 19:16:28
合計ジャッジ時間 5,555 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 21
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

sys.setrecursionlimit(10 ** 7)
rl = sys.stdin.readline


def solve():
    N = int(rl())
    A = list(map(int, rl().split()))
    Q = int(rl())
    K = [int(rl()) for _ in range(Q)]
    
    X = 0
    counter = dict()
    cnt_to_x = {0: 0}
    cnt = 0
    while 1:
        cnt += 1
        r = X % N
        X += A[r]
        if r in counter:
            a = counter[r]
            b = cnt - counter[r]
            d = X - cnt_to_x[a]
            break
        else:
            counter[r] = cnt
            cnt_to_x[cnt] = X
    
    ans = []
    for ki in K:
        if ki <= a:
            ans.append(cnt_to_x[ki])
        else:
            roop, rem = divmod(ki - a, b)
            ans.append(roop * d + cnt_to_x[a + rem])
    print(*ans, sep='\n')


if __name__ == '__main__':
    solve()
0