HDU 2971 Tower(构造矩阵)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2971

题意:a(1)=1,a(2)给定,a(n)=2*a(2)*a(n-1)-a(n-2)。求s(n)=a(1)^2+a(2)^2+……+a(n)^2。

思路:s(n)=s(n-1)+a(n)^2=s(n-1)+4a(2)^2*a(n-1)^2+a(n-2)^2-4a(2)*a(n-1)*a(n-2)。由此建立矩阵。

#include <iostream>

#include <cstdio>

#include <string.h>

#include <algorithm>

#include <cmath>

#include <vector>

#include <queue>

#include <set>

#include <string>

#include <map>



#define max(x,y) ((x)>(y)?(x):(y))

#define min(x,y) ((x)<(y)?(x):(y))

#define abs(x) ((x)>=0?(x):-(x))

#define i64 long long

#define u32 unsigned int

#define u64 unsigned long long

#define clr(x,y) memset(x,y,sizeof(x))

#define CLR(x) x.clear()

#define ph(x) push(x)

#define pb(x) push_back(x)

#define Len(x) x.length()

#define SZ(x) x.size()

#define PI acos(-1.0)

#define sqr(x) ((x)*(x))



#define FOR0(i,x) for(i=0;i<x;i++)

#define FOR1(i,x) for(i=1;i<=x;i++)

#define FOR(i,a,b) for(i=a;i<=b;i++)

#define DOW0(i,x) for(i=x;i>=0;i--)

#define DOW1(i,x) for(i=x;i>=1;i--)

#define DOW(i,a,b) for(i=a;i>=b;i--)

using namespace std;





void RD(int &x){scanf("%d",&x);}

void RD(i64 &x){scanf("%lld",&x);}

void RD(u64 &x){scanf("%llu",&x);}

void RD(u32 &x){scanf("%u",&x);}

void RD(double &x){scanf("%lf",&x);}

void RD(int &x,int &y){scanf("%d%d",&x,&y);}

void RD(i64 &x,i64 &y){scanf("%lld%lld",&x,&y);}

void RD(u64 &x,u64 &y){scanf("%llu%llu",&x,&y);}

void RD(u32 &x,u32 &y){scanf("%u%u",&x,&y);}

void RD(double &x,double &y){scanf("%lf%lf",&x,&y);}

void RD(int &x,int &y,int &z){scanf("%d%d%d",&x,&y,&z);}

void RD(i64 &x,i64 &y,i64 &z){scanf("%lld%lld%lld",&x,&y,&z);}

void RD(u64 &x,u64 &y,u64 &z){scanf("%llu%llu%llu",&x,&y,&z);}

void RD(u32 &x,u32 &y,u32 &z){scanf("%u%u%u",&x,&y,&z);}

void RD(double &x,double &y,double &z){scanf("%lf%lf%lf",&x,&y,&z);}

void RD(char &x){x=getchar();}

void RD(char *s){scanf("%s",s);}

void RD(string &s){cin>>s;}



void PR(int x) {printf("%d\n",x);}

void PR(i64 x) {printf("%I64d\n",x);}

void PR(u64 x) {printf("%llu\n",x);}

void PR(u32 x) {printf("%u\n",x);}

void PR(double x) {printf("%lf\n",x);}

void PR(char x) {printf("%c\n",x);}

void PR(char *x) {printf("%s\n",x);}

void PR(string x) {cout<<x<<endl;}



int mod;

const int N=4;

int n;

i64 a[N][N];





void cal(i64 a[][N],i64 b[][N])

{

    int i,j,k;

    i64 c[N][N];

    FOR0(i,n) FOR0(j,n) c[i][j]=a[i][j],a[i][j]=0;

    FOR0(i,n) FOR0(k,n) if(c[i][k]) FOR0(j,n) if(b[k][j])

    {

        (a[i][j]+=c[i][k]*b[k][j]%mod)%=mod;

    }

}



void cal(i64 a[][N])

{

    int i,j;

    i64 d[N][N];

    FOR0(i,n) FOR0(j,n) d[i][j]=a[i][j];

    cal(a,d);

}



void power(i64 a[][N],int m)

{

    int i,j;

    i64 b[N][N];

    clr(b,0);

    FOR0(i,n) b[i][i]=1;

    while(m)

    {

        if(m&1) cal(b,a);

        cal(a);

        m>>=1;

    }

    FOR0(i,n) FOR0(j,n) a[i][j]=b[i][j];

}



int a2,m;

int C;



int main()

{

    RD(C);

    while(C--)

    {

        RD(a2,m,mod);

        n=4;

        i64 ans;

        if(m==1) puts("1");

        else if(m==2) printf("%I64d\n",(1+(i64)a2*a2)%mod);

        else

        {

            clr(a,0);

            a[0][0]=a[0][2]=a[1][2]=a[2][1]=1;

            a[3][3]=mod-1;

            a[0][1]=a[1][1]=4*(i64)a2%mod*a2%mod;

            a[0][3]=a[1][3]=mod-4*(i64)a2%mod;

            a[3][1]=2*a2%mod;

            power(a,m-2);

            ans=0;

            (ans+=a[0][0]*(1+(i64)a2*a2%mod)%mod)%=mod;

            (ans+=a[0][1]*a2%mod*a2%mod)%=mod;

            (ans+=a[0][2]+a[0][3]*a2%mod)%=mod;

            printf("%I64d\n",ans);

        }

    }

    return 0;

}

  

你可能感兴趣的:(HDU)