結果

問題 No.1275 綺麗な式
ユーザー rogi52
提出日時 2020-10-31 01:29:33
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 1,145 bytes
コンパイル時間 1,695 ms
コンパイル使用メモリ 176,296 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-07-22 04:13:00
合計ジャッジ時間 3,285 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 60
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;

const ll mod = 1000000007;
vector<vector<long long>> matmul(vector<vector<long long>> A, vector<vector<long long>> B){
	int N = A.size();
	vector<vector<long long>> ans(N, vector<long long>(N, 0));
	for (int i = 0; i < N; i++){
		for (int j = 0; j < N; j++){
			for (int k = 0; k < N; k++){
				ans[i][k] += A[i][j] * B[j][k];
				ans[i][k] %= mod;
			}
		}
	}
	return ans;
}
vector<vector<long long>> matpow(vector<vector<long long>> A, long long b){
	int N = A.size();
	vector<vector<long long>> ans(N, vector<long long>(N, 0));
	for (int i = 0; i < N; i++){
		ans[i][i] = 1;
	}
	while (b > 0){
		if (b % 2 == 1){
			ans = matmul(ans, A);
		}
		A = matmul(A, A);
		b /= 2;
	}
	return ans;
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(0);

    ll a,b; cin>>a>>b;
    ll n; cin>>n;
    vector<vector<long long>> mat = {
        {a * 2 % mod, (b - a * a % mod + mod) % mod},
        {1,0}
    };

    mat = matpow(mat, n);
    ll ans = (mat[1][0] * a * 2 + mat[1][1] * 2) % mod;
    cout << ans << '\n';

    return 0;
}
0