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 输 ...
随机推荐
- maven安装错误履历
1\:maven cannot find entry:"/src/main/java" 先删除source下的文件夹 再新建文件夹
- memcache 随笔
第一次用可能有很多不足的地方 以后慢慢改进. memcache 是一个简单的键/值对 是通过键和值储存信息到memcache中 ,通过特定的键请求来返回信息. 信息会无限期的保留在内存中 : ...
- 643. Maximum Average Subarray I 最大子数组的平均值
[抄题]: Given an array consisting of n integers, find the contiguous subarray of given length k that h ...
- Linux tmux
一.简介 Tmux是一个用于终端复用的软件,它允许一个用户在一个终端窗口或远程终端会话中使用多个不同的终端会话.在同一个命令行接口处理多个程序,以及将程序从已经开始运行另外的程序的Unix shell ...
- p2148 [SDOI2009]E&D
传送门 分析 https://www.luogu.org/blog/flashblog/solution-p2148 代码 #include<bits/stdc++.h> using na ...
- js加载页面使用execute_script选定加载位置
#由于js逐步加载页面,存在未显示的网页无法加载源码 from selenium import webdriver driver = webdriver.Firefox() init_element ...
- wordpress中add_action和add_filter
add_action( string $tag, callable $function_to_add, int $priority = 10,int $accepted_args = 1 ) 官网是这 ...
- 2.python IP/DNS地址处理之IPy/Dnspython模块
1.IPy模块 在IP地址规划中,涉及到计算大量的IP地址,包括网段.网络掩码.广播地址.子网数.IP类型等,即便是专业的网络人员也要进行繁琐的计算,而IPy模块提供了专门针对IPV4地址与IPV6 ...
- Claims Based Authentication and Token Based Authentication和WIF
基于声明的认证方式,其最大特性是可传递(一方面是由授信的Issuer,即claims持有方,发送到你的应用上,注意信任是单向的.例如QQ集成登录,登录成功后,QQ会向你的应用发送claims.另一方面 ...
- Highway Networks(高速路神经网络)
Rupesh Kumar Srivastava (邮箱:RUPESH@IDSIA.CH)Klaus Greff (邮箱:KLAUS@IDSIA.CH)J¨ urgen Schmidhuber (邮箱: ...