hdu 6185 递推+【矩阵快速幂】
<题目链接>
<转载于 >>> >
题目大意:
让你用1*2规格的地毯去铺4*n规格的地面,告诉你n,问有多少种不同的方案使得地面恰好被铺满且地毯不重叠。答案对1000000007取模。
解题分析:
看到题目所给n的数据这么大,就知道肯定存在递推公式,至于递推公式的具体的分析过程 >>>大牛博客。求出递推公式后,由于数据太大,所以我们利用矩阵快速幂来加速。当然,如果比赛的时候想不到递推公式,我们也可以通过搜素得到前面的几组数据,然后在通过高斯消元来得到符合这些数据的公式的通解,最后再利用矩阵快速幂来求解。
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
#define LL long long
const int mod=;
struct matrix
{
LL x[][];
};
matrix mutimatrix(matrix a,matrix b)
{
matrix temp;
memset(temp.x,,sizeof(temp.x));
for(int i=;i<;i++)
for(int j=;j<;j++)
for(int k=;k<;k++)
{
temp.x[i][j]+=a.x[i][k]*b.x[k][j];
temp.x[i][j]%=mod;
}
return temp;
} matrix k_powmatrix(matrix a,LL n)//矩阵快速幂
{
matrix temp;
memset(temp.x,,sizeof(temp.x));
for(int i=;i<;i++)
temp.x[i][i]=; while(n)
{
if(n&)
temp=mutimatrix(temp,a); a=mutimatrix(a,a);
n>>=;
}
return temp;
} int main()
{
LL n;
while(scanf("%lld",&n)!=EOF)
{
//前面四个手算下
if(n==)
{
printf("1\n");
continue;
}
if(n==)
{
printf("5\n");
continue;
}
if(n==)
{
printf("11\n");
continue;
}
if(n==)
{
printf("36\n");
continue;
} matrix st;
memset(st.x,,sizeof(st.x));
st.x[][]=;
st.x[][]=;
st.x[][]=;
st.x[][]=-; st.x[][]=;
st.x[][]=;
st.x[][]=; matrix init;//初始矩阵
memset(init.x,,sizeof(init.x)); init.x[][]=;
init.x[][]=;
init.x[][]=;
init.x[][]=; st=k_powmatrix(st,n-);//经过n-4次相乘
st=mutimatrix(init,st);//然后再乘上初始矩阵 printf("%lld\n",(st.x[][]+mod)%mod);
}
return ;
}
2018-08-09
hdu 6185 递推+【矩阵快速幂】的更多相关文章
- hdu 6185 递推+矩阵快速幂
思路:考虑全部铺满时,前2列的放法.有如下5种情况:(转自http://blog.csdn.net/elbadaernu/article/details/77825979 写的很详细 膜一下) 假设 ...
- hdu 2604 递推 矩阵快速幂
HDU 2604 Queuing (递推+矩阵快速幂) 这位作者讲的不错,可以看看他的 #include <cstdio> #include <iostream> #inclu ...
- HDU 2842 (递推+矩阵快速幂)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2842 题目大意:棒子上套环.第i个环能拿下的条件是:第i-1个环在棒子上,前i-2个环不在棒子上.每个 ...
- Recursive sequence HDU - 5950 (递推 矩阵快速幂优化)
题目链接 F[1] = a, F[2] = b, F[i] = 2 * F[i-2] + F[i-1] + i ^ 4, (i >= 3) 现在要求F[N] 类似于斐波那契数列的递推式子吧, 但 ...
- HDU Queuing(递推+矩阵快速幂)
Queuing Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- HDU 5950 Recursive sequence 【递推+矩阵快速幂】 (2016ACM/ICPC亚洲区沈阳站)
Recursive sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Other ...
- HDU - 6185 Covering(暴搜+递推+矩阵快速幂)
Covering Bob's school has a big playground, boys and girls always play games here after school. To p ...
- [hdu 2604] Queuing 递推 矩阵快速幂
Problem Description Queues and Priority Queues are data structures which are known to most computer ...
- HDU6030 Happy Necklace(递推+矩阵快速幂)
传送门:点我 Little Q wants to buy a necklace for his girlfriend. Necklaces are single strings composed of ...
- 五校联考R1 Day1T3 平面图planar(递推 矩阵快速幂)
题目链接 我们可以把棱柱拆成有\(n\)条高的矩形,尝试递推. 在计算的过程中,第\(i\)列(\(i\neq n\))只与\(i-1\)列有关,称\(i-1\)列的上面/下面为左上/左下,第\(i\ ...
随机推荐
- android 使用SQLite存储数据
创建一个类继承SQLiteOpenHelper,重写他的构造方法.onCreate().onUpgrade() 构建出SQLiteOpenHelper实例后,再调用他的getReadableDatab ...
- stderr 和stdout
今天又查了一下fprintf,其中对第一个参数stderr特别感兴趣. int fprintf(FILE *stream,char *format,[argument]): 在此之前先区分一下:pri ...
- SpringBoot集成Dubbo
(1).新建一个普通Maven项目,用于存放一些公共服务接口及公共的Bean等. 项目: 公共Bean: package cn.coreqi.entities; import java.io.Seri ...
- SpringBoot整合Jdbc
(1).添加相关依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId ...
- kafka系列九、kafka事务原理、事务API和使用场景
一.事务场景 最简单的需求是producer发的多条消息组成一个事务这些消息需要对consumer同时可见或者同时不可见 . producer可能会给多个topic,多个partition发消息,这些 ...
- eclipse 反编译
Eclipse Class Decompiler安装此插件,可以编译源代码且调试
- phpstudy中apache的默认根目录的配置
默认配置文件是:vhosts.conf. 安装laravel后需要把根目录配置到public. 下面的配置需要在本地计算机的host文件配置域名,一个是“localhost”,一个是“www.goho ...
- 自动化运维之cobbler安装centos7.3
自动化运维之cobbler安装centos7.3 一.cobbler简介和服务 Cobbler是一款自动化操作系统安装的实现,与PXE安装系统的区别就是可以同时部署多个版本的系统,而PXE只能选择一种 ...
- 【前端】js截取or分割字符串的常见方法
1.截取字符串 分割字符串方法 1.charAt(): 没有一种有别于字符串类型的字符数据类型,所以返回的字符是长度为 1 的字符串 例如:var str="Hello world!&quo ...
- Java基础93 JDBC连接MySQL数据库
本文知识点(目录): 1.什么是jdbc 2.jdbc接口的核心API 3.使用JDBC技术连接MySQL数据库的方法 4.使用Statement执行sql语句(DDL.DML. ...