結果

問題 No.1595 The Final Digit
ユーザー ks2m
提出日時 2021-07-09 21:42:55
言語 Java
(openjdk 23)
結果
AC  
実行時間 143 ms / 2,000 ms
コード長 847 bytes
コンパイル時間 2,335 ms
コンパイル使用メモリ 81,456 KB
実行使用メモリ 54,228 KB
最終ジャッジ日時 2024-07-01 15:40:33
合計ジャッジ時間 5,948 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 17
権限があれば一括ダウンロードができます

ソースコード

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