#include<stdio.h>
#include<iostream>
using namespace std;
const int N= 2;
const int MOD = 7;
const int F[3] = { 0,1,1 };
struct Mat
{
int A[N][N];
Mat()
{
for (int i = 0; i < N; ++i)
for (int j = 0; j < N; ++j)
A[i][j] = 0;
}
Mat(initializer_list<int> rhs)
{
auto it = rhs.begin();
for (int i = 0; it != rhs.end(); ++it,++i)
A[i >> 1][i & 1] = *it;
}
Mat operator*(const Mat &rhs)
{
Mat c;
for (int i = 0; i < N; ++i)
for (int j = 0; j < N; ++j)
for (int k = 0; k < N; ++k)
c.A[i][j] = (c.A[i][j] + A[i][k] * rhs.A[k][j]) % MOD;
return c;
}
friend Mat operator^(Mat a, int b)
{
Mat r;
for (int i = 0; i < N; ++i)
r.A[i][i] = 1;
while (b)
{
if (b & 1)
r = r*a;
a = a*a;
b >>= 1;
}
return r;
}
};
int main(void)
{
int A, B, n;
while (~scanf("%d%d%d", &A, &B, &n) && (A || B || n))
{
Mat left = { A,B,1,0 };
Mat right = { F[2],0,F[1],0 };
if (n <= 2)
printf("%d\n", F[n]);
else
{
left = left ^ (n - 2);
Mat resultMat = left*right;
printf("%d\n", resultMat.A[0][0]);
}
}
return 0;
}
我原来的代码是有A[i][j]=0的 .不知道为什么发到论坛上就没有【i】了,你有QQ吗,能私下个不交流吗 |