1. 程式人生 > >HDU6185 Covering(矩陣快速冪)

HDU6185 Covering(矩陣快速冪)

/*
遞推公式+矩陣快速冪
a(n)=a(n-1)+5*a(n-2)+a(n-3)-a(n-4)
*/
#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
const int maxn=1e5+5;
const int mod=1e9+7;
LL n;

struct Matrix
{
    LL mat[4
][4]; Matrix()//注意初始化 { memset(mat,0,sizeof(mat)); } Matrix operator -(Matrix &tmp) { Matrix res; for(int i=0; i<4; i++) for(int j=0; j<4; j++) { res.mat[i][j]=mat[i][j]-tmp.mat[i][j]+mod; res.mat[i][j]%=mod; } return
res; } Matrix operator *(Matrix &tmp) { Matrix res; memset(res.mat,0,sizeof(res.mat)); for(int i=0; i<4; ++i) for(int j=0; j<4; ++j) for(int k=0; k<4; ++k) { res.mat[i][j]+=mat[i][k]*tmp.mat[k][j]; res.mat[i][j]%=mod; } return
res; } }; Matrix pow_M(Matrix a,LL n) { Matrix ret; memset(ret.mat,0,sizeof(ret.mat)); ret.mat[0][0]=ret.mat[1][1]=1; Matrix temp=a; while(n) { if(n&1)ret=ret*temp; temp=temp*temp; n>>=1; } return ret; } LL pow_m(LL a,LL n) { LL ret=1; LL temp=a%mod; while(n) { if(n&1) { ret*=temp; ret%=mod; } temp*=temp; temp%=mod; n>>=1; } return ret; } int main() { while(~scanf("%I64d",&n)) { Matrix A,B,C; //A為係數矩陣,B為基數矩陣 A.mat[0][0]=1; A.mat[0][1]=5; A.mat[0][2]=1; A.mat[0][3]=-1; A.mat[1][0]=1; A.mat[2][1]=1; A.mat[3][2]=1; B.mat[0][0]=36; B.mat[1][0]=11; B.mat[2][0]=5; B.mat[3][0]=1; if(n==1) { printf("1\n"); continue; } if(n==2) { printf("5\n"); continue; } if(n==3) { printf("11\n"); continue; } if(n==4) { printf("36\n"); continue; } C=pow_M(A,n-4)*B; printf("%I64d\n",(C.mat[0][0]+mod)%mod); } return 0; }
/*
狀態壓縮dp+矩陣快速冪
*/

#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
const int maxn=1e5+5;
const int mod=1e9+7;
LL n;

struct Matrix
{
    LL mat[6][6];
    Matrix(){memset(mat,0,sizeof(mat));}//注意初始化
    Matrix operator -(Matrix &tmp)
    {
        Matrix res;
        for(int i=0; i<6; i++)
            for(int j=0; j<6; j++)
            {
                res.mat[i][j]=mat[i][j]-tmp.mat[i][j]+mod;
                res.mat[i][j]%=mod;
            }
        return res;
    }
    Matrix operator *(Matrix &tmp)
    {
        Matrix res;
        memset(res.mat,0,sizeof(res.mat));
        for(int i=0; i<6; ++i)
            for(int j=0; j<6; ++j)
                for(int k=0; k<6; ++k)
                {
                    res.mat[i][j]+=mat[i][k]*tmp.mat[k][j];
                    res.mat[i][j]%=mod;
                }
        return res;
    }
};

Matrix pow_M(Matrix a,LL n)
{
    Matrix ret;
    memset(ret.mat,0,sizeof(ret.mat));
    ret.mat[0][0]=ret.mat[1][1]=1;
    Matrix temp=a;
    while(n)
    {
        if(n&1)ret=ret*temp;
        temp=temp*temp;
        n>>=1;
    }
    return ret;
}
int main()
{
    while(~scanf("%I64d",&n))
    {
       Matrix A,B;
      /*
      狀態可達矩陣,根據某一行狀態轉移到下一行時,可達的狀態
      每一行分成4個部分,已填用1表示,未填用0表示
     下一行     0000  0011  0110  1001  1100  1111
     某一行       0     1     2     3    4     5
      0000   0    1     1           1    1     1
      0011   1    1                      1
      0110   2                      1
      1001   3    1           1
      1100   4    1     1
      1111   5    1
      某一行與下一行結合後符合實際填補的情況
      */
      //初始化狀態可達矩陣
       A.mat[0][0]=1;
       A.mat[0][1]=1;
       A.mat[0][3]=1;
       A.mat[0][4]=1;
       A.mat[0][5]=1;

       A.mat[1][0]=1;
       A.mat[1][4]=1;

       A.mat[2][3]=1;

       A.mat[3][0]=1;
       A.mat[3][2]=1;

       A.mat[4][0]=1;
       A.mat[4][1]=1;

       A.mat[5][0]=1;

       B=pow_M(A,n);
       //恰好填滿時,從狀態0000->0000
       printf("%I64d\n",(B.mat[0][0])%mod);
    }
    return 0;
}