Tri Tiling

Time Limit: 1000MS Memory Limit: 65536K

Total Submissions: 9016 Accepted: 4684

Description

In how many ways can you tile a 3xn rectangle with 2x1 dominoes?

Here is a sample tiling of a 3x12 rectangle.

Input

Input consists of several test cases followed by a line containing -1. Each test case is a line containing an integer 0 <= n <= 30.

Output

For each test case, output one integer number giving the number of possible tilings.

Sample Input

2

8

12

-1

Sample Output

3

153

2131

Source

之前也写过这种堆方块,就是找递推的方程,还是比较简单的题目。但是这道题目却是一个升级,因为他的递推方程需要变形,所以以后遇到这类题目就又多了一点见识。

dp[n]=3*dp[n-2]+2*dp[n-4]+2*dp[n-6]+……2*dp[0];

如果只拿这个方程去解肯定麻烦或者还可能超时

变形

dp[n-2]=3*dp[n-4]+2*(dp[n-6]+dp[n-8]+…..dp[0]);

dp[n-2]-dp[n-4]=2*(dp[n-4]+dp[n-6]+dp[n-8]+….dp[0]);

dp[n]=4*dp[n-2]-dp[n-4];

就搞定了,

#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <algorithm> using namespace std;
int dp[35];
int n;
int main()
{
dp[0]=1;
dp[1]=0;
dp[2]=3;
for(int i=3;i<=30;i++)
{
if(i&1)
dp[i]=0;
else
dp[i]=dp[i-2]*4-dp[i-4];
}
while(scanf("%d",&n)!=EOF)
{
if(n==-1)
break;
printf("%d\n",dp[n]);
}
return 0;
}

HOJ 2124 &POJ 2663Tri Tiling(动态规划)的更多相关文章

  1. poj 3783 Balls 动态规划 100层楼投鸡蛋问题

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4098409.html 题目链接:poj 3783 Balls 动态规划 100层楼投鸡蛋问题 ...

  2. HOJ 2133&POJ 2964 Tourist(动态规划)

    Tourist Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1503 Accepted: 617 Description A ...

  3. HOJ 2148&POJ 2680(DP递推,加大数运算)

    Computer Transformation Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4561 Accepted: 17 ...

  4. POJ 1609 Tiling Up Blocks

    Tiling Up Blocks Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4675   Accepted: 1824 ...

  5. poj 2229 一道动态规划思维题

    http://poj.org/problem?id=2229 先把题目连接发上.题目的意思就是: 把n拆分为2的幂相加的形式,问有多少种拆分方法. 看了大佬的完全背包代码很久都没懂,就照着网上的写了动 ...

  6. [POJ 2063] Investment (动态规划)

    题目链接:http://poj.org/problem?id=2063 题意:银行每年提供d种债券,每种债券需要付出p[i]块钱,然后一年的收入是v[i],到期后我们把本金+收入取出来作为下一年度本金 ...

  7. [POJ 2923] Relocation (动态规划 状态压缩)

    题目链接:http://poj.org/problem?id=2923 题目的大概意思是,有两辆车a和b,a车的最大承重为A,b车的最大承重为B.有n个家具需要从一个地方搬运到另一个地方,两辆车同时开 ...

  8. poj 2506 Tiling(递推 大数)

    题目:http://poj.org/problem?id=2506 题解:f[n]=f[n-2]*2+f[n-1],主要是大数的相加; 以前做过了的 #include<stdio.h> # ...

  9. POJ 1088 滑雪 -- 动态规划

    题目地址:http://poj.org/problem?id=1088 Description Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当 ...

随机推荐

  1. cmd.exe启动参数详解

    https://blog.csdn.net/moonhillcity/article/details/53039763 各个系统中打开文件的命令 "windows系统: cmd " ...

  2. centos7 安装 最新版本的docker

    yum update # vim /etc/yum.repos.d/docker.repo //添加以下内容 [dockerrepo] name=Docker Repository baseurl=h ...

  3. SpringMVC由浅入深day02_7上传图片

    7 上传图片 7.1 需求 在修改商品页面,添加上传商品图片功能. 7.2 springmvc中对多部件类型解析 在页面form中提交enctype="multipart/form-data ...

  4. GoF--适配器设计模式

    1.概念:  适配器模式(Adapter Pattern)把一个类的接口变换成客户端所期待的另一种接口,从而使原本因接口不匹配而无法在一起工作的两个类能够在一起工作. 2.形式  a.类的适配器模式  ...

  5. Go之简单并发

    func Calculate(id int) { fmt.Println(id) } 使用go来实现并发 func main() { for i := 0; i < 100; i++ { go ...

  6. [转]Tomcat的部署

    1.1 Context descriptors Tomcat4中的Manager和Admin管理工具其实就是利用它来部署的.在Tomcat5中提出了Context descriptor这个概念,且为其 ...

  7. iOS开发-编译出错 duplicate symbols for architecture x86_64

    今天对原来项目文件进行重新整理,根据文件内容进行分类,结果复制粘贴时没注意把一个文件复制了两遍 编译的时候就出现Duplicate Symbol Error 在网上搜素了一圈发现也有人遇到过这个问题, ...

  8. SpringBoot(五)-- 整合Spring的拦截器

    一.步骤 1.创建我们自己的拦截器类并实现 HandlerInterceptor 接口. 2.创建一个Java类继承WebMvcConfigurerAdapter,并重写 addInterceptor ...

  9. sine曲线向前运动

    using UnityEngine; using System.Collections; public class sineWork : MonoBehaviour { float verticalS ...

  10. mybais 之parameterType ="list"

    <!-- 根据货品编号获取商品价格和库存 --> <select id="getGoodsPriceAndStockByGoodsNo" resultMap=&q ...