链接:点击打开链接
题意:A(0)=1, A(1)=1, A(N)=X*A(N-1)+Y*A(N-2)(N>=2)求S(N),S(N)=A(0)2+A(1)2+……+A(n)2.
代码:
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
#define mod 10007
struct node{
int m[4][4];
};
node P={1,1,1,1, //S(N)=S(N-1)+A²(N)
0,1,1,1, //A²(N)=[xA(N-1)+yA(N-2)]²
0,1,0,0, // =x²A²(N-1)+y²A²(N-2)+2xyA(n-1)A(n-2)
0,1,0,1}; //从而推出矩阵,矩阵的选择根据每个人所选取的项不同
node I={1,0,0,0, //从而有不同的矩阵,因此矩阵并不唯一
0,1,0,0,
0,0,1,0,
0,0,0,1};
node mul(node a,node b){
node c;
int i,j,k;
for(i=0;i<4;i++)
for(j=0;j<4;j++){
c.m[i][j]=0;
for(k=0;k<4;k++)
c.m[i][j]+=(a.m[i][k]*b.m[k][j])%mod;
c.m[i][j]%=mod;
}
return c;
} //矩阵乘法
node quickmod(int n){
node a,b;
a=P;b=I;
while(n){
if(n&1)
b=mul(b,a);
n>>=1;
a=mul(a,a);
}
return b;
} //矩阵快速幂
int main(){
int n,x,y,sum;
node temp;
while(scanf("%d%d%d",&n,&x,&y)!=EOF){
x%=mod;y%=mod; //x,y必须取模,x*x和y*y也是,否则会溢出
P.m[0][1]=P.m[1][1]=x*x%mod;
P.m[0][2]=P.m[1][2]=y*y%mod;
P.m[0][3]=P.m[1][3]=2*x*y%mod;
P.m[3][1]=x;P.m[3][3]=y;
temp=quickmod(n-1);
sum=temp.m[0][0]*2+temp.m[0][1]+temp.m[0][2]+temp.m[0][3];
sum%=mod; //矩阵第一行的和就是结果
printf("%d\n",sum);
}
return 0;
}