HDU-6470,HDU-1575, HDU-1757(矩阵快速幂)

题目链接 HDU-6470

思路

根据题意有 f[n] = f[n-1] + 2f[n-2] +n^3,构造转移矩阵。

#include 
#include 
#include 
#include 
using namespace std;
typedef long long ll;
const ll mod = 123456789;
struct Matrix{
    ll m[7][7];
}A,B;
Matrix mul(Matrix A,Matrix B)
{
    int i,j,k;
    Matrix tmp;
    memset(tmp.m,0,sizeof(tmp.m));
    for(i = 1; i <= 6; i++)
        for(j = 1; j <= 6 ; j++)
          for(k = 1; k <= 6 ; k++)
    {
        tmp.m[i][j] = (tmp.m[i][j] + A.m[i][k] * B.m[k][j])% mod;
        tmp.m[i][j] %= mod;
    }
    return tmp;
}
Matrix pos(Matrix A,ll N)
{
    int i;
    Matrix res;
    memset(res.m,0,sizeof(res.m));
    for(i = 1; i <= 6 ; i++)
        res.m[i][i] = 1;
    while(N)
    {
        if(N & 1)
            res = mul(res,A);
        A = mul(A,A);
        N >>= 1;
    }
    return res;
}
void init()
{
    memset(B.m,0,sizeof(B.m));
    B.m[1][1] = 2;
    B.m[2][1] = 1;
    B.m[3][1] = 8;
    B.m[4][1] = 4;
    B.m[5][1] = 2;
    B.m[6][1] = 1;
    A.m[1][1] = 1;A.m[1][2] = 2;A.m[1][3] = 1;A.m[1][4] = 3;A.m[1][5] = 3;A.m[1][6] = 1;
    A.m[2][1] = 1;A.m[2][2] = 0;A.m[2][3] = 0;A.m[2][4] = 0;A.m[2][5] = 0;A.m[2][6] = 0;
    A.m[3][1] = 0;A.m[3][2] = 0;A.m[3][3] = 1;A.m[3][4] = 3;A.m[3][5] = 3;A.m[3][6] = 1;
    A.m[4][1] = 0;A.m[4][2] = 0;A.m[4][3] = 0;A.m[4][4] = 1;A.m[4][5] = 2;A.m[4][6] = 1;
    A.m[5][1] = 0;A.m[5][2] = 0;A.m[5][3] = 0;A.m[5][4] = 0;A.m[5][5] = 1;A.m[5][6] = 1;
    A.m[6][1] = 0;A.m[6][2] = 0;A.m[6][3] = 0;A.m[6][4] = 0;A.m[6][5] = 0;A.m[6][6] = 1;
}
int main()
{
    Matrix res1;
    init();
    ll N;
    int T;
    cin >> T;
    while(T--)
    {
        cin >> N;
        res1 = pos(A,N-2);
        res1 = mul(res1,B);
        cout << res1.m[1][1] << endl;
    }
}

题目链接: HDU-1575

思路

简单的矩阵快速幂,先求出来A^k,再加和取余。

#include 
#include 
#include 
#include 
using namespace std;
typedef long long ll;
const ll mod = 9973;
struct Matrix{
    ll m[11][11];
}A,B;
Matrix mul(Matrix A,Matrix B,int n)
{
    int i,j,k;
    Matrix tmp;
    memset(tmp.m,0,sizeof(tmp.m));
    for(i = 1; i <= n ; i++ )
        for(j = 1; j <= n ; j++)
        for(k = 1; k <= n ; k++)
    {
        tmp.m[i][j] = (tmp.m[i][j] + A.m[i][k] * B.m[k][j])%mod;
        tmp.m[i][j] %= mod;
    }
    return tmp;
}
Matrix pow(Matrix A,int N,int n)
{
    int i ;
    Matrix res;
    memset(res.m,0,sizeof(res.m));
    for(i = 1; i <= n ; i++)
        res.m[i][i] = 1;
    while(N)
    {
        if(N & 1)
           res = mul(res,A,n);
        A = mul(A,A,n);
        N >>= 1;
    }
    return res;
}
int main()
{
    int t,n,i,j,k,sum;
    Matrix res1;
    cin >> t;
    while(t--)
    {
       cin >> n >> k ;
       for(i = 1; i <= n ; i++)
       {
           for(j = 1;j <= n ; j++)
            cin >> A.m[i][j];
       }
       sum = 0;
       res1 = pow(A,k,n);
       for(i = 1; i <= n ; i++)
       {
           sum = (sum + res1.m[i][i]) % mod;
       }
       cout << sum%mod << endl;
    }
}

题目链接: HDU-1757

思路

代码即思路。

#include 
#include 
#include 
#include 
using namespace std;
typedef long long ll;
struct Matrix{
    ll m[11][11];
}A;
int mod;
int a[11];
Matrix mul(Matrix A,Matrix B)
{
    int i,j,k;
    Matrix tmp;
    memset(tmp.m,0,sizeof(tmp.m));
    for(i = 1; i <= 10 ; i++ )
      for(j = 1; j <= 10 ; j++)
        for(k = 1; k <= 10 ; k++)
    {
        tmp.m[i][j] = (tmp.m[i][j] + A.m[i][k] * B.m[k][j])%mod;
        tmp.m[i][j] %= mod;
    }
    return tmp;
}
Matrix pow(Matrix A,int N)
{
    int i ;
    Matrix res;
    memset(res.m,0,sizeof(res.m));
    for(i = 1; i <= 10; i++)
        res.m[i][i] = 1;
    while(N)
    {
        if(N & 1)
           res = mul(res,A);
        A = mul(A,A);
        N >>= 1;
    }
    return res;
}
void init()
{
    int i;
    memset(A.m,0,sizeof(A.m));
    for(i = 1; i <= 10 ; i++)
    {
        A.m[1][i] = a[i];
    }
    for(i = 2; i <= 10 ; i++)
        A.m[i][i-1] = 1;
}
int main()
{
    int t,n,i,j,k,sum;
    Matrix res1,B;
    B.m[1][1] = 9;
    B.m[2][1] = 8;
    B.m[3][1] = 7;
    B.m[4][1] = 6;
    B.m[5][1] = 5;
    B.m[6][1] = 4;
    B.m[7][1] = 3;
    B.m[8][1] = 2;
    B.m[9][1] = 1;
    B.m[10][1] = 0;
    while(cin >> n >> mod)
    {
       for(i = 1; i <= 10; i++)
       {
            cin >> a[i];
       }
       init();
       if(n < 10)
        cout << n % mod << endl;
       else{
         res1 = pow(A,n - 9);
         res1 = mul(res1,B);
         cout << res1.m[1][1] << endl;
    }
    }
}

你可能感兴趣的:(矩阵快速幂)