結果

問題 No.891 隣接3項間の漸化式
ユーザー f1b_maxbl00df1b_maxbl00d
提出日時 2020-03-11 21:29:36
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,138 bytes
コンパイル時間 1,466 ms
コンパイル使用メモリ 136,980 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-10 01:41:15
合計ジャッジ時間 3,084 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 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 2 ms
4,376 KB
testcase_07 AC 1 ms
4,384 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 2 ms
4,376 KB
testcase_25 AC 2 ms
4,380 KB
testcase_26 AC 2 ms
4,380 KB
testcase_27 AC 1 ms
4,376 KB
testcase_28 AC 1 ms
4,376 KB
testcase_29 AC 1 ms
4,376 KB
testcase_30 AC 2 ms
4,380 KB
testcase_31 AC 1 ms
4,376 KB
testcase_32 AC 2 ms
4,376 KB
testcase_33 AC 1 ms
4,376 KB
testcase_34 AC 2 ms
4,376 KB
testcase_35 AC 2 ms
4,376 KB
testcase_36 AC 1 ms
4,376 KB
testcase_37 AC 2 ms
4,380 KB
testcase_38 AC 2 ms
4,380 KB
testcase_39 AC 1 ms
4,380 KB
testcase_40 AC 2 ms
4,380 KB
testcase_41 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <limits.h>
#include <algorithm>
#include <string>
#include <math.h>
#include <limits.h>
#include <queue>
#include <map>
#include <set>
#include <iomanip>
#include <bitset>
#include <cassert>
#include <random>
#include <functional>
#include <stack>
#include <iomanip>
#include <cassert>
//#include <boost/multiprecision/cpp_int.hpp>
#include <complex>
#include <cstdio>
#include <list>

//< in.txt > out.txt
using namespace std;
//std::ios::sync_with_stdio(false);
//std::cin.tie(0);
const long long MOD = 1e9 + 7;
typedef long long LL;
typedef long double LD;
typedef pair<LL, LL> PLL;
typedef pair<LD, LL> PDL;
typedef pair<LD, LD> PDD;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
//typedef boost::multiprecision::cpp_int bigint;

template<class T>
void in(T& x) {
	cin >> x;
}

template<class T1, class T2>
void in(pair<T1, T2>& p) {
	in(p.first);
	in(p.second);
}

template<class T>
void in(vector<T>& v, LL st = -1, LL en = -1) {
	if (st == -1) {
		st = 0;
		en = v.size() - 1;
	}
	for (LL n = st; n <= en; n++) {
		in(v[n]);
	}
}

VVLL modpow(VVLL base, LL p) {
	if (p == 0) {
		VVLL res;
		res.resize(2, VLL(2, 0));
		res[0][0] = 1;
		res[1][1] = 1;
		return res;
	}
	if (p == 1) return base;
	VVLL res = modpow(base, p / 2);
	VVLL temp;
	temp.resize(2, VLL(2,0));
	for (LL x = 0; x < 2; x++) {
		for (LL y = 0; y < 2; y++) {
			for (LL k = 0; k < 2; k++) {
				temp[x][y] += res[x][k] * res[k][y];
				temp[x][y] %= MOD;
			}
		}
	}
	if (p % 2 == 0)return temp;
	VVLL temp2;
	temp2.resize(2, VLL(2, 0));
	for (LL x = 0; x < 2; x++) {
		for (LL y = 0; y < 2; y++) {
			for (LL k = 0; k < 2; k++) {
				temp2[x][y] += temp[x][k] * base[k][y];
				temp2[x][y] %= MOD;
			}
		}
	}
	return temp2;
}

int main() {
	std::ios::sync_with_stdio(false);
	std::cin.tie(0);
	LL a, b, n;
	cin >> a >> b >> n;
	if (n == 0) {
		cout << 0 << "\n";
		return 0;
	}
	else if (n == 1) {
		cout << 1 << "\n";
		return 0;
	}
	VVLL base;
	base.resize(2, VLL(2, 0));
	base[0][0] = a;
	base[0][1] = b;
	base[1][0] = 1;
	base = modpow(base, n - 1);
	cout << (base[0][0] + MOD) % MOD << "\n";
	return 0;
}
0