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(矩阵快速幂)的更多相关文章

  1. HDU 6185 Covering 矩阵快速幂

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

  2. HDU - 6185 Covering(暴搜+递推+矩阵快速幂)

    Covering Bob's school has a big playground, boys and girls always play games here after school. To p ...

  3. HDU-6185-Covering(推递推式+矩阵快速幂)

    Covering Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  4. 矩阵快速幂 HDU 4565 So Easy!(简单?才怪!)

    题目链接 题意: 思路: 直接拿别人的图,自己写太麻烦了~ 然后就可以用矩阵快速幂套模板求递推式啦~ 另外: 这题想不到或者不会矩阵快速幂,根本没法做,还是2013年长沙邀请赛水题,也是2008年Go ...

  5. 51nod 算法马拉松18 B 非010串 矩阵快速幂

    非010串 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 如果一个01字符串满足不存在010这样的子串,那么称它为非010串. 求长度为n的非010串的个数.(对1e9+7取模) ...

  6. 51nod 1113 矩阵快速幂

    题目链接:51nod 1113 矩阵快速幂 模板题,学习下. #include<cstdio> #include<cmath> #include<cstring> ...

  7. 【66测试20161115】【树】【DP_LIS】【SPFA】【同余最短路】【递推】【矩阵快速幂】

    还有3天,今天考试又崩了.状态还没有调整过来... 第一题:小L的二叉树 勤奋又善于思考的小L接触了信息学竞赛,开始的学习十分顺利.但是,小L对数据结构的掌握实在十分渣渣.所以,小L当时卡在了二叉树. ...

  8. 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 ...

  9. 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 输 ...

随机推荐

  1. HtmlHelper扩展

    扩展 HtmlHelper类 public static class MyHtmlHelper { //扩展方法 //静态类,静态方法,this关键字 //调用方法<%=Html.MyLabel ...

  2. IWebBrowser和IE浏览器的行为不一样

    原本一直以为IWebBrowser2的行为和IE浏览器的行为应该是一样的,但是最近发现事实不是如此. IE8以后的浏览器都带有兼容模式,而IWebBrowser2默认情况下是在兼容模式下运行的,可以参 ...

  3. SQL SERVER 2008权限配置

    我要的结果是这样:只能有查询表的权限,而且还要有运行SQL Server Profiler的权限.这样才能跟踪发现问题,当然解决问题是另外一回事,即不能有修改和更新存储过程的权限. 我在分配角色成员时 ...

  4. CMD指令大全

    命令提示符(CMD)是在OS / 2 , Windows CE与Windows NT平台为基础的操作系统(包括Windows 2000和XP中, Vista中,和Server 2003 )下的“MS- ...

  5. U14.04 teamviewer install

    转载http://www.cnblogs.com/kunyuanjushi/p/5205639.html 安装i386库 sudo apt-get install libc6:i386 libgcc1 ...

  6. nmap 笔记

    本文由阿德马翻译自国外网站,请尊重劳动成果,转载请注明出处,谢谢 1.初级用法: 教程   Nmap使用不同的技术来执行扫描,包括:TCP的connect()扫描,TCP反向的ident扫描,FTP反 ...

  7. HTML总结之:HTML5的DOCTYPE 与 meta 属性介绍

    HTML5头部常用介绍: [DOCTYPE html] 声明文档类型为HTML5文件.   [meta标签] <meta> 元素可提供有关页面的元信息(meta-information), ...

  8. IOS GPS跟踪备注

    CLLocationManager还提供了如下类方法来判断当前设备的定位相关服务状态. Ø + locationServicesEnabled:返回当前定位服务是否可用. Ø + deferredLo ...

  9. 标准库函数begin和end------c++primer

    尽管能计算得到尾后指针,但这种用法极易出错.为了让指针的使用更简单.更安全,c++新标准引入了两个名为begin和end的函数.这两个函数与容器中的两个同名成员功能类似,不过数组毕竟不是类类型,因此这 ...

  10. Delphi XE8中开发DataSnap程序常见问题和解决方法 (三)用TClientDataSet的“ProviderName”属性连接服务器时,无法找到服务器端的“DatasetProvier”

    当我们在客户端用TClientDataSet的“ProviderName”属性连接服务器时,无法找到服务器端的“DatasetProvier”!! 问题原因:服务器端对提供服务的控件所属单元选择错误. ...