# HDU 1005 Number Sequence(矩阵快速幂，快速幂模板)

Problem Description
A number sequence is defined as follows:

f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7.

Given A, B, and n, you are to calculate the value of f(n).

Input
The input consists of multiple test cases. Each test case contains 3 integers A, B and n on a single line (1 <= A, B <= 1000, 1 <= n <= 100,000,000). Three zeros signal the end of input and this test case is not to be processed.

Output
For each test case, print the value of f(n) on a single line.

Sample Input
1 1 3 1 2 10 0 0 0

Sample Output
2 5

int main(){
int n;
int a;
cin >> a>>n;
int ans = 1;
while(n){//a^n
if(n&1){
ans *= a;
}
a*=a;
n>>=1;
ans %= 10000007;
}
cout << ans<<endl;
return 0;
}

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<set>
#include<map>
#include<queue>
#include<cmath>
#include<stdlib.h>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
const int maxn = 1e5 + 100;
double eps = 1e-8;
int a[4][4], b[4][4];
int ans[4][4];

struct mtx{
int a[4][4];
};
mtx mtpl(mtx x, mtx y){//将矩阵a乘b的结果直接放在tmp里
mtx tmp;
memset(tmp.a, 0, sizeof(tmp.a));
for(int i = 1; i <= 2; i++){
for(int j = 1; j <= 2; j++){
int sum = 0;
for(int k = 1; k <= 2; k++){
sum += x.a[i][k] * y.a[k][j];
}
sum%=7;
tmp.a[i][j] += sum;
}
}
return tmp;
}
int main(){
int A, B;
ll n;
while(scanf("%d %d %lld", &A, &B, &n) ){
if(A==B && B==n && n==0)break;
mtx a, ans;
ans.a[1][1] = ans.a[2][2] = 1;
ans.a[1][2] = ans.a[2][1] = 0;
a.a[1][1] = A;
a.a[1][2] = B;
a.a[2][1] = 1;
a.a[2][2] = 0;
n-=2;  //注意这里求的是a^(n-2)而不是a^n
if(n==-1) {
cout << 1<<endl;
continue;
}
while(n){
if(n&1)ans=mtpl(ans, a);
a=mtpl(a, a);
n>>=1;
}
cout << (ans.a[1][1]+ans.a[1][2]) %7<< endl;//人家说最后结果mod 7，WA的时候没想到这里要%7。。不是第一次了
}
return 0;
}