[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


解题思路:

有两种长方形, 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; }



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

原文地址:https://www.cnblogs.com/mengfanrong/p/4645637.html