結果

問題 No.1595 The Final Digit
ユーザー lavoxlavox
提出日時 2021-07-09 21:53:30
言語 Java21
(openjdk 21)
結果
AC  
実行時間 126 ms / 2,000 ms
コード長 862 bytes
コンパイル時間 2,097 ms
コンパイル使用メモリ 73,756 KB
実行使用メモリ 57,580 KB
最終ジャッジ日時 2023-09-14 08:50:42
合計ジャッジ時間 5,640 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 126 ms
55,832 KB
testcase_01 AC 121 ms
55,536 KB
testcase_02 AC 125 ms
53,220 KB
testcase_03 AC 122 ms
55,380 KB
testcase_04 AC 122 ms
55,608 KB
testcase_05 AC 121 ms
53,528 KB
testcase_06 AC 121 ms
56,012 KB
testcase_07 AC 122 ms
55,528 KB
testcase_08 AC 122 ms
55,780 KB
testcase_09 AC 122 ms
55,724 KB
testcase_10 AC 119 ms
55,604 KB
testcase_11 AC 120 ms
57,580 KB
testcase_12 AC 120 ms
55,608 KB
testcase_13 AC 122 ms
55,700 KB
testcase_14 AC 122 ms
55,576 KB
testcase_15 AC 124 ms
55,492 KB
testcase_16 AC 123 ms
55,508 KB
testcase_17 AC 123 ms
55,256 KB
testcase_18 AC 124 ms
55,568 KB
testcase_19 AC 121 ms
55,256 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int p = Integer.parseInt(sc.next()) % 10;
		int q = Integer.parseInt(sc.next()) % 10;
		int r = Integer.parseInt(sc.next()) % 10;
		long K = Long.parseLong(sc.next());
		sc.close();
		
		long[] cnt = new long[1000];
		long i = 3;
		long ans = 0;
		while ( true ) {
			if ( i == K ) {
				ans = r;
				break;
			}
			if ( cnt[100 * p + 10 * q + r] > 0 ) {
				long per = i - cnt[100 * p + 10 * q + r];
				long x = (K - i) / per;
				i += x * per;
				i++;
				for ( ; i <= K ; i++ ) {
					int n = (p + q + r) % 10;
					p = q;
					q = r;
					r = n;
				}
				ans = r;
				break;
			} else {
				cnt[100 * p + 10 * q + r] = i++;
			}
			int n = (p + q + r) % 10;
			p = q;
			q = r;
			r = n;
		}

		System.out.println(ans);
	}
}
0