結果
| 問題 |
No.658 テトラナッチ数列 Hard
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2018-03-03 16:31:48 |
| 言語 | Java (openjdk 23) |
| 結果 |
AC
|
| 実行時間 | 1,017 ms / 2,000 ms |
| コード長 | 1,974 bytes |
| コンパイル時間 | 3,580 ms |
| コンパイル使用メモリ | 78,368 KB |
| 実行使用メモリ | 61,804 KB |
| 最終ジャッジ日時 | 2024-06-30 04:42:13 |
| 合計ジャッジ時間 | 10,017 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 8 |
ソースコード
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());
}
}
}