結果

問題 No.718 行列のできるフィボナッチ数列道場 (1)
ユーザー matsukin1111matsukin1111
提出日時 2019-04-25 14:26:55
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 1,796 bytes
コンパイル時間 1,368 ms
コンパイル使用メモリ 113,388 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-29 20:58:31
合計ジャッジ時間 2,387 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include<iostream>
#include<cstdio>
#include<cstring>
#include <cstdlib>  
#include <cmath>   
#include<cctype>
#include<string>
#include<set>
#include<iomanip>
#include <map>
#include<algorithm>
#include <functional>
#include<vector>
#include<climits>
#include<stack>
#include<queue>
#include <deque>
#include <climits>
#include <typeinfo>
#include <utility> 
#define all(x) (x).begin(),(x).end()
#define rep(i,m,n) for(int i = m;i < n;++i)
#define pb push_back
#define fore(i,a) for(auto &i:a)
#define rrep(i,m,n) for(int i = m;i >= n;--i)
#define INF INT_MAX/2
using namespace std;
using ll = long long;
using R = double;
using Data = pair<ll, vector<int>>;
const ll MOD = 1e9 + 7;
const ll inf = 1LL << 50;
struct edge { ll from; ll to; ll cost; };


ll fib[101010];
ll sum[101010];


struct Fibonacci {
	ll mod = 1e9 + 7;
	vector<vector<ll>> matpow(vector<vector<ll>>a,vector<vector<ll>>b){
		vector<vector<ll>>ret(a.size(), vector<ll>(b[0].size(),0));
		rep(i, 0, a.size()) {
			rep(j, 0, b[0].size()) {
				rep(k, 0, b.size()) {
					ret[i][j] += a[i][k] * b[k][j];
					ret[i][j] %= mod;
				}
			}
		}
		return ret;
	}


	map<ll, vector<vector<ll>>>memo;
	set<int>used;
	vector<vector<ll>> pow(ll n) {
		if (n == 1) {
			vector<vector<ll>> ret = { {1,1},{1,0} };
			return ret;
		}
		else if (used.count(n) != 0) {
			return memo[n];
		}
		else if (n % 2 == 0) {
			used.insert(n);
			return memo[n] = matpow(pow(n/2),pow(n/2));
		}
		else {
			used.insert(n);
			return memo[n] = matpow(pow(1),pow(n-1));
		}
	}

	ll query(ll n) {
		if (n == 0)return 0;
		else if (n == 1) return 1;
		vector<vector<ll>>ans = pow(n);
		return ans[1][0];
	}
};


int main() {
	ll n;
	cin >> n;
	Fibonacci f;
	
	ll fn = f.query(n);
	ll fn1 = f.query(n + 1);
	cout << (fn*fn1) % MOD << endl;

	return 0;
}
0