結果

問題 No.658 テトラナッチ数列 Hard
ユーザー xxxasdfghjkxxxasdfghjk
提出日時 2018-08-13 19:44:35
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 271 ms / 2,000 ms
コード長 1,198 bytes
コンパイル時間 782 ms
コンパイル使用メモリ 79,500 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-24 16:44:48
合計ジャッジ時間 2,792 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 3 ms
4,348 KB
testcase_04 AC 104 ms
4,348 KB
testcase_05 AC 118 ms
4,348 KB
testcase_06 AC 147 ms
4,348 KB
testcase_07 AC 156 ms
4,348 KB
testcase_08 AC 185 ms
4,348 KB
testcase_09 AC 271 ms
4,348 KB
testcase_10 AC 271 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<cstdio>
#include<queue>
#include<utility>
#include<cstring>
#include<stack>
#include<algorithm>
#include<cmath>
#include<iostream>
#include<map>
#define MAX_N 100001
#define INF_INT 2147483647
#define INF_LL 9223372036854775807
#define REP(i,n) for(int i=0;i<(int)(n);i++)
using namespace std;
typedef long long int ll;
typedef pair<ll,ll> P;
typedef vector<int> vec;
typedef vector<vec> mat;

const int M = 17;

mat mul(mat &A, mat &B){
  mat C(A.size(),vec(B[0].size()));
    REP(i,A.size())REP(k,B.size())REP(j,B[0].size()){
    C[i][j] = (C[i][j] + A[i][k] * B[k][j]) % M;
  }
  return C;
}

mat pow(mat A,ll n)
{
  mat B(A.size(),vec(A.size()));
  REP(i,A.size()){
    B[i][i] = 1;
  }
  while(n > 0){
    if ( n & 1) B = mul(B,A);
    A = mul(A,A);
    n >>= 1;
  }
  return B;
}

int main()
{
  int Q;
  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;
  ll n;
  cin >> Q;
  REP(i,Q){
    cin >> n;
    mat B = pow(A,n-1);
    cout << B[3][0] << endl;
  }
  
  return 0;
}

0