結果

問題 No.658 テトラナッチ数列 Hard
ユーザー tentententen
提出日時 2024-03-07 14:42:06
言語 Java21
(openjdk 21)
結果
AC  
実行時間 338 ms / 2,000 ms
コード長 2,412 bytes
コンパイル時間 2,799 ms
コンパイル使用メモリ 87,860 KB
実行使用メモリ 64,416 KB
最終ジャッジ日時 2024-03-07 14:42:12
合計ジャッジ時間 5,868 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 70 ms
54,756 KB
testcase_01 AC 69 ms
54,776 KB
testcase_02 AC 69 ms
54,752 KB
testcase_03 AC 73 ms
54,744 KB
testcase_04 AC 207 ms
60,096 KB
testcase_05 AC 220 ms
62,712 KB
testcase_06 AC 209 ms
60,060 KB
testcase_07 AC 251 ms
60,368 KB
testcase_08 AC 202 ms
59,624 KB
testcase_09 AC 338 ms
64,416 KB
testcase_10 AC 238 ms
59,460 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;
import java.util.function.*;
import java.util.stream.*;

public class Main {
    static final int MOD = 17;
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int q = sc.nextInt();
        int[][][] matrix = new int[60][4][4];
        matrix[0] = new int[][]{{0, 1, 0, 0}, {0, 0, 1, 0}, {0, 0, 0, 1}, {1, 1, 1, 1}};
        for (int i = 1; i < 60; i++) {
            for (int a = 0; a < 4; a++) {
                for (int b = 0; b < 4; b++) {
                    for (int c = 0; c < 4; c++) {
                        matrix[i][a][c] += matrix[i - 1][a][b] * matrix[i - 1][b][c] % MOD;
                        matrix[i][a][c] %= MOD;
                    }
                }
            }
        }
        String result = IntStream.range(0, q).mapToObj(x -> {
            long n = sc.nextLong() - 1;
            int[] ans = new int[]{0, 0, 0, 1};
            for (int i = 59; i >= 0; i--) {
                if (n < (1L << i)) {
                    continue;
                }
                n -= (1L << i);
                int[] next = new int[4];
                for (int a = 0; a < 4; a++) {
                    for (int b = 0; b < 4; b++) {
                        next[a] += matrix[i][a][b] * ans[b] % MOD;
                        next[a] %= MOD;
                    }
                }
                ans = next;
            }
            return String.valueOf(ans[0]);
        }).collect(Collectors.joining("\n"));
        System.out.println(result);
    }
}
class Scanner {
    BufferedReader br;
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    public Scanner() {
        try {
            br = new BufferedReader(new InputStreamReader(System.in));
        } catch (Exception e) {
            
        }
    }
    
    public int nextInt() {
        return Integer.parseInt(next());
    }
    
    public long nextLong() {
        return Long.parseLong(next());
    }
    
    public double nextDouble() {
        return Double.parseDouble(next());
    }
    
    public String next() {
        try {
            while (!st.hasMoreTokens()) {
                st = new StringTokenizer(br.readLine());
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return st.nextToken();
        }
    }
    
}
0