結果
問題 | No.720 行列のできるフィボナッチ数列道場 (2) |
ユーザー |
![]() |
提出日時 | 2018-07-27 23:33:54 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 2 ms / 2,000 ms |
コード長 | 1,281 bytes |
コンパイル時間 | 1,489 ms |
コンパイル使用メモリ | 174,452 KB |
実行使用メモリ | 5,376 KB |
最終ジャッジ日時 | 2024-07-05 05:31:45 |
合計ジャッジ時間 | 2,441 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 20 |
ソースコード
#include<bits/stdc++.h> using namespace std; using Int = long long; const Int MOD=1e9+7; //<- alert!!! typedef vector<Int> arr; typedef vector<arr> mat; inline arr mul(const mat &a,arr &b,Int mod){ arr res(b.size(),0); for(Int i=0;i<(Int)b.size();i++) for(Int j=0;j<(Int)a[i].size();j++) (res[i]+=a[i][j]*b[j])%=mod; return res; } inline mat mul(const mat &a,const mat &b,Int mod){ mat res(a.size(),arr(b[0].size(),0)); for(Int i=0;i<(Int)a.size();i++) for(Int j=0;j<(Int)b[0].size();j++) for(Int k=0;k<(Int)b.size();k++) (res[i][j]+=a[i][k]*b[k][j])%=mod; return res; } inline mat mat_pow(mat a,Int n,Int mod){ mat res(a); for(Int i=0;i<(Int)a.size();i++) for(Int j=0;j<(Int)a[i].size();j++) res[i][j]=(i==j); while(n){ if(n&1) res=mul(a,res,mod); a=mul(a,a,mod); n>>=1; } return res; } Int fib(Int n){ mat A(2,arr(2,1)); A[1][1]=0; A=mat_pow(A,n,MOD); return A[1][0]; } Int calc(Int n,Int a,Int b,Int c,Int d){ mat A(3,arr(3,0)); A[0][0]=a; A[0][1]=b; A[1][0]=c; A[1][1]=d; A[2][1]=1; A[2][2]=1; A=mat_pow(A,n+1,MOD); return A[2][0]; } //INSERT ABOVE HERE signed main(){ Int n,m; cin>>n>>m; Int a=fib(m+1),b=fib(m),c=fib(m),d=fib(m-1); cout<<calc(n,a,b,c,d)<<endl; return 0; }