結果

問題 No.1595 The Final Digit
ユーザー ks2mks2m
提出日時 2021-07-09 21:42:55
言語 Java17
(openjdk 17.0.1)
結果
AC  
実行時間 100 ms / 2,000 ms
コード長 847 bytes
コンパイル時間 2,121 ms
使用メモリ 37,940 KB
最終ジャッジ日時 2023-02-01 22:41:37
合計ジャッジ時間 5,266 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
testcase_00 AC 93 ms
37,720 KB
testcase_01 AC 93 ms
37,668 KB
testcase_02 AC 92 ms
37,736 KB
testcase_03 AC 91 ms
37,828 KB
testcase_04 AC 97 ms
37,656 KB
testcase_05 AC 92 ms
37,684 KB
testcase_06 AC 95 ms
37,940 KB
testcase_07 AC 93 ms
37,768 KB
testcase_08 AC 91 ms
37,696 KB
testcase_09 AC 93 ms
37,708 KB
testcase_10 AC 93 ms
37,608 KB
testcase_11 AC 95 ms
37,764 KB
testcase_12 AC 100 ms
37,696 KB
testcase_13 AC 97 ms
37,860 KB
testcase_14 AC 92 ms
37,684 KB
testcase_15 AC 95 ms
37,740 KB
testcase_16 AC 92 ms
37,632 KB
testcase_17 AC 97 ms
37,852 KB
testcase_18 AC 93 ms
37,648 KB
testcase_19 AC 91 ms
37,836 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