HDU 1143 Tri Tiling
链接:http://acm.hdu.edu.cn/showproblem.php?
pid=1143
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的更多相关文章
- HDU 1143 Tri Tiling (递推)
Tri Tiling Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...
- HDU 1143 Tri Tiling 递归问题
将一个3*n的矩形用1*2的矩形填充,n为奇数时一定不能被填满,n*3%2==1 接下来处理这个问题我们要从简单的情况开始考虑,所谓递归就是要能将问题的规模不断减小,通过小问题的解决最后将复杂问题解决 ...
- HDU 1143 Tri Tiling(递归)
意甲冠军:一些现有的1*2小盒子.求拼3*n多少个长方形的拼写. 思考: 因为它是一个递归式.肯定会遇到层的关系.仔细观察,研究发现,每层应设置2一层.(奇数层不能是矩形)而从显卡好最后一层的最后一战 ...
- Tri Tiling(hdu1143)
Tri Tiling Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- Tri Tiling[HDU1143]
Tri Tiling Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- POJ 2663 Tri Tiling
Tri Tiling Time Li ...
- uva 10918 - Tri Tiling(规律)
题目链接:uva 10918 - Tri Tiling 题目大意:给出n,计算用1*2的瓷砖有多少种方法铺满3*n的地方. 解题思路:和uva 10359 - Tiling有点相似,不过难度会比较大, ...
- POJ 2663 Tri Tiling 矩阵快速幂 难度:3
Tri Tiling Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7841 Accepted: 4113 Descri ...
- I - Tri Tiling
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Status #in ...
随机推荐
- go的接口
一.接口定义 接口类型 在讲基础数据类型时,我们曾提了一下 interface 数据类型,这个数据类型就是接口类型 什么是接口 Go 语言不是"传统"的面向对象的编程语言:它里面没 ...
- 最小生成树之Prim算法(最原始最详细入门)
//算法6.8 普里姆算法 #include <iostream> using namespace std; typedef char VerTexType; typedef int Ar ...
- jQuery获取及设置单选框、多选框、文本框
获取一组radio被选中项的值 var item = $("input[@name=items][@checked]").val(); 获取select被选中项的文本 var it ...
- ScreenRecord(about C# winform)
由于一些不得不做的事(哈,有时间再聊),所以就不得不写一个关于录屏的软件了,如此无力的开篇,开始吧. 在网上搜了很多关于录屏的源码,发现都使不了,剧情的需要很难满足.于是突然想到了github上的一个 ...
- 将DataTable某一列的值整体赋值给 另一个DataTable
将 DataTable某一列的值,赋值给 另一个DataTable: DataSet _ds=bll.GetAllList(); //将要取其中一列 DataView view = _ds.Table ...
- buf.readInt16LE函数详解
offset {Number} 0 noAssert {Boolean} 默认:false 返回:{Number} 从该 Buffer 指定的带有特定尾数格式(readInt16BE() 返回一个较大 ...
- 【原创】redhat5安装oracle10g
安装缺失的包: 用 root 用户身份运行以下命令: rpm -q gcc make binutils openmotif setarch compat-db compat-gcc compat-gc ...
- Android 链接 手机有关问题及解决方案
我出现的问题: 这是我百度的解决方案:
- java JDBC连接 Sqlserver 非默认的实例名问题
一般我们在连接数据库的时候都是用的默认实例名,今天遇到了用非默认是实例名:连接代码如下(Java): <property name="url" value="jdb ...
- 微信小程序—picker(滚动选择器)
官方api:https://mp.weixin.qq.com/debug/wxadoc/dev/component/picker.html 上边是官网的api.小程序中,底部下拉滚动选择主要有这几种 ...