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? 

InputThere are no more than 5000 test cases.

Each test case only contains one positive integer n in a line.

1≤n≤10181≤n≤1018 
OutputFor each test cases, output the answer mod 1000000007 in a line. 
Sample Input

1
2

Sample Output

1
5 题意:用1*2铺满4*n的地面。
特别综合的一道题。求法是先用模拟暴搜找出初始几个n的情况,//1 5 11 36 95 281 781 2245 6336 18061
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#define MAX 1005
#define INF 0x3f3f3f3f
#define MOD 1000000007
using namespace std;
typedef long long ll; int b[][MAX];
int n;
ll ans; void dfs(int s){
int i,j;
if(s==*n){
ans++;
return;
}
for(i=;i<=;i++){
for(j=;j<=n;j++){
if(b[i][j]==){
if(i+<=){
if(b[i+][j]==){
b[i][j]=;
b[i+][j]=;
dfs(s+);
b[i][j]=;
b[i+][j]=;
}
}
if(j+<=n){
if(b[i][j+]==){
b[i][j]=;
b[i][j+]=;
dfs(s+);
b[i][j]=;
b[i][j+]=;
}
}
return;
}
}
}
}
int main()
{
int i;
while(~scanf("%d",&n)){
memset(b,,sizeof(b));
ans=;
dfs();
printf("%lld\n",ans);
}
return ;
}

然后再找规律,猜想存在递推关系,项数可以一点点加,当用五层循环寻找递推系数关系时发现,存在一种相同的系数情况。//1 5 1 -1 0

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#define MAX 1005
#define INF 0x3f3f3f3f
#define MOD 1000000007
using namespace std;
typedef long long ll; int x[]={,,,,,,,,,,}; void find(int i){
for(int a=-;a<=;a++){
for(int b=-;b<=;b++){
for(int c=-;c<=;c++){
for(int d=-;d<=;d++){
for(int e=-;e<=;e++){
if(a*x[i-]+b*x[i-]+c*x[i-]+d*x[i-]+e==x[i]){
printf("%d:%d %d %d %d %d\n",i,a,b,c,d,e);
}
}
}
}
}
}
}
int main()
{
int n,i;
for(i=;i<=;i++){
find(i);
}
return ;
}

最后确定递推公式:f[i]=f[i-1]+5*f[i-2]+f[i-3]-f[i-4]+0。(注意:负数系数取余时要先加再余)然后套矩阵快速幂。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#define MAX 10
#define MOD 1000000007
using namespace std;
typedef long long ll; struct mat{
ll a[MAX][MAX];
}; mat operator *(mat x,mat y) //重载乘运算
{
mat ans;
memset(ans.a,,sizeof(ans.a));
for(int i=;i<=;i++){
for(int j=;j<=;j++){
for(int k=;k<=;k++){
ans.a[i][j]+=(x.a[i][k]*y.a[k][j]+MOD)%MOD; //先加后余,避免负数取模异常
ans.a[i][j]%=MOD;
}
}
}
return ans;
}
mat qMod(mat a,ll n) //矩阵快速幂
{
mat t;
t.a[][]=;t.a[][]=;t.a[][]=;t.a[][]=-;
t.a[][]=;t.a[][]=;t.a[][]=;t.a[][]=;
t.a[][]=;t.a[][]=;t.a[][]=;t.a[][]=;
t.a[][]=;t.a[][]=;t.a[][]=;t.a[][]=; //单位矩阵
while(n){
if(n&) a=t*a; //顺序不可颠倒
n>>=;
t=t*t;
}
return a;
}
int main()
{
ll n;
while(~scanf("%I64d",&n)){
if(n==) printf("1\n");
else if(n==) printf("5\n");
else if(n==) printf("11\n");
else if(n==) printf("36\n");
else{
mat a;
memset(a.a,,sizeof(a.a));
a.a[][]=;
a.a[][]=;
a.a[][]=;
a.a[][]=; //初始矩阵
a=qMod(a,n-);
printf("%I64d\n",a.a[][]);
}
}
return ;
}

