Covering(矩阵快速幂)
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×21×2 and 2×12×1, and the size of the playground is 4×n4×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.
1≤n≤10181≤n≤1018
Output
For each test cases, output the answer mod 1000000007 in a line.
Sample Input
1
2
Sample Output
1
5
递推公式为F[N]=F[N-1]+5F[n-2]+F[n-3]-F[n-4];
这个题有个坑点,就是在取模之后数值会变小,然后导致系数为-的给减的导致最后的值是-的
这种问题我们的解法是在最后的结果+MOD再取模
学到了
代码:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<vector>
#include<map>
#include<cmath>
#define MOD 1000000007
const int maxn=1e5+5;
typedef long long ll;
using namespace std;
struct mat
{
ll a[5][5];
};
mat Mul(mat a,mat b)
{
mat ans;
memset(ans.a,0,sizeof(ans.a));
for(int t=1;t<=4;t++)
{
for(int j=1;j<=4;j++)
{
for(int k=1;k<=4;k++)
{
ans.a[t][j]=(ans.a[t][j]+a.a[t][k]*b.a[k][j])%MOD;
}
}
}
return ans;
}
mat ans;
ll quickpow(ll n)
{
mat res;
memset(res.a,0,sizeof(res.a));
res.a[1][1]=1;
res.a[1][2]=5;
res.a[1][3]=1;
res.a[1][4]=-1;
res.a[2][1]=1;
res.a[3][2]=1;
res.a[4][3]=1;
while(n)
{
if(n&1)
{
ans=Mul(res,ans);
}
res=Mul(res,res);
n>>=1;
}
return ans.a[1][1];
}
int main()
{
ll n;
while(cin>>n)
{
memset(ans.a,0,sizeof(ans.a));
ans.a[1][1]=36;
ans.a[2][1]=11;
ans.a[3][1]=5;
ans.a[4][1]=1;
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
{
ll s=quickpow(n-4)+MOD;
printf("%lld\n",(s)%MOD);
}
}
return 0;
}
Covering(矩阵快速幂)的更多相关文章
- HDU 6185 Covering 矩阵快速幂
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6185 题意:用 1 * 2 的小长方形完全覆盖 4 * n的矩形有多少方案. 解法:小范围是一个经典题 ...
- HDU - 6185 Covering(暴搜+递推+矩阵快速幂)
Covering Bob's school has a big playground, boys and girls always play games here after school. To p ...
- HDU-6185-Covering(推递推式+矩阵快速幂)
Covering Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- 矩阵快速幂 HDU 4565 So Easy!(简单?才怪!)
题目链接 题意: 思路: 直接拿别人的图,自己写太麻烦了~ 然后就可以用矩阵快速幂套模板求递推式啦~ 另外: 这题想不到或者不会矩阵快速幂,根本没法做,还是2013年长沙邀请赛水题,也是2008年Go ...
- 51nod 算法马拉松18 B 非010串 矩阵快速幂
非010串 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 如果一个01字符串满足不存在010这样的子串,那么称它为非010串. 求长度为n的非010串的个数.(对1e9+7取模) ...
- 51nod 1113 矩阵快速幂
题目链接:51nod 1113 矩阵快速幂 模板题,学习下. #include<cstdio> #include<cmath> #include<cstring> ...
- 【66测试20161115】【树】【DP_LIS】【SPFA】【同余最短路】【递推】【矩阵快速幂】
还有3天,今天考试又崩了.状态还没有调整过来... 第一题:小L的二叉树 勤奋又善于思考的小L接触了信息学竞赛,开始的学习十分顺利.但是,小L对数据结构的掌握实在十分渣渣.所以,小L当时卡在了二叉树. ...
- HDU5950(矩阵快速幂)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5950 题意:f(n) = f(n-1) + 2*f(n-2) + n^4,f(1) = a , f(2 ...
- 51nod 1126 矩阵快速幂 水
有一个序列是这样定义的:f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7. 给出A,B和N,求f(n)的值. Input 输 ...
随机推荐
- 使用FileReader与FileWriter读写数据
-------------siwuxie095 工程名:TestFileRW 包名:com.siwuxie095.filerw 类名:FileReade ...
- css常见问题解决方法
设置方法: div内的img和span都需要设置vertical-align:middle; 解决inline-block的空格: http://www.w3cplus.com/css/fightin ...
- 黑盒测试实践--Day1 11.25
黑盒测试实践--Day1 今天完成任务情况: 晚上得到老师布置的本周小组作业--黑盒测试的基本要求,然后小组在上周作业建立的微信群里开了个在线的短会,主要内容如下: 组长小靳带领大家学习了这个要求 计 ...
- Mac OS 10.8 中的 OpenGL 开发环境设置(转)
转自:http://www.th7.cn/Program/cp/201305/137743.shtml 一.XCode 4.5 在项目的"Build Phases">&quo ...
- 编写高质量代码改善C#程序的157个建议——建议50:在Dispose模式中应区别对待托管资源和非托管资源
建议50:在Dispose模式中应区别对待托管资源和非托管资源 真正资源释放代码的那个虚方法是带一个bool参数的,带这个参数,是因为我们在资源释放时要区别对待托管资源和非托管资源. 提供给调用者调用 ...
- 如何恢复VS2013代码实时校验功能
VS2013在某一天突然无法进行实时代码校验了,只有在编译的时候,错误列表才显示语法错误 怎么来解决这个问题呢?试试环境重置吧. 首先:打开工具菜单,选择“导入和导出设置”. 其次:可以先导出选定 ...
- .net IAsyncResult 异步操作
//定义一个委托 public delegate int DoSomething(int count); //BeginInvoke 的回调函数 private static void Execute ...
- 使用Boost库(1)
如何说服你的公司.组织使用Boost库 one of the most highly regarded and expertly designed C++ library projects in th ...
- 系统架构一:snmp+mrtg服务器监控
//@author:yuan<turing_zhy@163.com> 码字不易,转载请注明出处 #================================== 开始,服务器准备 ...
- 【转】android手势处理揭秘
当滑动(fling)比移动(scroll)有更高的效率时,为什么要让用户使用scroll操作呢?在面积很小而数据又很多的移动设备上,要显示远在后面的那些内容scroll是很困难的,这种情况下fling ...