Covering HDU - 6185 (找递推公式+矩阵快速幂)

本文探讨了HDU-6185题目中的地毯覆盖问题,即如何用1×2和2×1的地毯完全覆盖4×n的操场而不重叠。通过深度优先搜索(DFS)寻找规律,并利用矩阵快速幂解决大数问题,给出了详细的解决方案及代码实现。

Covering

HDU - 6185

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

摘自博客点击打开链接

首先可以通过dfs打表找规律

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
bool book[7][1007];
int cnt;
int n;
bool findpos(int &x,int &y){
    for(int i = 1; i <= 4; i++){
        for(int j = 1; j <= n; j++){
            if(!book[i][j]){
                x = i,y = j;
                return false;
            }
        }
    }
    return true;
}
void dfs(int x,int y){
    if(!book[x+1][y] && x < 4){
        book[x][y] = book[x+1][y] = true;
        int newx,newy;
        if(findpos(newx,newy)){
            book[x][y] = book[x+1][y] = false;
            cnt++;
            return;
        }
        dfs(newx,newy);
        book[x][y] = book[x+1][y] = false;
    }
    if(!book[x][y+1] && y < n){
        book[x][y] = book[x][y+1] = true;
        int newx,newy;
        if(findpos(newx,newy)){
            book[x][y] = book[x][y+1] = false;
            cnt++;
            return;
        }
        dfs(newx,newy);
        book[x][y] = book[x][y+1] = false;
    }
}
int main(){
    while(cin >> n){
        cnt = 0;
        memset(book,false,sizeof(book));
        dfs(1,1);
        cout << cnt << endl;
    }
    return 0;
}

得到了答案:1 ,5 ,11 ,36 ,95 ,281,781,2245,6336,18061

然后觉得有一个边长是常数4,就写了五层for循环确定ans[i]与前四项和一个常数之间的系数关系,得到:


(这个递推式我没想明白为什么会和前四项有关,可能是试出来的?)

,然后想到矩阵快速幂,容易得到:


  需要注意的一点是,因为有一个系数是1

,所以在矩阵乘法的过程中,在两两相加的时候要再加上一个mod之后再取模,不然的话有可能出现一个小的正数加上一个大的负数的情况。

然后因为需要前四项,所有可以从第五项开始算,前四项直接输出,但是博客中构造了一个神奇的矩阵

1

0

1

1

这个列向量,可以直接从第一项开始算

code:

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
typedef long long ll;
const int maxn = 4;
const int mod = 1e9+7;
ll n;
struct Matrix{
    ll m[maxn][maxn];
    Matrix(ll i = 0){
        memset(m,0,sizeof(m));
        if(i == 1)
            for(int j = 0; j < 4; j++)
                m[j][j] = 1;
    }
    Matrix operator * (const Matrix tmp)const{
        Matrix ret;
        ll x;
        for(int i = 0; i < 4; i++){
            for(int j = 0; j < 4; j++){
                x = 0;
                for(int k = 0; k < 4; k++){
                    x += (m[i][k] * tmp.m[k][j] + mod) % mod;
                }
                ret.m[i][j] = x % mod;
            }
        }
        return ret;
    }
    Matrix q_pow(ll n){
        Matrix ret = 1,tmp = *this;
        while(n){
            if(n & 1)
                ret = ret * tmp;
            tmp = tmp * tmp;
            n >>= 1;
        }
        return ret;
    }
};
int main(){
    Matrix base1 = 0,base2 = 0;
    base1.m[0][0] = base1.m[0][2] = base1.m[1][0] = base1.m[2][1] = base1.m[3][2] = 1;
    base1.m[0][3] = -1,base1.m[0][1] = 5;
    base2.m[0][0] = 1;
    base2.m[1][0] = 0;
    base2.m[2][0] = 1;
    base2.m[3][0] = 1;
    while(~scanf("%lld",&n)){
        Matrix ans = base1.q_pow(n)*base2;
        printf("%lld\n",ans.m[0][0]);
    }
    return 0;
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值