結果

問題 No.718 行列のできるフィボナッチ数列道場 (1)
ユーザー aa
提出日時 2018-11-01 21:22:56
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,472 bytes
コンパイル時間 1,105 ms
コンパイル使用メモリ 103,660 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-12 16:34:51
合計ジャッジ時間 3,227 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <cmath>
#include <complex>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <bitset>
#include <algorithm>
#include <numeric>
#include <functional>
#include <cassert>
#include <iomanip>
using namespace std;

#define Rep(b, e, i) for(int i = b; i <= e; i++)
#define Repr(e, b, i) for(int i = e; i >= b; i--)
#define rep(n, i) Rep(0, n-1, i)
#define repr(n, i) Repr(n-1, 0, i)
#define all(v) (v).begin(), (v).end()
#define pb(v) push_back(v)
#define uniq(v) (v).erase(unique(all(v)),(v).end())
#define bitcnt(x) __builtin_popcount(x)
#define fst first
#define snd second
#define Pqaz(T) priority_queue<T,vector<T>,greater<T>>
#define Pqza(T) priority_queue<T>
#define put(x) cout << x;
#define putsp(x) cout << x << ' ';
#define putbd cout << "---------------------------------------------" << endl;
#define putln(x) cout << x << endl;
#define debug(x) cerr << #x << "=" << x << endl;
#define ENJYU std::ios::sync_with_stdio(false);std::cin.tie(0);

typedef long long ll;
typedef pair<ll, ll> llP;
typedef pair<int, int> intP;
typedef complex<double> comp;
typedef vector <int> vec;
typedef vector <ll> vecll;
typedef vector <double> vecd;
typedef vector <vec> mat;
typedef vector <vecll> matll;
typedef vector <vecd> matd;

//vector の中身を出力
template <class T>ostream &operator<<(ostream &o,const vector<T>&v)
{o<<"{";for(int i=0;i<(int)v.size();i++)o<<(i>0?", ":"")<<v[i];o<<"}";return o;}

const int MAX = 200020;
const double PI = 3.14159265358979323846;
const double EPS = 1e-12;
const int INF = 1<<29;
const ll INFL = 1e18;
const ll MOD = 1000000007;

const int dx[]={1,0,-1,0},dy[]={0,1,0,-1};
//const int dx[]={-1,-1,-1,0,1,1,1,0},dy[]={-1,0,1,1,1,0,-1,-1};

//********************************template END****************************************//

//型を決めてね!!!!!!!!!!!
typedef ll MatType;

//演算の単位元も決めてね!!!!!!!!!!!!!!
const MatType plus_base = 0;
const MatType mul_base = 1;

typedef vector <MatType> Vec;
typedef vector <Vec> Mat;

//A * B = C
Mat mul(const Mat &A, const Mat &B)
{
	//大前提
	assert((int)A[0].size() == (int)B.size());

	Mat C((int)A.size(), Vec((int)B[0].size(), plus_base));

	//メモリへのアクセス的にこうしたほうが早いのです
	rep((int)A.size(), i) rep((int)B.size(), k) rep((int)B[0].size(), j)
	{
		C[i][j] += A[i][k] * B[k][j] % MOD;
		C[i][j] %= MOD;
	}

	return C;
}

// v' = A * v
//オーバーロードしているんです!!!!!!!!!!!!
Vec mul(const Mat &A, const Vec &v)
{
	//大前提
	assert((int)A[0].size() == (int)v.size());

	Vec nv((int)A.size(), plus_base);

	rep((int)A.size(), i) rep((int)v.size(), j)
	{
		nv[i] += A[i][j] * v[j] % MOD;
		nv[i] %= MOD;
	}

	return nv;
}

// A ^ N
Mat mpow(Mat A, int N)
{
	//大前提
	assert((int)A[0].size() == (int)A.size() && N > 0);

	Mat Res((int)A.size(), Vec((int)A.size(), plus_base));

	// A ^ 0 = E
	rep((int)A.size(), i)
	{
		Res[i][i] = mul_base;
	}

	while(N)
	{
		if (N & 1)
		{
			Res = mul(Res, A);
		}
		A = mul(A, A);
		N >>= 1;
	}

	return Res;
}

void solve(void)
{
	ll N;
	cin >> N;

	Mat m(2, Vec(2, 1LL));
	m[1][1] = 0LL;

	m = mpow(m, N);

	ll ans = m[0][0] * m[0][1] % MOD;

	cout << ans << endl;

}

int main(void){
	solve();
	//cout << "yui(*-v・)yui" << endl;
	return 0;
}
0