FZU 1683 纪念SlingShot (构造矩阵)

题目链接:http://acm.fzu.edu.cn/problem.php?pid=1683

题意:f(0)=1,f(1)=3,f(2)=5,f(n)=3f(n-1)+2f(n-2)+5f(n-3)。求s(n)=(f(0)+f(1)……+f(n))%2009。

思路:S(n)=S(n-1)+F(n)=S(n-1)+3F(n-1)+2F(n-2)+7F(n-3),因此构造矩阵:

 

#include <iostream>

#include <cstdio>

#include <string.h>

#include <algorithm>

#include <cmath>

#include <vector>

#include <queue>

#include <set>

#include <string>



#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("%lld\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;}



const int mod=2009;

const int INF=2000000000;

const int N=4;

int C,num=0;

int n;



class Matrix

{

public:

    int a[N][N];

    int n;



    void init(int x)

    {

        int i;

        if(!x) clr(a,0);

        else

        {

            clr(a,0);

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

        }

    }



    void read(int n)

    {

        int i,j;

        FOR1(i,n) FOR1(j,n)

        {

            RD(a[i][j]);

            a[i][j]%=mod;

        }

    }



    Matrix operator+(Matrix b)

    {

        int i,j;

        Matrix c;

        FOR0(i,n) FOR0(j,n) c.a[i][j]=(a[i][j]+b.a[i][j])%mod;

        return c;

    }



    Matrix operator+(int x)

    {

        int i;

        Matrix c=*this;

        FOR0(i,n) c.a[i][i]+=x;

        return c;

    }



    Matrix operator*(Matrix b)

    {

        int i,j,k;

        Matrix p;

        p.n=4;

        p.init(0);

        FOR0(i,n) FOR0(j,n) FOR0(k,n)

        {

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

        }

        return p;

    }



    Matrix power(int t)

    {

        Matrix ans,p=*this;

        ans.n=4;

        ans.init(1);

        while(t)

        {

            if(t&1) ans=ans*p;

            p=p*p;

            t>>=1;

        }

        return ans;

    }



    void print(int n)

    {

        int i,j;

        FOR0(i,n)

        {

            FOR0(j,n) printf("%d ",a[i][j]);

            puts("");

        }

    }

};



Matrix a;





int main()

{

    RD(C);

    while(C--)

    {

        scanf("%d",&n);

        printf("Case %d: ",++num);

        if(n<=2)

        {

            PR(sqr(n+1));

            continue;

        }

        a.n=4;

        a.init(0);

        a.a[0][0]=1;a.a[0][1]=3;a.a[0][2]=2;a.a[0][3]=7;

        a.a[1][1]=3;a.a[1][2]=2;a.a[1][3]=7;

        a.a[2][1]=1;a.a[3][2]=1;

        a=a.power(n-2);

        int ans=(a.a[0][0]*9+a.a[0][1]*5+a.a[0][2]*3+a.a[0][3])%mod;

        PR(ans);

    }

    return 0;

}

  

你可能感兴趣的:(in)