Tri Tiling

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 4343    Accepted Submission(s): 2518

Problem 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

2812-1
 
Sample Output

31532131

思路:

先来看能用来填满的格子的样式:1是只能加两列,2是能加4+2*n列

对第一种:f[n]=3*f[n-2](在前n-2基础上不断加第一种)

对第二种:f[n]=2*f[n-4]+2*f[n-6]+...+2*f[0](在第二种基础上加第一种方块)

两式相加:f[n]=3*f[n-2]+2*f[n-4]+2*f[n-6]+...+2*f[0]-----------1

由上式得出:f[n-2]=3*f[n-4]+2*f[n-6]+2*f[n-8]+...+2*f[0]------2

将2代入1式得出最后递归式:f[n]=4*f[n-2]-f[n-4]

Code:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<cctype>
#include<queue>
#include<math.h>
#include<iostream>
#include<algorithm>
#define INF 0x3f3f3f3f
#define N 1000005
using namespace std; int main(){
int n;
int a[35];
a[0]=1,a[2]=3;
for(int i=4;i<=30;i+=2) a[i]=4*a[i-2]-a[i-4];
while(~scanf("%d",&n)){
if(n==-1) break;
if(n%2) printf("0\n");
else printf("%d\n",a[n]);
}
return 0;
}

HDU1143 (递推)题解的更多相关文章

  1. 【KMP】【矩阵加速】【递推】洛谷 P3193 [HNOI2008]GT考试 题解

        看出来矩阵加速也没看出来KMP…… 题目描述 阿申准备报名参加 GT 考试,准考证号为\(N\)位数\(X_1,X_2…X_n(0\le X_i\le9)\),他不希望准考证号上出现不吉利的数 ...

  2. P1541 乌龟棋 题解(洛谷,动态规划递推)

    题目:P1541 乌龟棋 感谢大神的题解(他的写的特别好) 写一下我对他的代码的理解吧(哎,蒟蒻就这能这样...) 代码: #include<bits/stdc++.h> #define ...

  3. [题解][SHOI2013]超级跳马 动态规划/递推式/矩阵快速幂优化

    这道题... 让我见识了纪中的强大 这道题是来纪中第二天(7.2)做的,这么晚写题解是因为 我去学矩阵乘法啦啦啦啦啦对矩阵乘法一窍不通的童鞋戳链接啦 层层递推会TLE,正解矩阵快速幂 首先题意就是给你 ...

  4. [模板][题解][Luogu1939]矩阵乘法加速递推(详解)

    题目传送门 题目大意:计算数列a的第n项,其中: \[a[1] = a[2] = a[3] = 1\] \[a[i] = a[i-3] + a[i - 1]\] \[(n ≤ 2 \times 10^ ...

  5. 题解报告:hdu 2084 数塔(递推dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2084 Problem Description 在讲述DP算法的时候,一个经典的例子就是数塔问题,它是这 ...

  6. 牛客网暑期ACM多校训练营(第二场) 题解 A run 递推 dp

    链接:https://www.nowcoder.com/acm/contest/140/A来源:牛客网 White Cloud is exercising in the playground. Whi ...

  7. 致初学者(四):HDU 2044~2050 递推专项习题解

    所谓递推,是指从已知的初始条件出发,依据某种递推关系,逐次推出所要求的各中间结果及最后结果.其中初始条件或是问题本身已经给定,或是通过对问题的分析与化简后确定.关于递推的知识可以参阅本博客中随笔“递推 ...

  8. 第二场周赛(递归递推个人Rank赛)——题解

    很高兴给大家出题,本次难度低于上一场,新生的六个题都可以直接裸递归式或者裸递推式解决,对于老生的汉诺塔3,需要找出一般式,后两题分别为裸ST算法(或线段树)/线性DP. 正确的难度顺序为 种花 角谷定 ...

  9. BZOJ5017题解SNOI2017炸弹--玄学递推

    题目链接 https://www.lydsy.com/JudgeOnline/problem.php?id=5017 分析 老师讲课谈到了这道题,课上想出了个连边建图然后乱搞的操作,被老师钦定的递推方 ...

  10. luogu题解 P1707 【刷题比赛】矩阵加速递推

    题目链接: https://www.luogu.org/problemnew/show/P1707 分析: 洛谷的一道原创题,对于练习矩阵加速递推非常不错. 首先我们看一下递推式: \(a[k+2]= ...

随机推荐

  1. Frogger--poj2253

    http://poj.org/problem?id=2253 题意:The frog distance (humans also call it minimax distance) between t ...

  2. 用Monitor简单3步监控中间件ActiveMQ

    Apache ActiveMQ是一个基于JMX规范的纯Java消息中间件,它为应用系统提供高效.灵活的消息同步与异步传输处理.存储转发.可靠传输的特性. 消息队列对于应用的健康运行非常重要,作为运维人 ...

  3. Linux IO模式及 select、poll、epoll详解及源码(转)

    原文:https://segmentfault.com/a/1190000003063859 我只摘取了其中的epoll代码示例,服务端代码 #define IPADDRESS "127.0 ...

  4. java连接数据库时的报错

    //java连接数据库时的报错 1 package Java数据库编程; import java.sql.DriverManager; import java.sql.SQLException; im ...

  5. 使用java进行excel读取和写入

    1:添加处理excel的依赖jar包 <!-- 引入poi,解析workbook视图 --> <dependency> <groupId>org.apache.po ...

  6. [LeetCode] 687. Longest Univalue Path_Easy tag: DFS recursive

    Given a binary tree, find the length of the longest path where each node in the path has the same va ...

  7. virtualBox虚拟机联网

    1. 2. 3. 4. 5. 6. 7. 8. 9. 10.

  8. Problem B. Full Binary Tree

    题目 链接:http://code.google.com/codejam/contest/2984486/dashboard#s=p1 googlde code jam 2014 Round1A 解题 ...

  9. 019-centos的yum用法

    1.检测系统是否已经安装过mysql或其依赖:# yum list installed | grep mysql(当然也可以用 rpm -qa | grep mysql) 2.卸载已经存在的mysql ...

  10. JavaScript: apply 方法 详解(转)——非常好

    转载自  http://www.cnblogs.com/KeenLeung/archive/2012/11/19/2778229.html 我在一开始看到javascript的函数apply和call ...