結果

問題 No.720 行列のできるフィボナッチ数列道場 (2)
ユーザー tempura_pptempura_pp
提出日時 2018-07-28 02:01:39
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,315 bytes
コンパイル時間 1,493 ms
コンパイル使用メモリ 96,120 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-19 05:52:50
合計ジャッジ時間 2,096 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#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;
}
0