結果

問題 No.718 行列のできるフィボナッチ数列道場 (1)
ユーザー chocoruskchocorusk
提出日時 2018-07-27 23:27:15
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,629 bytes
コンパイル時間 532 ms
コンパイル使用メモリ 74,980 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-18 14:26:30
合計ジャッジ時間 1,524 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#define MOD 1000000007LL

using namespace std;
typedef long long int ll;
typedef pair<ll, ll> P;

long long int powmod(long long int a, long long int k, long long int m){
	if(a==0){
		return 0;
	}
	long long int apow[35];
	for(int i=0; i<35; i++){
		if(i==0){
			apow[i]=a;
		}else{
			apow[i]=apow[i-1]*apow[i-1]%m;
		}
	}
	long long int ans=1;
	for(int i=34; i>=0; i--){
		if(k>=(1ll<<i)){
			ans=ans*apow[i]%m;
			k-=(1ll<<i);
		}
	}
	return ans;
}
 
ll inv(ll x, ll p){
  return powmod(x, p-2, p);
}

P pow5(ll a, ll b, ll k){
	ll ap[35], bp[35];
	for(int i=0; i<35; i++){
		if(i==0){
			ap[i]=a, bp[i]=b;
		}else{
			ap[i]=(ap[i-1]*ap[i-1]%MOD+5*bp[i-1]*bp[i-1]%MOD)%MOD;
			bp[i]=2*ap[i-1]*bp[i-1]%MOD;
		}
	}
	ll a0=1, b0=0;
	for(int i=34; i>=0; i--){
		if(k>=(1ll<<i)){
			ll a1=a0;
			a0=(a0*ap[i]%MOD+5*b0*bp[i]%MOD)%MOD;
			b0=(a1*bp[i]+b0*ap[i])%MOD;
			k-=(1ll<<i);
		}
	}
	return P(a0, b0);
}

P inv5(ll a, ll b){
	ll t=(a*a%MOD-5*b*b%MOD+MOD)%MOD;
	t=inv(t, MOD);
	ll a1=a*t%MOD, b1=(MOD-b*t%MOD)%MOD;
	return P(a1, b1);
}

P mul(ll a1, ll a2, ll b1, ll b2){
	ll a=(a1*b1%MOD+5*a2*b2%MOD)%MOD, b=(a1*b2+a2*b1)%MOD;
	return P(a, b);
}

int main()
{
	ll n;
	cin>>n;
	ll iv2=500000004;
	P p1=pow5(iv2, iv2, n), p2=pow5(iv2, MOD-iv2, n);
	ll f1=(p1.second-p2.second+MOD)%MOD;
	P p3=pow5(iv2, iv2, n+1), p4=pow5(iv2, MOD-iv2, n+1);
	ll f2=(p3.second-p4.second+MOD)%MOD;
	cout<<f1*f2%MOD<<endl;
	return 0;
}
0