POJ 2506 Tiling(DP+大数运算)

来源:互联网 发布:电子竞技综合数据分析 编辑:程序博客网 时间:2024/06/05 17:54
                                                                                           Tiling
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 4720 Accepted: 2280

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

2812100200

Sample Output

317127318451004001521529343311354702511071292029505993517027974728227441735014801995855195223534251
这道题实在是一道容易强奸做题人的水题,我看到这个题目的时候很快就推出了动态转移方程dp[i]=dp[i-1]+dp[i-2]*2;写很快的蒋程序
写出来了,没有想到的是竟然WA了4次,杯具呀杯具!!我最后考虑到是不是0的取值问题,果不其然,隐藏的太深了。也许有些
人会认为应该可以AC了吧,但是一定一定要仔细,要不然又要WA了,当为0时的值为1(强大的数据).YMing。
下面是我程序:
#include<iostream>
#include<cstring>
using namespace std;
int dp[301][200],n,Long[301];
int main()
{
    memset(dp,0,sizeof(dp));
    memset(Long,0,sizeof(Long));
    int i,j,t;
    dp[0][1]=1;
    dp[1][1]=1;dp[2][1]=3;
    Long[0]=Long[1]=Long[2]=1;
    for(i=3;i<=300;i++)
    {
         t=Long[i-1];
         for(j=1;j<=t;j++)
             dp[i][j]=dp[i-1][j]+dp[i-2][j]*2;
         for(j=1;j<=t;j++)
         {
            
             if(dp[i][j]>9)
             {
                  if(dp[i][t]>9) t++;
                  dp[i][j+1]+=dp[i][j]/10;
                  dp[i][j]%=10;
             }
         }
         Long[i]=t;
    }                                                  
    while(cin>>n)
    {
         for(i=Long[n];i>=1;i--)
            cout<<dp[n][i];
         cout<<endl;
    }
    return 0;
}                                          
原创粉丝点击