結果

問題 No.891 隣接3項間の漸化式
ユーザー 沙耶花沙耶花
提出日時 2019-09-20 22:24:50
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,073 bytes
コンパイル時間 1,846 ms
コンパイル使用メモリ 176,204 KB
実行使用メモリ 4,372 KB
最終ジャッジ日時 2023-10-12 19:53:11
合計ジャッジ時間 3,986 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,352 KB
testcase_01 AC 2 ms
4,352 KB
testcase_02 AC 2 ms
4,352 KB
testcase_03 AC 1 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 1 ms
4,356 KB
testcase_07 AC 2 ms
4,352 KB
testcase_08 AC 1 ms
4,352 KB
testcase_09 AC 1 ms
4,348 KB
testcase_10 AC 1 ms
4,352 KB
testcase_11 AC 2 ms
4,352 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 2 ms
4,352 KB
testcase_14 AC 2 ms
4,352 KB
testcase_15 AC 1 ms
4,352 KB
testcase_16 AC 2 ms
4,352 KB
testcase_17 AC 1 ms
4,352 KB
testcase_18 AC 2 ms
4,348 KB
testcase_19 AC 2 ms
4,348 KB
testcase_20 AC 2 ms
4,352 KB
testcase_21 AC 2 ms
4,352 KB
testcase_22 AC 1 ms
4,352 KB
testcase_23 AC 1 ms
4,348 KB
testcase_24 AC 1 ms
4,356 KB
testcase_25 AC 1 ms
4,348 KB
testcase_26 AC 2 ms
4,372 KB
testcase_27 AC 2 ms
4,348 KB
testcase_28 AC 1 ms
4,352 KB
testcase_29 AC 1 ms
4,348 KB
testcase_30 AC 2 ms
4,352 KB
testcase_31 AC 1 ms
4,352 KB
testcase_32 AC 1 ms
4,348 KB
testcase_33 AC 1 ms
4,352 KB
testcase_34 AC 2 ms
4,368 KB
testcase_35 AC 1 ms
4,352 KB
testcase_36 AC 2 ms
4,352 KB
testcase_37 AC 2 ms
4,352 KB
testcase_38 AC 1 ms
4,348 KB
testcase_39 AC 2 ms
4,352 KB
testcase_40 AC 2 ms
4,348 KB
testcase_41 AC 2 ms
4,352 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define modulo 1000000007
#define mod(mod_x) ((((long long)mod_x+modulo))%modulo)
#define Inf 100000000


int beki(int a,int b){
	int x = 1;
	while(b!=0){
		if(b&1){
			x=mod(x*a);
		}
		a=mod(a*a);
		b>>=1;
	}
	return x;
}

struct matrix{
	vector<vector<long long>> value;
	
	matrix(int height,int width){
		value.resize(height,vector<long long>(width,0));
		if(height==width){
			for(int i=0;i<get_width();i++){
				value[i][i] = 1;
			}
		}
	}
	
	void resize(int height,int width){
		value.resize(height,vector<long long>(width,0));
		for(int i=0;i<get_width();i++){
			value[i][i] = 1;
		}
	}
	
	void set_value(vector<vector<long long>> &V){
		value = V;
	}
	
	void set_value(initializer_list<long long> list){
		auto it = list.begin();
		for(int i=0;i<get_height();i++){
			for(int j=0;j<get_width();j++){
				value[i][j]=*it;
				if(i!=get_height()-1||j!=get_width()-1)it++;
			}
		}
	}
	
	int get_width(){
		return value[0].size();
	}
	
	int get_height(){
		return value.size();
	}
	
	void print(){
		for(int i=0;i<get_height();i++){
			for(int j=0;j<get_width();j++){
				if(j!=0)cout<<' ';
				cout<<value[i][j];
			}
			cout<<endl;
		}
	}
	
};

matrix matrix_multiple(matrix &A,matrix &B){

	matrix R(A.get_height(),B.get_width());
	int multi_cnt;
	if(A.get_width()!=B.get_height()){
		assert(false);
	}
	else multi_cnt = A.get_width();
	
	for(int i=0;i<R.get_height();i++){
		for(int j=0;j<R.get_width();j++){
			R.value[i][j]=0;
			for(int k=0;k<multi_cnt;k++){
				
				R.value[i][j] = mod(R.value[i][j] + mod(A.value[i][k] * B.value[k][j]));
	
			}
		}
	}
	return R;
	
}

matrix matrix_beki(matrix A,long long cnt){
	if(A.get_width()!=A.get_height())assert(0);
	matrix R(A.get_width(),A.get_width());
	while(cnt!=0){
		if(cnt&1){
			R=matrix_multiple(R,A);
		}
		A=matrix_multiple(A,A);
		cnt>>=1;
	}
	return R;
}

int main(){
	
	int a,b,n;
	cin>>a>>b>>n;
	
	if(n==0){
		cout<<0<<endl;
	}
	else{
		matrix R(2,2);
		R.set_value({a,b,1,0});
		R = matrix_beki(R,n-1);
		cout<<R.value[0][0]<<endl;
	}
	
	return 0;
}

0