http://poj.org/problem?id=2506

题目大意:用多少种方法可以用2*1或2*2瓦片来铺一个2*n的矩形?

这是一个2*17长方形的样品。

输入是一行行的序列,每一行包含一个整数0 <= n <= 250。对于每一行输入,在单独的行中输出一个整数,给出一个2 * n矩形的可能摆放方式数。

也就是说给一个2*n的棋盘,用三种方块(2*2,1*2,2*1)将其铺满,求有多少种可能性,通过给出的案例可以发现,输出的结果很大long long类型也存不下,所以要运用大整数的加法。作图可以发现递归方程式,对2 * 1,2 * 2,2 * 3来说 2 * 1有1种;2 * 2有3种;2 * 3 可以在2 * 2 的基础上加一个2 * 1竖着放的瓦片,在 2 * 1 的基础上加一个2 * 2的瓦片,也可以加两个2 * 1横着放的瓦片(竖着放与在 2 * 2的基础上重复)。num[3] = num[2]+ 2 * num[1]

算法思想:递归求解,这里采用一次性计算的迭代法提高算法的效率。我们可以发现递归的方程式:

1)num[i] = num[i - 1]+ 2 * num[i - 2];  i>2

2)num[0]  =  1 ;  i=0

3)num[1]  =  1 ;  i=1

4)num[2]  =  3 ;  i=2

 #include <iostream>
#include <string>
using namespace std;
string Add(string str1, string str2)//大整数加法(两个正整数)
{
string str;
int len1 = str1.length();
int len2 = str2.length();
if (len1 < len2) //前面补0,使两个字符串长度相同
{
for (int i = ; i <= len2 - len1; i++)
str1 = "" + str1;
}
else
{
for (int i = ; i <= len1 - len2; i++)
str2 = "" + str2;
}
len1 = str1.length();
int cf = ;//进位
int temp;//当前位的值
for (int i = len1 - ; i >= ; i--)
{
temp = str1[i] - '' + str2[i] - '' + cf;
cf = temp / ;
temp %= ;
str = char(temp + '') + str;
}
if (cf != ) str = char(cf + '') + str;
return str;
}
int main()
{
string num[] = { "","","" };
for (int i = ; i <= ; i++)
{
num[i] = Add(num[i - ], Add(num[i - ], num[i-]));
}
int temp;
while (cin >> temp) {
cout << num[temp] << endl;
}
return ;
}

poj2506 Tiling的更多相关文章

  1. POJ2506——Tiling

    Tiling Description In how many ways can you tile a 2xn rectangle by 2x1 or 2x2 tiles? Here is a samp ...

  2. POJ2506:Tiling(递推+大数斐波那契)

    http://poj.org/problem?id=2506 #include <iostream> #include <stdio.h> #include <strin ...

  3. Texture tiling and swizzling

    Texture tiling and swizzling 原帖地址:http://fgiesen.wordpress.com If you’re working with images in your ...

  4. 图文详解Unity3D中Material的Tiling和Offset是怎么回事

    图文详解Unity3D中Material的Tiling和Offset是怎么回事 Tiling和Offset概述 Tiling表示UV坐标的缩放倍数,Offset表示UV坐标的起始位置. 这样说当然是隔 ...

  5. POJ3420Quad Tiling(矩阵快速幂)

    Quad Tiling Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3740 Accepted: 1684 Descripti ...

  6. Tri Tiling[HDU1143]

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

  7. Tiling 分类: POJ 2015-06-17 15:15 8人阅读 评论(0) 收藏

    Tiling Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8091   Accepted: 3918 Descriptio ...

  8. Tiling Up Blocks_DP

    Description Michael The Kid receives an interesting game set from his grandparent as his birthday gi ...

  9. I - Tri Tiling

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

随机推荐

  1. 761A Dasha and Stairs

    A. Dasha and Stairs time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  2. iOS.ObjC.Compiler.Directives

    Objective-C Compiler Directives @dynamic "You use the @dynamic keyword to tell the compiler tha ...

  3. 轻松学SQL Server数据库

    轻松学SQL Server数据库pdf   下载地址:网盘下载 目录:  第1章 数据库与SQL Server 2008 11.1 数据库基础 21.1.1 数据库的概念 21.1.2 数据库模型 2 ...

  4. Warning: Attempt to present A on B whose view is not in the window hierarchy!

    昨天写豆瓣发广播Demo的时候,为了写Demo的简单,就使用了Storyboard,结果执行视图跳转时遇到了这个问题: Warning: Attempt to present <UINaviga ...

  5. 编译HBase1.0.0-cdh5.4.2版本

    1. 编译环境准备 Jdk:1.7.0_x Maven : 3.3.9 hbase: cdh5.4.2-release 2. 用idea打开项目 使用git clone得到HBase源码.打开git: ...

  6. 2018.10.20 bzoj2748: [HAOI2012]音量调节(背包)

    传送门 这题是不是太sbsbsb了一点. 难度直逼普及-. 直接背包判存在性就行了. 代码: #include<bits/stdc++.h> using namespace std; bo ...

  7. 2018.10.05 NOIP模拟 上升序列(状压dp)

    传送门 状压dp好题. 首先需要回忆O(nlogn)O(nlog n)O(nlogn)求lislislis的方法,我们会维护一个单调递增的ddd数组. 可以设计状态f(s1,s2)f(s1,s2)f( ...

  8. 24. Indoor Air pollution 室内空气污染

    . Indoor Air pollution 室内空气污染 ① Priscilla Ouchida's "energy-efficient"house turned out to ...

  9. noexcept(c++11)

    1.概念 1)c++中的异常处理是在运行时而不是编译时检测的,为了实现运行时检测,编译器可能会创建额外的异常处理代码,然而这会妨碍程序优化 2)noexcept说明符:若修饰函数(紧跟在参数列表后面) ...

  10. DataGrid组件

    <?xml version="1.0" encoding="utf-8"?><s:WindowedApplication xmlns:fx=& ...