結果

問題 No.1595 The Final Digit
ユーザー ks2mks2m
提出日時 2021-07-09 21:42:55
言語 Java21
(openjdk 21)
結果
AC  
実行時間 131 ms / 2,000 ms
コード長 847 bytes
コンパイル時間 3,017 ms
コンパイル使用メモリ 74,500 KB
実行使用メモリ 56,320 KB
最終ジャッジ日時 2023-09-14 08:15:38
合計ジャッジ時間 6,661 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 126 ms
55,796 KB
testcase_01 AC 124 ms
53,636 KB
testcase_02 AC 124 ms
55,952 KB
testcase_03 AC 125 ms
56,060 KB
testcase_04 AC 125 ms
55,788 KB
testcase_05 AC 125 ms
55,788 KB
testcase_06 AC 124 ms
56,036 KB
testcase_07 AC 125 ms
56,024 KB
testcase_08 AC 124 ms
55,664 KB
testcase_09 AC 124 ms
55,488 KB
testcase_10 AC 125 ms
55,788 KB
testcase_11 AC 127 ms
55,788 KB
testcase_12 AC 129 ms
56,320 KB
testcase_13 AC 124 ms
55,640 KB
testcase_14 AC 125 ms
55,660 KB
testcase_15 AC 126 ms
55,684 KB
testcase_16 AC 131 ms
55,616 KB
testcase_17 AC 126 ms
56,032 KB
testcase_18 AC 127 ms
55,908 KB
testcase_19 AC 131 ms
55,512 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		int[] a = new int[1000];
		a[0] = sc.nextInt() % 10;
		a[1] = sc.nextInt() % 10;
		a[2] = sc.nextInt() % 10;
		long k = sc.nextLong() - 1;
		sc.close();

		Map<Integer, Integer> map = new HashMap<>();
		int s = 0;
		int d = 0;
		map.put(a[0] * 100 + a[1] * 10 + a[2], 2);
		for (int i = 3; i < a.length; i++) {
			a[i] = a[i - 3] + a[i - 2] + a[i - 1];
			a[i] %= 10;
			if (i == k) {
				System.out.println(a[i]);
				return;
			}
			int v = a[i - 2] * 100 + a[i - 1] * 10 + a[i];
			if (map.containsKey(v)) {
				s = map.get(v);
				d = i - s;
				break;
			}
			map.put(v, i);
		}
		int x = (int) ((k - s) % d);
		System.out.println(a[s + x]);
	}
}
0