hdu3306

链接:点击打开链接

题意: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;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值