結果

問題 No.658 テトラナッチ数列 Hard
ユーザー ikdikd
提出日時 2018-03-02 23:48:31
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 63 ms / 2,000 ms
コード長 1,278 bytes
コンパイル時間 613 ms
コンパイル使用メモリ 85,404 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-03 18:55:12
合計ジャッジ時間 1,656 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 29 ms
4,376 KB
testcase_05 AC 32 ms
4,376 KB
testcase_06 AC 38 ms
4,376 KB
testcase_07 AC 40 ms
4,376 KB
testcase_08 AC 45 ms
4,380 KB
testcase_09 AC 63 ms
4,380 KB
testcase_10 AC 63 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

void main(){
  import std.stdio, std.string, std.conv, std.algorithm;

  const int mod=17;

  auto mat=new int[][][](64, 4, 4);
  mat[0][0][0]=1; mat[0][0][1]=1; mat[0][0][2]=1; mat[0][0][3]=1;
  mat[0][1][0]=1; mat[0][1][1]=0; mat[0][1][2]=0; mat[0][1][3]=0;
  mat[0][2][0]=0; mat[0][2][1]=1; mat[0][2][2]=0; mat[0][2][3]=0;
  mat[0][3][0]=0; mat[0][3][1]=0; mat[0][3][2]=1; mat[0][3][3]=0;

  foreach(t; 1..64){
    auto prev=mat[t-1], nex=mat[t];
    foreach(i; 0..4)foreach(j; 0..4){
      foreach(k; 0..4) (nex[i][j]+=prev[i][k]*prev[k][j])%=mod;
    }
  }

  int q; rd(q);
  while(q--){
    long n; rd(n);
    if(n<=3){writeln(0); continue;}
    if(n==4){writeln(1); continue;}
    auto a=new int[](4);
    a[0]=1; a[1]=0; a[2]=0; a[3]=0;
    n-=4;
    foreach(t; 0..64)if(n&(1L<<t)){
      auto b=new int[](4);
      foreach(i; 0..4)foreach(j; 0..4) (b[i]+=mat[t][i][j]*a[j])%=mod;
      a.swap(b);
    }
    writeln(a[0]);
  }
}

/+

  a5   1 1 1 1 a4
  a4 = 1 0 0 0 a3
  a3   0 1 0 0 a2
  a2   0 0 1 0 a1

  a1=a2=a3=0, a4=1

  a5 ... T   ... 001
  a6 ... T^2 ... 010
  a7 ... T^3 ... 011

+/

void rd(T...)(ref T x){
  import std.stdio, std.string, std.conv;
  auto l=readln.split;
  assert(l.length==x.length);
  foreach(i, ref e; x){
    e=l[i].to!(typeof(e));
  }
}
0