HDU - 6185 Covering (高斯消元 矩阵快速幂)

Covering

Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1774    Accepted Submission(s): 677


Problem Description
Bob's school has a big playground, boys and girls always play games here after school.

To protect boys and girls from getting hurt when playing happily on the playground, rich boy Bob decided to cover the playground using his carpets.

Meanwhile, Bob is a mean boy, so he acquired that his carpets can not overlap one cell twice or more.

He has infinite carpets with sizes of 1×2 and 2×1, and the size of the playground is 4×n.

Can you tell Bob the total number of schemes where the carpets can cover the playground completely without overlapping?
 

Input
There are no more than 5000 test cases.

Each test case only contains one positive integer n in a line.

1n1018
 

Output
For each test cases, output the answer mod 1000000007 in a line.
 

Sample Input
 
  
1 2
 

Sample Output
 
  
1 5
Source

题意:在4*n的地板上铺1*2 或 2*1的瓷砖,在不重叠的前提下,问共有多少种铺满的方式?

思路:n的范围很大很大,所以肯定是个规律题,用dfs把前几组的答案找出来,1,5,11,36,95,281,2245,6336,18063,只凭肉眼观察很难找出规律来,猜测是线性递推方程,假设 f(n)=af(n-1) +bf(n-2),利用高斯消元发现结果不是整数,高斯消元实际上就是线性代数上学的求解线性方程组,当我们试到f(n)=af(n-1) +bf(n-2) + cf(n-3)+df(n-4)时发现存在正数解,之后就简单了,利用矩阵快速幂求解。

高斯消元,网上找得模板,有了这个,给你任意一组数,都可以出线性递推关系了,前提是存在这个关系

//f(n)=f(n-1)*a+f(n-2)*b+f(n-3)*c+f(n-4)*d情况下的高斯消元
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std;
const int maxn=105;
typedef double Matrix[maxn][maxn];
Matrix A,S;
//n是方程的个数
void gauss(Matrix A,int n)
{
    int i,j,k,r;
    for(int i=0; i<n; i++)
    {
        r=i;
        for( j=i+1; j<n; j++)
            if(fabs(A[j][i])>fabs(A[r][i]))r=j;
        if(r!=i)
            for(j=0; j<=n; j++)swap(A[r][j],A[i][j]);
        for(k=i+1; k<n; k++)
        {
            double f=A[k][i]/A[i][i];
            for(j=i; j<=n; j++)
                A[k][j]-=f*A[i][j];
        }
    }
    for(i=n-1; i>=0; i--)
    {
        for(j=i+1; j<n; j++)
            A[i][n]-=A[j][n]*A[i][j];
        A[i][n]/=A[i][i];
    }
}
int main()
{

    memset(A,0,sizeof(A));
    A[0][0]=95,A[0][1]=36,A[0][2]=11,A[0][3]=5,A[1][0]=6336,A[1][1]=2245,A[1][2]=781;
    A[1][3]=281,A[2][0]=781,A[2][1]=281,A[2][2]=95,A[2][3]=36,A[3][0]=2245,A[3][1]=781;
    A[3][2]=281,A[3][3]=95,A[0][4]=281,A[1][4]=18061,A[2][4]=2245,A[3][4]=6336;
    gauss(A,4);
    for(int i=0; i<4; i++)
    {
        printf("%8.2f\n",A[i][4]);
    }
    return 0;
}                
#include <stdio.h>
#include <string.h>


const int MOD = 1000000007;
typedef long long ll;
struct Matrix
{
    ll A[4][4];
};
Matrix mul(Matrix A,Matrix B)
{
    Matrix C;
    memset(C.A,0,sizeof(C.A));
    for(int i = 0; i < 4; i++) {
        for(int j = 0; j < 4; j++) {
            for(int k = 0; k < 4; k++) {
                C.A[i][j] = (C.A[i][j] % MOD + A.A[i][k] * B.A[k][j] + MOD) % MOD;
             }
        }
    }
    return C;
}
Matrix q_pow(Matrix A,ll n)
{
    Matrix ans;
    memset(ans.A,0,sizeof(ans.A));
    for(int i = 0; i < 4; i++) {
        ans.A[i][i] = 1;
    }
    while(n) {
        if(n & 1) {
            ans = mul(ans,A);
        }
        A = mul(A , A);
        n >>= 1;
    }
    return ans;
}
int main(void)
{
    Matrix base,B;
    base.A[0][0] = 1,base.A[0][1] = 5,base.A[0][2] = 1,base.A[0][3] = -1;
    base.A[1][0] = 1,base.A[1][1] = 0,base.A[1][2] = 0,base.A[1][3] = 0;
    base.A[2][0] = 0,base.A[2][1] = 1,base.A[2][2] = 0,base.A[2][3] = 0;
    base.A[3][0] = 0,base.A[3][1] = 0,base.A[3][2] = 1,base.A[3][3] = 0;
    B.A[0][0] = 36,B.A[1][0] = 11,B.A[2][0] = 5,B.A[3][0] = 1;

    ll n;
    while(scanf("%I64d",&n) != EOF) {


        if(n == 1) printf("1\n");
        else if(n == 2) printf("5\n");
        else if(n == 3) printf("11\n");
        else if(n == 4) printf("36\n");
        else {
            Matrix ans = q_pow(base,n - 4);
            ans = mul(ans,B);
            printf("%I64d\n",(ans.A[0][0] + MOD) % MOD);
        }
    }
}


猜你喜欢

转载自blog.csdn.net/gyh0730/article/details/81047322