結果

問題 No.891 隣接3項間の漸化式
ユーザー tkmst201tkmst201
提出日時 2019-09-20 21:41:06
言語 C++11
(gcc 13.3.0)
結果
WA  
実行時間 -
コード長 1,553 bytes
コンパイル時間 1,531 ms
コンパイル使用メモリ 166,392 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-09-14 16:57:52
合計ジャッジ時間 2,738 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 35 WA * 4
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
#define fi first
#define se second
template<typename A, typename B> inline bool chmax(A &a, B b) { if (a<b) { a=b; return 1; } return 0; }
template<typename A, typename B> inline bool chmin(A &a, B b) { if (a>b) { a=b; return 1; } return 0; }
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const ll INF = 1ll<<30;
const ll longINF = 1ll<<60;
const ll MOD = 1000000007;
const double EPS = 1e-9;
const bool debug = 0;
//---------------------------------//

typedef vector<ll> vl;
typedef vector<vl> vvl;

vvl multi(vvl &a, vvl &b, ll mod) {
	vvl res(a.size(), vl(b[0].size()));
	
	REP(i, a.size()) {
		REP(j, b[0].size()) {
			REP(k, b.size()) {
				res[i][j] += a[i][k] * b[k][j] % mod;
				res[i][j] %= mod;
			}
		}
	}
	
	return res;
}

vvl pow(vvl x, ll n, ll mod) {
	vvl res(x.size(), vl(x.size()));
	
	REP(i, x.size()) res[i][i] = 1;
	
	while (n > 0) {
		if (n & 1) res = multi(res, x, mod);
		x = multi(x, x, mod);
		n >>= 1;
	}
	
	return res;
}

vvl init(ll a, ll b) {
	vvl res(2, vl(2));
	res[0][0] = a;
	res[0][1] = b;
	res[1][0] = 1;
	res[1][1] = 0;
	return res;
}

ll solve(ll x, ll mod, ll a, ll b) {
	vvl cur = init(a, b);
	
	vvl se(2, vl(1));
	se[0][0] = 1;
	se[1][0] = 0;
	
	cur = pow(cur, x - 1, mod);
	cur = multi(cur, se, mod);
	
	return cur[0][0];
}

int main() {
	ll a, b, n;
	cin >> a >> b >> n;
	
	cout << solve(n, MOD, a, b) << endl;
	return 0;
}
0