[ACM] POJ 2506 Tiling (递归,睑板)

Tiling

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 7487   Accepted: 3661

Description

In how many ways can you tile a 2xn rectangle by 2x1 or 2x2 tiles?

Here is a sample tiling of a 2x17 rectangle.

Input

Input is a sequence of lines, each line containing an integer number 0 <= n <= 250.

Output

For each line of input, output one integer number in a separate line giving the number of possible tilings of a 2xn rectangle.

Sample Input

2
8
12
100
200

Sample Output

3
171
2731
845100400152152934331135470251
1071292029505993517027974728227441735014801995855195223534251

Source

The UofA Local 2000.10.14

解题思路:

有两种长方形, 1*2 (长1。高2),  2*2   .给定一块 n*2的大长方形,问用两种规格的长方形把它铺满,一共同拥有多少种方法。

如果已经求得了 长度为n-1 的方法数。那么要铺满n,仅仅有一种方法。用1*2

如果已经求得了 长度为n-2 的方法数,那么要铺满n。有三种方法

可是第三种会跟已经求得了长度为n-1的方法数的情况反复,去掉。

所以求得的递推方程为   f [0] =1   f[1] = 1    f[2] =3     f[n]= f[n-2]*2 + f[n-1]

须要注意的是本题用到了大数。

代码:

#include <iostream>
#include <string.h>
using namespace std;
string s1,s2;
int a[1000],b[1000],c[1000];//a,b保存两个字符串得到的大数。c保存a,b相加以后得到的大数
string f[300];

string add(string s1,string s2) //将大数s1,s2相加。并返回字符串类型的结果
{
    memset(a,0,sizeof(a));
    memset(b,0,sizeof(b));
    memset(c,0,sizeof(c));
    string result;
    int lena=s1.length();
    int lenb=s2.length();
    int k=0;
    for(int i=lena-1;i>=0;i--)
        a[k++]=s1[i]-'0';
    k=0;
    for(int j=lenb-1;j>=0;j--)
        b[k++]=s2[j]-'0';
    int len=lena>lenb?

lena:lenb;
    for(int i=0;i<len;i++)
    {
        c[i]+=a[i]+b[i];//注意是+=,还要考虑进位
        if(c[i]>=10)
        {
            c[i+1]++;
            c[i]-=10;
        }
    }
    int i;
    for( i=999;i>=0;i--)
        if(c[i]!=0)
            break;
    for(;i>=0;i--)
        result+=(char)(c[i]+'0');
    return result;
}

void get()
{
    f[0]="1";
    f[1]="1";
    f[2]="3";
    f[3]="5";
    for(int i=4;i<=250;i++)
    {
        f[i]=add(f[i-2],f[i-2]);
        f[i]=add(f[i],f[i-1]);
    }
}
int main()
{
    get();
    int n;
    while(cin>>n)
    {
        cout<<f[n]<<endl;
    }
    return 0;
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

时间: 2024-10-02 01:50:55

[ACM] POJ 2506 Tiling (递归,睑板)的相关文章

[ACM] POJ 2506 Tiling (递推,大数)

Tiling Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7487   Accepted: 3661 Description In how many ways can you tile a 2xn rectangle by 2x1 or 2x2 tiles? Here is a sample tiling of a 2x17 rectangle. Input Input is a sequence of lines,

POJ 2506 -TILING

水题,一个小模拟,规律也好找 f3 = f1 * 2 + f2; #include <iostream> #include <cstdlib> #include <cstdio> #include <cstring> #include <algorithm> const int INF = 1e8; const int N = 100; #define ll long long using namespace std; int a[251][N]

poj 2506 Tiling(java解法)

题目链接:http://poj.org/problem?id=2506 本题用的java解的,因为涉及到大数问题,如果对java中的大数操作不熟悉请点这儿:链接 思路:地推公式f[i]=f[i-1]+2*f[i-2] code: import java.math.*; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner cin=new Scanner(Syst

POJ 2506 Tiling (递推 + 大数加法模拟 )

Tiling Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7965   Accepted: 3866 Description In how many ways can you tile a 2xn rectangle by 2x1 or 2x2 tiles? Here is a sample tiling of a 2x17 rectangle. Input Input is a sequence of lines,

POJ 2506 Tiling(递推+大整数加法)

http://poj.org/problem?id=2506 题意: 思路:递推.a[i]=a[i-1]+2*a[i-2]. 计算的时候是大整数加法.错了好久,忘记考虑1了...晕倒. 1 #include<iostream> 2 #include<string> 3 #include<cstring> 4 #include<cstdio> 5 using namespace std; 6 7 int n; 8 char s[255][255]; 9 10

poj 2506 Tiling 递推

题目链接: http://poj.org/problem?id=2506 题目描述: 有2*1和2*2两种瓷片,问铺成2*n的图形有多少种方法? 解题思路: 利用递推思想,2*n可以由2*(n-1)的状态加上一块竖放2*1的瓷片转移得来,也可以由2*(n-2)的状态加上一块2*2的瓷片或者加上两块横放的2*1的瓷片转移得来. 可得出递推公式:dp[n] = dp[n-1] + dp[n-2]*2: ac秘诀: (1):从输出样例可以看出要用大数来表示,大概需要90位左右. (2):2*0不是零种

poj 2506 Tiling(高精度)

Description In how many ways can you tile a 2xn rectangle by 2x1 or 2x2 tiles? Here is a sample tiling of a 2x17 rectangle.  Input Input is a sequence of lines, each line containing an integer number 0 <= n <= 250. Output For each line of input, out

POJ 2506 Tiling (递推+高精度)

[题目链接]click here~~ [题目大意] In how many ways can you tile a 2xn rectangle by 2x1 or 2x2 tiles? Here is a sample tiling of a 2x17 rectangle. [解题思路]: (1)一个2*2的格子有三种填充方法: 两个横着放, 两个竖着放, 放一个2*2 (2)得出递推公式F[i]=F[i-1]+F[i-2]*2 然后就是套高精度模板了 代码; /* Author:HRW 递推+

poj 2506 Tiling(大数+规律)

poj2506Tiling 此题规律:A[0]=1;A[1]=1;A[2]=3;--A[n]=A[n-1]+2*A[n-2];用大数来写,AC代码: #include<stdio.h> #include<string.h> #define MAX 300 int num[MAX][MAX]; int main() { int n; while(scanf("%d",&n)!=EOF) { memset(num,0,sizeof(num)); num[0]