欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

1126 求递推序列的第N项

程序员文章站 2022-04-08 09:17:57
...

1126 求递推序列的第N项

1126 求递推序列的第N项

#include<iostream>
#include<algorithm>
#include<vector>
#include<cstdio>
typedef long long ll;
using namespace std;
typedef vector<ll> vec;
typedef vector<vec> mat;
ll n;
mat mul(mat &A, mat &B){
	mat C(A.size(), vec(B[0].size(), 0));
	for(int i = 0; i < A.size(); ++i){
		for(int j = 0; j < B.size(); ++j){
			for(int k = 0; k < B[0].size(); ++k){
				C[i][j] = (C[i][j] + A[i][k] * B[k][j]);
				C[i][j] %= 7;
			}
		}
	}
	return C;
} 
mat modPow(mat A, ll n){
	mat B(A.size(), vec(A.size()));
	for(int i = 0; i < A.size(); ++i)	B[i][i] = 1;
	while(n){
		if(n&1){
			B = mul(B, A);
		} 
		A = mul(A, A);
		n >>= 1;
	}
	return B;
}
void solve(ll a, ll b){
	mat A(2, vec(2));
	A[0][0] = a;
	A[0][1] = b;
	A[1][0] = 1;
	A[1][1] = 0;
	A = modPow(A, n - 2);
	printf("%lld\n", (A[0][0] + A[0][1] + 7) % 7);
}
int main(){
	ll a, b;
	scanf("%lld%lld%lld", &a, &b, &n);
	solve(a, b);
	return 0;
}

相关标签: 斐波那契数列