結果

問題 No.658 テトラナッチ数列 Hard
ユーザー tetsutetsu
提出日時 2018-03-03 16:31:48
言語 Java21
(openjdk 21)
結果
AC  
実行時間 983 ms / 2,000 ms
コード長 1,974 bytes
コンパイル時間 3,483 ms
コンパイル使用メモリ 74,400 KB
実行使用メモリ 60,984 KB
最終ジャッジ日時 2023-09-12 16:47:45
合計ジャッジ時間 9,835 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
49,324 KB
testcase_01 AC 44 ms
49,436 KB
testcase_02 AC 45 ms
49,396 KB
testcase_03 AC 61 ms
52,156 KB
testcase_04 AC 563 ms
60,648 KB
testcase_05 AC 615 ms
60,656 KB
testcase_06 AC 689 ms
60,984 KB
testcase_07 AC 710 ms
60,776 KB
testcase_08 AC 796 ms
60,680 KB
testcase_09 AC 983 ms
60,884 KB
testcase_10 AC 968 ms
60,736 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Matrix {

	static int [][] pow(int[][] A, long n) {
		if(n==1) {
			return A;
		}
		if(n%2==0) {
			int[][] B = pow(A, n/2);
			return mul(B, B);
		} else {
			int[][] B = mul(A, pow(A, n-1));
			return B;
		}
	}
	
	static int MOD = 17;
	static int[][] mul(int [][] A, int [][] B) {
		assert A[0].length == B.length;
		int n = A.length;
		int k = A[0].length;
		int m = B[0].length;
		int[][] C = new int[n][m];
		for(int i=0; i<n; i++) {
			for(int j=0; j<m; j++) {
				for(int l=0; l<k; l++) {
					C[i][j] = (C[i][j] + A[i][l]*B[l][j])%MOD; // YOU NEED TO CHANGE MOD.
				}
			}
		}
		return C;
	}

	// https://yukicoder.me/problems/no/658
	public static void main(String[] args) throws IOException {
		MyScanner sc = new MyScanner(System.in);
		int Q = sc.nextInt();
		for(int i=0; i<Q; i++) {
			long n = sc.nextLong();
			System.out.println(solve(n));
		}
	}
	
	static int solve(long n) {
		if(n==1||n==2||n==3) return 0;
		if(n==4) return 1;
		int[][] A = {{1, 1, 1, 1}, {1, 0, 0, 0}, {0, 1, 0, 0}, {0, 0, 1, 0}};
		return pow(A, n-4)[0][0];
	}

	static class MyScanner
	{
		BufferedReader br;
		StringTokenizer st;
		public MyScanner(InputStream s)
		{
			br=new BufferedReader(new InputStreamReader(s));
		}
		public String nextLine() throws IOException
		{
			return br.readLine();
		}
		public String next() throws IOException
		{
			while(st==null || !st.hasMoreTokens())
				st=new StringTokenizer(br.readLine());
			return st.nextToken();
		}
		public int nextInt() throws IOException
		{
			return Integer.parseInt(next());
			
		}
		public double nextDouble() throws IOException
		{
			return Double.parseDouble(next());
		}
		public boolean ready() throws IOException
		{
			return br.ready();
		}
		public long nextLong() throws IOException
		{
			return Long.parseLong(next());
		}
}
}
0