[Explanations] P4838 P brother to crack passwords

P4838 P brother to crack passwords

Moment by recursive optimization (hint: N <= 10e9, a linear, no law)
F [I] [J] (I ∈ [. 1, n-], J ∈ [0,2]), represents n == i when, at the end there is a number j of scheme a number.

Because there is the possibility of the end of A depends only on the previous state in a state (A 1 A 0 and about, A 2 and A 1 related).
And at the end there are cases 0 AA, as long as a B plug on the line. So is the number of all programs on state and a state.

initialization:f[1][0]=1,f[1][1]=1,f[1][2]=0;

Transfer equation:

f[i][0] = f[i - 1][2] + f[i - 1][1] + f[i - 1][0]

f[i][1] = f[i - 1][0]

f[i][2] = f[i - 1][1]

Hereinafter the coordinate + 1

//1 1 0
/* 
1 1 0
1 0 1 
1 0 0
*/ 
#define mod 19260817

struct Matrix{
    int m[4][4];
    Matrix(){mem(m,0);}
    friend Matrix operator *(Matrix a,Matrix b){
        Matrix c;
        rep(i,1,3)
            rep(j,1,3)
                rep(k,1,3)
                    c.m[i][j]=(c.m[i][j]+a.m[i][k]*b.m[k][j])%mod;
        return c;
    }
    friend Matrix operator ^(Matrix a,int k){
        Matrix res;
        rep(i,1,3)res.m[i][i]=1;
        for(;k;k>>=1){
            if(k&1)res=res*a;
            a=a*a; 
        }
        return res;
    }
}; 
    int T;rd(T);
    while(T--){
        int n;rd(n);
        Matrix A;
        A.m[1][1]=1;A.m[1][2]=1;
        Matrix base;
        base.m[1][1]=1,base.m[1][2]=1,base.m[1][3]=0;
        base.m[2][1]=1,base.m[2][2]=0,base.m[2][3]=1;
        base.m[3][1]=1,base.m[3][2]=0,base.m[3][3]=0;
        base=base^(n-1);
        A=A*base;
        printf("%lld\n",(A.m[1][1]+A.m[1][2]+A.m[1][3])%mod);
    }

Guess you like

Origin www.cnblogs.com/sjsjsj-minus-Si/p/11634749.html