結果

問題 No.1595 The Final Digit
ユーザー tenten
提出日時 2021-07-12 12:20:56
言語 Java
(openjdk 23)
結果
AC  
実行時間 52 ms / 2,000 ms
コード長 1,844 bytes
コンパイル時間 2,384 ms
コンパイル使用メモリ 77,112 KB
実行使用メモリ 37,084 KB
最終ジャッジ日時 2024-07-02 03:25:12
合計ジャッジ時間 3,893 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 17
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int current = sc.nextInt() % 10 * 100 + sc.nextInt() % 10 * 10 + sc.nextInt() % 10;
        long k = sc.nextLong();
        int[] place = new int[1000];
        int count = 0;
        int interval = 0;
        for (int i = 4; i <= 1004; i++) {
            if (i == k) {
                System.out.println((current / 100 + current % 100 / 10 + current % 10) % 10);
                return;
            }
             if (place[current] != 0) {
                count = place[current];
                interval = i - place[current];
                break;
            }
            
            place[current] = i;
            current = getNext(current);
        }
        long mod = (k - count) % interval;
        int ans = 0;
        for (int i = 0; i < 1000; i++) {
            if (count + mod == place[i]) {
                System.out.println((i / 100 + i % 100 / 10 + i % 10) % 10);
                return;
            }
        }
    }
    
    static int getNext(int x) {
        int count = x / 100 + x % 100 / 10 + x % 10;
        count %= 10;
        return (x * 10 + count) % 1000;
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public String next() throws Exception {
        if (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}
0