链接:http://acm.hdu.edu.cn/showproblem.php?

pid=1143

Tri Tiling
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2799 Accepted Submission(s): 1585

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

2

8

12

-1

Sample Output

3

153

2131



Source



University of Waterloo Local Contest 2005.09.24



Recommend



Eddy



大意——3*n的长方形方格,用2*1的骨牌铺满。问:给定一个n。求解总的方案数。

思路——显然这是一道递推题目。非常明显地能够发现,当n为奇数时,是无解的。并且f(0)=1,f(2)=3,如今我们考察最后两列:假设最后两列铺满。则有3种方案,所以方案数为3*f(n-2);假设最后两列不铺满。则一定是后面四列组合。有2种方案,所以方案数为f(n-4)。以此类推。当n>=4时,f(n)=3*f(n-2)+2*f(n-4)+2*f(n-6)+···+2*f(0)。令2*m=n,则f(m)=3*f(m-1)+2*f(m-2)+···+2*f(0),f(m-1)=3*f(m-2)+2*f(m-3)+···+2*f(0),两式相减并整理得到,f(m)=4*f(m-1)-f(m-2)。从而。f(n)=4*f(n-2)-f(n-4),n>=4,并且n为偶数。又由于n最大为30,则直接递推就可以。



复杂度分析——时间复杂度:O(n),空间复杂度:O(n)

附上AC代码:

#include <iostream>
#include <cstdio>
#include <string>
#include <cmath>
#include <iomanip>
#include <ctime>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
typedef unsigned int UI;
typedef long long LL;
typedef unsigned long long ULL;
typedef long double LD;
const double PI = 3.14159265;
const double E = 2.71828182846;
LL num[31] = {1, 0, 3, 0}; void init(); int main()
{
ios::sync_with_stdio(false);
init();
short n;
while (cin >> n && n != -1)
{
cout << num[n] << endl;
}
return 0;
} void init()
{
for (int i=4; i<31; i++)
num[i] = 4*num[i-2]-num[i-4];
}

HDU 1143 Tri Tiling的更多相关文章

  1. HDU 1143 Tri Tiling (递推)

    Tri Tiling Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total ...

  2. HDU 1143 Tri Tiling 递归问题

    将一个3*n的矩形用1*2的矩形填充,n为奇数时一定不能被填满,n*3%2==1 接下来处理这个问题我们要从简单的情况开始考虑,所谓递归就是要能将问题的规模不断减小,通过小问题的解决最后将复杂问题解决 ...

  3. HDU 1143 Tri Tiling(递归)

    意甲冠军:一些现有的1*2小盒子.求拼3*n多少个长方形的拼写. 思考: 因为它是一个递归式.肯定会遇到层的关系.仔细观察,研究发现,每层应设置2一层.(奇数层不能是矩形)而从显卡好最后一层的最后一战 ...

  4. Tri Tiling(hdu1143)

    Tri Tiling Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  5. Tri Tiling[HDU1143]

    Tri Tiling Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  6. POJ 2663 Tri Tiling

                                                                                    Tri Tiling   Time Li ...

  7. uva 10918 - Tri Tiling(规律)

    题目链接:uva 10918 - Tri Tiling 题目大意:给出n,计算用1*2的瓷砖有多少种方法铺满3*n的地方. 解题思路:和uva 10359 - Tiling有点相似,不过难度会比较大, ...

  8. POJ 2663 Tri Tiling 矩阵快速幂 难度:3

    Tri Tiling Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7841   Accepted: 4113 Descri ...

  9. I - Tri Tiling

      Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status #in ...

随机推荐

  1. 【贪心】小Y的炮[cannon]题解

    模拟赛的题目,做的时候由于第二题表打太久了,只剩下40分钟,想都没想就写了一个爆搜20分... 这道题单调性很关键,下面会解释 P.S.解释在代码里 #include<cstdio> #i ...

  2. A - Team

    Problem description One day three best friends Petya, Vasya and Tonya decided to form a team and tak ...

  3. Java中的synchronized

    学习 https://blog.csdn.net/a158123/article/details/78607964 以及 https://www.cnblogs.com/beiyetengqing/p ...

  4. [转]line-height1.5和line-height:150%的区别

    line-height1.5和line-height:150%的区别   一.区别 区别体现在子元素继承时,如下: 父元素设置line-height:1.5会直接继承给子元素,子元素根据自己的font ...

  5. 【C++】cin、cout的效率比scanf和printf低的解决方法

    玩竞赛的同学应该发现了C++中直接调用cout.cin的效率要比printf和scanf的效率要低. 要解决这个问题,只需要在前面加上一句 std::ios::sync_with_stdio(fals ...

  6. word中选择嵌入式时图片被遮住,只显示小部分的解决方法

    选中图片,点击如下 选择  行距选项 将行距改为单位行距即可.

  7. 终极解决VS2015 安装失败问题,如 安装包损坏或丢失

    1.去微软官网下载完成ISO镜像,最好不要在线安装, 打开官方链接 https://www.visualstudio.com/zh-cn/downloads/download-visual-studi ...

  8. [Intermediate Algorithm] - Everything Be True

    题目 所有的东西都是真的! 完善编辑器中的every函数,如果集合(collection)中的所有对象都存在对应的属性(pre),并且属性(pre)对应的值为真.函数返回ture.反之,返回false ...

  9. (转)基于Metronic的Bootstrap开发框架经验总结(1)-框架总览及菜单模块的处理

    http://www.cnblogs.com/wuhuacong/p/4757984.html 最近一直很多事情,博客停下来好久没写了,整理下思路,把最近研究的基于Metronic的Bootstrap ...

  10. 模拟登录新浪微博(Python)

    PC 登录新浪微博时, 在客户端用js预先对用户名.密码都进行了加密, 而且在POST之前会GET 一组参数,这也将作为POST_DATA 的一部分. 这样, 就不能用通常的那种简单方法来模拟POST ...