HDU - 6185 Covering(暴搜+递推+矩阵快速幂)的更多相关文章

  1. HDU 5950 Recursive sequence 【递推+矩阵快速幂】 (2016ACM/ICPC亚洲区沈阳站)

    Recursive sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Other ...

  2. hdu 2604 递推 矩阵快速幂

    HDU 2604 Queuing (递推+矩阵快速幂) 这位作者讲的不错,可以看看他的 #include <cstdio> #include <iostream> #inclu ...

  3. hdu 6185 递推+矩阵快速幂

    思路:考虑全部铺满时,前2列的放法.有如下5种情况:(转自http://blog.csdn.net/elbadaernu/article/details/77825979 写的很详细 膜一下)  假设 ...

  4. HDU 2842 (递推+矩阵快速幂)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2842 题目大意:棒子上套环.第i个环能拿下的条件是:第i-1个环在棒子上,前i-2个环不在棒子上.每个 ...

  5. 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] 类似于斐波那契数列的递推式子吧, 但 ...

  6. [hdu 2604] Queuing 递推 矩阵快速幂

    Problem Description Queues and Priority Queues are data structures which are known to most computer ...

  7. HDU6030 Happy Necklace(递推+矩阵快速幂)

    传送门:点我 Little Q wants to buy a necklace for his girlfriend. Necklaces are single strings composed of ...

  8. 五校联考R1 Day1T3 平面图planar(递推 矩阵快速幂)

    题目链接 我们可以把棱柱拆成有\(n\)条高的矩形,尝试递推. 在计算的过程中,第\(i\)列(\(i\neq n\))只与\(i-1\)列有关,称\(i-1\)列的上面/下面为左上/左下,第\(i\ ...

  9. LightOJ 1244 - Tiles 猜递推+矩阵快速幂

    http://www.lightoj.com/volume_showproblem.php?problem=1244 题意:给出六种积木,不能旋转,翻转,问填充2XN的格子有几种方法.\(N < ...

随机推荐

  1. 使用jQuery Ajax功能的时候需要注意的一个问题

    每次jquery的Ajax请求都会创建一个xmlHttprequest对象,理论上讲,长连接(页面需要和服务器保持长连接,而且在连接超时后需要重新请求连接)的请求是一个无限递归,请求数量是非常大的,但 ...

  2. 关于dubbo的负载均衡

    1 dubbo的集群 将同一个服务部署到多个机器上,然后全部注册到注册中心.这样的多个机器就是一个dubbo集群了. 2 dubbo的负载均衡是怎么回事 由于多台机器上都有同一个服务,因此consum ...

  3. LeetCode:安排工作以达到最大收益【455】

    LeetCode:安排工作以达到最大收益[455] 题目描述 有一些工作:difficulty[i] 表示第i个工作的难度,profit[i]表示第i个工作的收益. 现在我们有一些工人.worker[ ...

  4. BZOJ 4435 [Cerc2015]Juice Junctions 分治最小割+hash

    分治最小割的题目,要求n2. 之前用的n3的方法自然不能用了. 于是用hash,设hash[i][j]表示在最小割为i的时候,j是否与S联通. 看懂这个需要理解一下最小割树的构造. 这种题建议用EK写 ...

  5. appium不支持Android7.0系统设备解决办法

    1. 找到appium的安装目录下的adb.js文件. 2. 打开adb.js,手动修改该文件下的内容: Adb.prototype.getPIDsByName=function(name,cb){ ...

  6. 4.7 希尔(shell)排序法

    4-7 ShellSort.c #include <stdio.h> #include "4-1 CreateData.c" //生成随机数的函数 #define AR ...

  7. Kafka0.7运行时报错 kafka/javaapi/consumer/ConsumerConnector : Unsupported major.minor version 51.0 解决

    目前中央库中 org.apache.kafka 是用jdk1.7编译的, 故跑在1.6的jvm中会报错 解决方案: 1. 下载kafka源码, 本地sbt进行install, 编译前 java -ve ...

  8. python的easygui

    1.利用msgbox(单词messagebox的缩写)给出一个提示信息: import easygui as g reply=g.msgbox('This is a basic message box ...

  9. Apache禁止或允许固定IP访问特定目录、文件、URL

    1. 禁止访问某些文件/目录 增加Files选项来控制,比如要不允许访问 .inc 扩展名的文件,保护php类库: <Files ~ "\.inc$"> Order a ...

  10. python2.7 爬虫初体验爬取新浪国内新闻_20161130

    python2.7 爬虫初学习 模块:BeautifulSoup requests 1.获取新浪国内新闻标题 2.获取新闻url 3.还没想好,想法是把第2步的url 获取到下载网页源代码 再去分析源 ...