hdu 3306

http://acm.hdu.edu.cn/showproblem.php?pid=3306
题目大意:A(0) = 1 , A(1) = 1 , A(N) = X A(N - 1) + Y A(N - 2) (N >= 2);给定三个值N,X,Y求S(N):S(N) = A(0)2 +A(1)2+……+A(n)2。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include<stdio.h>
#include<cstring>
#include<algorithm>
#include<iostream>
#define mem(x,y) memset(x,y,sizeof(x))
#define inf 0x3f3f3f3f
using namespace std;
#define N 4
#define Mytype int
Mytype mod;
struct matrix
{
Mytype a[N][N];
matrix operator*(const matrix &y)const
{
matrix res;
mem(res.a,0);
for(int i=0;i<N;i++)
for(int j=0;j<N;j++)
if(a[i][j])
for(int k=0;k<N;k++)
res.a[i][k]+=a[i][j]*y.a[j][k],res.a[i][k]%=mod;
return res;
}
matrix operator+(const matrix &y)const
{
matrix res;
for(int i=0;i<N;i++)
for(int j=0;j<N;j++)
res.a[i][j]=a[i][j]+y.a[i][j],res.a[i][j]%=mod;
return res;
}
};
matrix qmod(matrix a,long long k)
{

matrix res;
mem(res.a,0);
for(int i=0;i<N;i++)
res.a[i][i]=1;
while(k)
{
if(k&1)
res=res*a;
a=a*a;
k>>=1;
}
return res;
}
int main()
{

mod=10007;
long long m,x,y;
while(~scanf("%I64d%I64d%I64d",&m,&x,&y))
{
matrix a;
mem(a.a,0);
x%=mod;
y%=mod;
a.a[0][0]=1,a.a[0][1]=1,a.a[1][1]=x*x%mod;
a.a[1][2]=y*y%mod;
a.a[1][3]=2*x*y%mod,a.a[2][1]=1;
a.a[3][1]=x,a.a[3][3]=y;
a=qmod(a,m);
int ans=0;
for(int i=0;i<4;i++)
ans+=a.a[0][i];
ans%=mod;
printf("%d\n",ans);
}
}

EOF