题目:http://poj.org/problem?id=3070
题目大意:
输 入 n , 求 ( 1 1 1 0 ) n 的 值 输入n,求 \begin{pmatrix} 1 & 1 \\ 1 & 0 \end{pmatrix} ^n的值 输入n,求(1110)n的值
解题方法:矩阵快速幂
code:
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cstdio>
using namespace std;
const int N = 2;
const int mod = 10000;
int res[N][N];
int temp[N][N];
int num[N][N];
void multi(int a[][N], int b[][N])
{
memset(temp, 0, sizeof(temp));
for(int i = 0; i < N; i++)
for(int j = 0; j < N; j++)
for(int k = 0; k < N; k++)
temp[i][j] = (temp[i][j] + a[i][k] * b[k][j]) % mod;
for(int i = 0; i < N; i++)
for(int j = 0; j < N; j++)
a[i][j] = temp[i][j];
}
void MatPow(int a[][N], int n)
{
memset(res, 0, sizeof(res));
for(int i = 0; i < N; i++) res[i][i] = 1;
while(n)
{
if(n&1)
{
multi(res, a);
}
multi(a, a);
n >>= 1;
}
}
int main()
{
int n;
while(~scanf("%d", &n) && (n != -1))
{
num[0][0] = 1;
num[0][1] = 1;
num[1][0] = 1;
num[1][1] = 0;
if(n == 0 || n == 1) printf("%d\n", n);
else
{
MatPow(num, n);
printf("%d\n", res[0][1]);
}
}
return 0;
}