結果
| 問題 |
No.720 行列のできるフィボナッチ数列道場 (2)
|
| コンテスト | |
| ユーザー |
tempura_pp
|
| 提出日時 | 2018-07-28 02:01:39 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 2 ms / 2,000 ms |
| コード長 | 1,315 bytes |
| コンパイル時間 | 818 ms |
| コンパイル使用メモリ | 93,800 KB |
| 実行使用メモリ | 6,944 KB |
| 最終ジャッジ日時 | 2024-07-05 18:10:05 |
| 合計ジャッジ時間 | 1,579 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 20 |
ソースコード
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<iomanip>
#include<math.h>
#include<complex>
#include<queue>
#include<deque>
#include<map>
#include<set>
#include<bitset>
using namespace std;
#define REP(i,m,n) for(int i=(int)m ; i < (int) n ; ++i )
#define rep(i,n) REP(i,0,n)
typedef long long ll;
typedef pair<int,int> pint;
typedef pair<ll,int> pli;
const int inf=1e9+7;
const ll longinf=1LL<<60 ;
const ll mod=1e9+7 ;
int dx[4]={1,0,-1,0} , dy[4]={0,1,0,-1} ;
typedef vector<vector<ll> > mat;
int sz;
mat mul(mat A,mat B){
mat C(sz,vector<ll>(sz));
rep(i,sz)rep(j,sz)rep(k,sz)C[i][j]=(C[i][j]+A[i][k]*B[k][j])%mod;
rep(i,sz)rep(j,sz)C[i][j]=(C[i][j]+mod)%mod;
return C;
}
mat pow(mat A,ll k){
mat B(sz,vector<ll>(sz));
rep(i,sz)B[i][i]=1;
while(k>0){
if(k&1)B=mul(A,B);
A=mul(A,A);
k/=2;
}
return B;
}
ll inv(ll n){
ll ret=1,k=mod-2;
n%=mod;
if(n<0)n+=mod;
while(k>0){
if(k&1)ret=ret*n%mod;
n=n*n%mod;
k>>=1;
}
return ret;
}
int main(){
ll n,m;
cin>>n>>m;
sz=2;
mat A={{1,1},{1,0}};
A=pow(A,m);
mat B=pow(A,n+1);
mat iA={{1+mod-A[1][1],A[0][1]},{A[1][0],1+mod-A[0][0]}};
B=mul(B,iA),A=mul(A,iA);
ll ans=(A[1][0]-B[1][0]+mod)%mod;
ll s=inv(iA[0][0]*iA[1][1]-iA[0][1]*iA[1][0]);
cout<<ans*s%mod<<endl;
return 0;
}
tempura_pp