結果

問題 No.718 行列のできるフィボナッチ数列道場 (1)
ユーザー ikdikd
提出日時 2018-07-27 22:43:53
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 946 bytes
コンパイル時間 631 ms
コンパイル使用メモリ 85,104 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-03 20:49:55
合計ジャッジ時間 1,744 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

  const int m=40;
  const long mod=1e9.to!long+7;
  auto mat=new long[][][](m, 2, 2);
  mat[0][0][0]=mat[0][0][1]=1;
  mat[0][1][0]=1; mat[0][1][1]=0;
  foreach(t; 1..m){
    auto prev=mat[t-1], cur=mat[t];
    foreach(i; 0..2)foreach(j; 0..2){
      foreach(k; 0..2) (cur[i][j]+=prev[i][k]*prev[k][j])%=mod;
    }
  }

  long n; rd(n);
  if(n==1){
    writeln(1);
    return;
  }
  n--;
  auto a=new long[](2);
  a[0]=1;
  foreach(t; 0..m)if(n&(1L<<t)){
    auto b=new long[](2);
    foreach(i; 0..2)foreach(j; 0..2) (b[i]+=mat[t][i][j]*a[j]%mod)%=mod;
    a.swap(b);
  }
  writeln(a[0]*(a[0]+a[1])%mod);
}

/*

  f_n      1 1  f_{n-1}
  f_{n-1}  1 0  f_{n-2}

  f_1   E 000
  f_2   T 001
  f_3 T^2 010

*/

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