由于棋盘只有两行,所以如果第i列的骨牌竖着放,那么就转移为第1列到第i-1列骨牌有多少种摆法

如果第一行第i列骨牌横着放,那么第二行第i列也要横着放,那么就转移为了第1列到第i-2列骨牌有多少种方法

dp[i] = dp[i-1] + dp[i-2],但是列数太多了。 这种递推的算式可以用矩阵快速幂来优化

所以时间复杂度瞬间变为O(logn)

 #include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <iostream>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <math.h>
using namespace std;
typedef long long LL;
const int INF = <<;
LL ans;
const int MOD = ;
//矩阵快速幂 a[i] = a[i-1] + a[i-2] struct Matrix
{
LL m[][];
};
Matrix operator*(const Matrix &lhs, const Matrix &rhs)
{
Matrix ret;
for(int i=; i<; ++i)
for(int j=; j<; ++j)
ret.m[i][j] = ;
for(int i=; i<; ++i)
for(int j=; j<; ++j)
for(int k=; k<; ++k)
if(lhs.m[i][k]!= && rhs.m[k][j]!=)
ret.m[i][j] = (ret.m[i][j] + lhs.m[i][k] * rhs.m[k][j])%MOD; return ret;
}
Matrix operator^(Matrix a, int k)
{
Matrix ret;
for(int i=; i<; ++i)
for(int j=; j<; ++j)
ret.m[i][j] = ;
ret.m[][] = ;
while(k)
{
if(k&)
ret = ret * a;
k>>=;
a = a * a;
}
return ret;
} int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
Matrix tmp;
for(int i=; i<; ++i)
for(int j=; j<; ++j)
tmp.m[i][j] = ;
tmp.m[][] = ;
Matrix final = tmp ^ (n-);
LL ans = ( * final.m[][] + * final.m[][])%MOD;
printf("%lld\n",ans);
}
return ;
}

hihocoder第41周 骨牌覆盖(矩阵快速幂)的更多相关文章

  1. hihoCoder#1743:K-偏差排列(矩阵快速幂+状压dp)

    题意 如果一个 \(1\to N\) 的排列 \(P=[P_1, P_2, ... P_N]\) 中的任意元素 \(P_i\) 都满足 \(|P_i-i| ≤ K\) ,我们就称 \(P\) 是 \( ...

  2. poj 3420 Quad Tiling (状压dp+多米诺骨牌问题+矩阵快速幂)

    还有这种操作?????? 直接用pre到now转移的方式构造一个矩阵就好了. 二进制长度为m,就构造一个长度为1 << m的矩阵 最后输出ans[(1 << m) - 1][( ...

  3. hihocoder第42周 3*N骨牌覆盖(状态dp+矩阵快速幂)

    http://hihocoder.com/contest/hiho42/problem/1 给定一个n,问我们3*n的矩阵有多少种覆盖的方法 第41周做的骨牌覆盖是2*n的,状态转移方程是dp[i] ...

  4. hihoCoder 1143 : 骨牌覆盖问题·一(递推,矩阵快速幂)

    [题目链接]:click here~~ 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 骨牌,一种古老的玩具.今天我们要研究的是骨牌的覆盖问题: 我们有一个2xN的长条形 ...

  5. hihoCoder #1151 : 骨牌覆盖问题·二 (矩阵快速幂,DP)

    题意:给一个3*n的矩阵,要求用1*2的骨牌来填满,有多少种方案? 思路: 官网题解用的仍然是矩阵快速幂的方式.复杂度O(logn*83). 这样做需要构造一个23*23的矩阵,这个矩阵自乘n-1次, ...

  6. (中等) CF 576D Flights for Regular Customers (#319 Div1 D题),矩阵快速幂。

    In the country there are exactly n cities numbered with positive integers from 1 to n. In each city ...

  7. HDU 6185 Covering 矩阵快速幂

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6185 题意:用 1 * 2 的小长方形完全覆盖 4 * n的矩形有多少方案. 解法:小范围是一个经典题 ...

  8. 【BZOJ5505】[GXOI/GZOI2019]逼死强迫症(矩阵快速幂)

    [BZOJ5505][GXOI/GZOI2019]逼死强迫症(矩阵快速幂) 题面 BZOJ 洛谷 题解 如果没有那两个\(1*1\)的东西,答案就是斐波那契数,可以简单的用\(dp\)得到. 大概是设 ...

  9. Luogu P3390 【模板】矩阵快速幂&&P1939 【模板】矩阵加速(数列)

    补一补之前的坑 因为上次关于矩阵的那篇blog写的内容太多太宽泛了,所以这次把一些板子和基本思路理一理 先看这道模板题:P3390 [模板]矩阵快速幂 首先我们知道矩阵乘法满足结合律而不满足交换律的一 ...

随机推荐

  1. eclipse weblogic debug 简易配置版

    1. eclipse->help->marketplace->search->weblogic 1.1安装对应eclipse版本的weblogic plugin 查看eclip ...

  2. javascript (二) 事件

    <script></script> 函数写法: function  fun_name(){ x=docment.getElementById("demo") ...

  3. 呜呼!Node.js是什么?

    近期看到非常多站点都使用node.js.開始感到非常好奇.就開始推測这是个什么东西,大概就是个js文件吧,所以開始根本 没有在意,可是越感觉就认为越不正确劲,为什么大家都在用它呢?所以我决定搞个明确. ...

  4. Qt 4.7.4 完美动态编译发布动态调试,以及静态编译发布

    首先是准备工作,去QT主页下载独立的QT类库安装包以及完整QT SDK安装包,还有QT Creator for windows 版 下载地址:http://qt.nokia.com/downloads ...

  5. Virtualbox mouse move in and out and file share with windows

    How to use Virstalbox to share files with Linux and Windows, and to move the mouse in and out Virtua ...

  6. 高效合并两个有序数组(Merge Sorted Array)

    Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: Y ...

  7. 基于Andoird 4.2.2的Account Manager源代码分析学习:创建选定类型的系统帐号

    AccountManager.addAccount() public AccountManagerFuture<Bundle> addAccount(final String accoun ...

  8. Android 自己定义View (二) 进阶

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/24300125 继续自己定义View之旅.前面已经介绍过一个自己定义View的基础 ...

  9. JavaScript 使用Document记录cookie

    cookie对于我们使用者来说,有时帮助还是挺大的,比方对于一些不是特别重要的站点,比方公司的測试平台,每次登陆都要手动输入username和password 非常繁琐.所以为了更少的引入其他框架,就 ...

  10. 关于WHERE后面不能放聚合函数(如SUM(...))的解决办法

    我们在编写SQL语句的时候,常常会遇到需要将SUM()放到WHERE后面作为条件查询,事实证明这样是无法执行的,执行会报异常:聚合不应出现在 WHERE 子句中. 那么如何解决呢,使用HAVING关键 ...