結果

問題 No.1595 The Final Digit
ユーザー ripityripity
提出日時 2021-12-13 23:20:22
言語 Java21
(openjdk 21)
結果
AC  
実行時間 116 ms / 2,000 ms
コード長 1,359 bytes
コンパイル時間 1,822 ms
コンパイル使用メモリ 74,780 KB
実行使用メモリ 56,688 KB
最終ジャッジ日時 2023-09-30 05:04:24
合計ジャッジ時間 5,110 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 107 ms
55,704 KB
testcase_01 AC 111 ms
55,708 KB
testcase_02 AC 106 ms
56,016 KB
testcase_03 AC 114 ms
55,824 KB
testcase_04 AC 106 ms
55,656 KB
testcase_05 AC 106 ms
55,616 KB
testcase_06 AC 107 ms
55,956 KB
testcase_07 AC 113 ms
56,280 KB
testcase_08 AC 108 ms
55,808 KB
testcase_09 AC 111 ms
55,688 KB
testcase_10 AC 107 ms
55,508 KB
testcase_11 AC 107 ms
55,476 KB
testcase_12 AC 108 ms
55,872 KB
testcase_13 AC 116 ms
56,688 KB
testcase_14 AC 111 ms
55,752 KB
testcase_15 AC 113 ms
55,944 KB
testcase_16 AC 109 ms
56,088 KB
testcase_17 AC 108 ms
55,608 KB
testcase_18 AC 107 ms
56,004 KB
testcase_19 AC 106 ms
55,816 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    
    public static void main(String[] args) {
        
        Scanner sc = new Scanner(System.in);
        int p = sc.nextInt()%10;
        int q = sc.nextInt()%10;
        int r = sc.nextInt()%10;
        long K = sc.nextLong()-1;
        int now = 0;
        int loop = 0;
        ArrayList<Integer> arr = new ArrayList<>();
        arr.add(p);
        arr.add(q);
        arr.add(r);
        for(;;) {
            now = (p+q+r)%10;
            arr.add(now);
            p = q;
            q = r;
            r = now;
            if( check(arr,p,q,r) != -1 ) {
                loop = check(arr,p,q,r);
                break;
            }
        }
        
        int s = arr.size();
        if( K < loop ) {
            System.out.println(arr.get((int)K));
        }else {
            K -= loop-1;
            K = K%(s-loop);
            System.out.println(arr.get((int)K+loop-1));
        }
        
    }
    
    static int check( ArrayList<Integer> arr, int p, int q, int r ) {
        
        int N = arr.size()-3;
        for( int i = 0; i < N; i++ ) {
            int x = arr.get(i);
            int y = arr.get(i+1);
            int z = arr.get(i+2);
            if( x == p && y == q && z == r ) {
                return i+3;
            }
        }
        
        return -1;
        
    }
    
}
0