結果

問題 No.1097 Remainder Operation
コンテスト
ユーザー ks2m
提出日時 2020-06-26 22:06:25
言語 Java
(openjdk 25.0.2)
コンパイル:
javac -encoding UTF8 _filename_
実行:
java -ea -Xmx700m -Xss256M -DONLINE_JUDGE=true _class_
結果
AC  
実行時間 494 ms / 2,000 ms
コード長 1,227 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,640 ms
コンパイル使用メモリ 81,744 KB
実行使用メモリ 111,320 KB
最終ジャッジ日時 2026-03-26 03:49:58
合計ジャッジ時間 9,054 ms
ジャッジサーバーID
(参考情報)
judge1_1 / judge3_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 21
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;

public class Main {
	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(br.readLine());
		String[] sa = br.readLine().split(" ");
		int[] a = new int[n];
		for (int i = 0; i < n; i++) {
			a[i] = Integer.parseInt(sa[i]);
		}
		int q = Integer.parseInt(br.readLine());
		long[] k = new long[q];
		for (int i = 0; i < q; i++) {
			k[i] = Long.parseLong(br.readLine());
		}
		br.close();

		int m = 43;
		int[][] next = new int[m][n];
		long[][] val = new long[m][n];
		for (int i = 0; i < n; i++) {
			next[0][i] = (i + a[i]) % n;
			val[0][i] = a[i];
		}

		for (int i = 1; i < m; i++) {
			for (int j = 0; j < n; j++) {
				next[i][j] = next[i - 1][next[i - 1][j]];
				val[i][j] = val[i - 1][j] + val[i - 1][next[i - 1][j]];
			}
		}

		PrintWriter pw = new PrintWriter(System.out);
		for (int i = 0; i < q; i++) {
			int now = 0;
			long ans = 0;
			for (int j = m - 1; j >= 0; j--) {
				if ((k[i] >> j & 1) == 1) {
					ans += val[j][now];
					now = next[j][now];
				}
			}
			pw.println(ans);
		}
		pw.flush();
	}
}
0