結果

問題 No.1097 Remainder Operation
ユーザー H3PO4H3PO4
提出日時 2020-06-26 22:18:34
言語 Python3
(3.11.6 + numpy 1.26.0 + scipy 1.11.3)
結果
AC  
実行時間 752 ms / 2,000 ms
コード長 741 bytes
コンパイル時間 477 ms
コンパイル使用メモリ 10,772 KB
実行使用メモリ 29,708 KB
最終ジャッジ日時 2023-09-18 05:32:42
合計ジャッジ時間 10,567 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
7,896 KB
testcase_01 AC 15 ms
7,892 KB
testcase_02 AC 16 ms
7,896 KB
testcase_03 AC 16 ms
7,936 KB
testcase_04 AC 17 ms
7,840 KB
testcase_05 AC 17 ms
7,776 KB
testcase_06 AC 17 ms
7,760 KB
testcase_07 AC 79 ms
9,204 KB
testcase_08 AC 78 ms
9,080 KB
testcase_09 AC 78 ms
9,200 KB
testcase_10 AC 79 ms
9,104 KB
testcase_11 AC 78 ms
9,076 KB
testcase_12 AC 666 ms
19,644 KB
testcase_13 AC 670 ms
19,812 KB
testcase_14 AC 674 ms
19,800 KB
testcase_15 AC 667 ms
19,656 KB
testcase_16 AC 663 ms
19,656 KB
testcase_17 AC 658 ms
19,800 KB
testcase_18 AC 750 ms
29,708 KB
testcase_19 AC 729 ms
27,316 KB
testcase_20 AC 727 ms
27,244 KB
testcase_21 AC 752 ms
27,676 KB
testcase_22 AC 746 ms
27,708 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

N = int(input())
A = list(map(int, input().split()))
M = [(i + a) % N for i, a in enumerate(A)]

cur = 0
t = 0
visited = dict()
Acc_tail = [0]
while cur not in visited:
    visited[cur] = t
    Acc_tail.append(Acc_tail[-1] + A[cur])
    cur = M[cur]
    t += 1

Acc_tail.append(Acc_tail[-1] + A[cur])

tail = visited[cur] + 1
loop_length = t - tail + 1
Acc_loop = [x - Acc_tail[tail] for x in Acc_tail[tail + 1:]]


def solve(K):
    if K < len(Acc_tail):
        return Acc_tail[K]
    else:
        K -= tail
        ret = Acc_tail[tail] + Acc_loop[-1] * (K // loop_length)
        if K % loop_length:
            ret += Acc_loop[K % loop_length - 1]
        return ret


Q = int(input())
for _ in range(Q):
    print(solve(int(input())))
0