結果

問題 No.658 テトラナッチ数列 Hard
ユーザー tetsutetsu
提出日時 2018-03-02 22:56:41
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 280 ms / 2,000 ms
コード長 1,300 bytes
コンパイル時間 713 ms
コンパイル使用メモリ 74,928 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-04 05:47:50
合計ジャッジ時間 2,603 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 3 ms
4,376 KB
testcase_04 AC 109 ms
4,380 KB
testcase_05 AC 125 ms
4,380 KB
testcase_06 AC 154 ms
4,376 KB
testcase_07 AC 165 ms
4,376 KB
testcase_08 AC 192 ms
4,376 KB
testcase_09 AC 279 ms
4,376 KB
testcase_10 AC 280 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <string>
#include <iostream>
#include <vector>
using namespace std;
#define MAX 1000001
typedef vector<int> vec;
typedef vector<vec> mat;
mat mul(mat &A, mat &B) {
  mat C(A.size(), vec(B[0].size()));
  for(int i=0; i<A.size(); i++) {
    for(int k=0; k<B.size(); k++) {
      for(int j=0; j<B[0].size(); j++) {
	C[i][j] = (C[i][j]+A[i][k]*B[k][j])%17;
      }
    }
  }
  return C;
}
mat pow(mat A, long n) {
  mat B(A.size(), vec(A.size()));
  for(int i=0; i<A.size(); i++) {
    B[i][i] = 1;
  }
  while(n>0) {
    if(n&1) B = mul(B, A);
    A = mul(A, A);
    n >>= 1;
  }
  return B;
}
int T(long n) {
  if(n==1) return 0;
  if(n==2) return 0;
  if(n==3) return 0;
  if(n==4) return 1;
  mat A(4, vec(4));
  A[0][0] = 1; A[0][1] = 1; A[0][2] = 1; A[0][3] = 1;
  A[1][0] = 1; A[1][1] = 0; A[1][2] = 0; A[1][3] = 0;
  A[2][0] = 0; A[2][1] = 1; A[2][2] = 0; A[2][3] = 0;
  A[3][0] = 0; A[3][1] = 0; A[3][2] = 1; A[3][3] = 0;
  A = pow(A, n-4);
  // printf("@@@\n");
  // for(int i=0; i<4; i++) {
  //   printf("%d %d %d %d\n", A[i][0], A[i][1], A[i][2], A[i][3]);
  // }
  // printf("@@@\n");
  return A[0][0];
}
int main() {
  int q;
  cin >> q;
  // printf("q: %d\n", q);
  for(int i=0; i<q; i++) {
    long n;
    cin >> n;
    printf("%d\n", T(n));
  }
  return 0;
}
0