难度:3
[f(n) , f(n-1)] = [f(n-1) , f(n-2)] * [A,1;B,0]
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 2;
int n , MOD;
struct Matrix {
int n , m;
int a[N][N];
Matrix() {}
Matrix(int _n,int _m):n(_n),m(_m){};
void intput() {
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
scanf("%d",&a[i][j]);
}
Matrix operator + (const Matrix &b) {
Matrix tmp = Matrix(n,m);
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
tmp.a[i][j] = a[i][j] + b.a[i][j];
return tmp;
}
Matrix operator - (const Matrix &b) {
Matrix tmp = Matrix(n,m);
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
tmp.a[i][j] = a[i][j] - b.a[i][j];
return tmp;
}
Matrix operator * (const Matrix &b) {
Matrix tmp = Matrix(n,b.m);
for(int i=0;i<n;i++)
for(int j=0;j<b.m;j++)
tmp.a[i][j] = 0;
for(int i=0;i<n;i++)
for(int j=0;j<b.m;j++)
for(int k=0;k<m;k++) {
tmp.a[i][j] += a[i][k]*b.a[k][j];
tmp.a[i][j] %= MOD;
}
return tmp;
}
};
Matrix operator ^ (Matrix a , int p) {
Matrix ret = Matrix(a.n,a.m);
for(int i=0;i<a.n;i++)
for(int j=0;j<a.m;j++)
ret.a[i][j] = (i == j ? 1 : 0);
while(p) {
if(p%2) ret = ret * a;
a = a * a;
p /= 2;
}
return ret;
}
Matrix A , B;
int a , b;
int main() {
MOD = 7;
while(~scanf("%d%d%d" , &a,&b,&n) && a+b+n) {
if(n <= 2) {
puts("1"); continue;
}
A = Matrix(1 , 2);
B = Matrix(2 , 2);
A.a[0][0] = A.a[0][1] = 1;
B.a[0][0] = a; B.a[0][1] = 1;
B.a[1][0] = b; B.a[1][1] = 0;
A = A * (B^(n-2));
int ans = A.a[0][0] % 7;
printf("%d\n" , ans);
}
return 0;
}