結果

問題 No.658 テトラナッチ数列 Hard
ユーザー 37zigen37zigen
提出日時 2020-03-22 06:20:45
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,256 ms / 2,000 ms
コード長 1,194 bytes
コンパイル時間 2,559 ms
コンパイル使用メモリ 74,132 KB
実行使用メモリ 65,512 KB
最終ジャッジ日時 2023-08-26 03:06:32
合計ジャッジ時間 11,084 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 121 ms
56,196 KB
testcase_01 AC 121 ms
55,812 KB
testcase_02 AC 124 ms
53,800 KB
testcase_03 AC 163 ms
56,268 KB
testcase_04 AC 720 ms
64,036 KB
testcase_05 AC 774 ms
63,700 KB
testcase_06 AC 884 ms
65,096 KB
testcase_07 AC 944 ms
64,804 KB
testcase_08 AC 1,040 ms
65,060 KB
testcase_09 AC 1,256 ms
65,284 KB
testcase_10 AC 1,229 ms
65,512 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.FileNotFoundException;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) throws FileNotFoundException {
		long t = System.currentTimeMillis();
		new Main().run();
		System.err.println(System.currentTimeMillis() - t);
	}
	
	final int MOD=17;

	int[][] mul(int[][] a,int[][] b) {
		int[][] ret=new int[a.length][b[0].length];
		for(int i=0;i<a.length;++i)
			for(int j=0;j<b[i].length;++j)
				for(int k=0;k<a[i].length;++k)
					ret[i][j]=(ret[i][j]+a[i][k]*b[k][j]%MOD)%MOD;
		return ret;
	}
	
	int[][] pow(int[][] a,long n) {
		int[][] ret=new int[a.length][a.length];
		for(int i=0;i<a.length;++i)ret[i][i]=1;
		for(;n>0;n>>=1,a=mul(a,a))if(n%2==1)ret=mul(ret,a);
		return ret;
	}
	
	
	void run() {
		Scanner sc = new Scanner(System.in);
		int Q=sc.nextInt();
		int[][] mat=new int[][] {{1,1,1,1},{1,0,0,0},{0,1,0,0},{0,0,1,0}};
		int[][] vec=new int[][] {{1},{0},{0},{0}};
		for(int q=0;q<Q;++q) {			
			long n=sc.nextLong();
			System.out.println(mul(pow(mat,n-1),vec)[3][0]%17);
		}
	}


	static void tr(Object... objects) {
		System.out.println(Arrays.deepToString(objects));
	}

}
0