poj 2506Tiling

来源:互联网 发布:数据存储的方式 编辑:程序博客网 时间:2024/06/13 22:50
<div class="ptt" lang="en-US" style="text-align: center; font-size: 18pt; font-weight: bold; color: blue;">Tiling</div><div class="plm" style="text-align: center;font-size:14px;"><table align="center"><tbody><tr><td><strong>Time Limit:</strong> 1000MS</td><td width="10px"> </td><td><strong>Memory Limit:</strong> 65536K</td></tr><tr><td><strong>Total Submissions:</strong> 9204</td><td width="10px"> </td><td><strong>Accepted:</strong> 4378</td></tr></tbody></table></div><p class="pst" style="font-size: 18pt; font-weight: bold; color: blue;">Description</p><div class="ptx" lang="en-US" style="font-family: "Times New Roman", Times, serif;font-size:14px;">In how many ways can you tile a 2xn rectangle by 2x1 or 2x2 tiles? Here is a sample tiling of a 2x17 rectangle. <center><img src="http://poj.org/images/2506_1.jpg" alt="" /></center></div><p class="pst" style="font-size: 18pt; font-weight: bold; color: blue;">Input</p><div class="ptx" lang="en-US" style="font-family: "Times New Roman", Times, serif;font-size:14px;">Input is a sequence of lines, each line containing an integer number 0 <= n <= 250.</div><p class="pst" style="font-size: 18pt; font-weight: bold; color: blue;">Output</p><div class="ptx" lang="en-US" style="font-family: "Times New Roman", Times, serif;font-size:14px;">For each line of input, output one integer number in a separate line giving the number of possible tilings of a 2xn rectangle. </div><p class="pst" style="font-size: 18pt; font-weight: bold; color: blue;">Sample Input</p><pre class="sio" style="font-family: "Courier New", Courier, monospace;font-size:14px;">2812100200

Sample Output

317127318451004001521529343311354702511071292029505993517027974728227441735014801995855195223534251

//递推公式类似与oj 上的骨牌问题
#include<iostream>#include<stdio.h>using namespace std;int str[251][101];//用数组储存  一行一行的存int main(){   int i,j,t;   str[0][0]=1;//0的时候   str[1][0]=1;//长度为一的时候   str[2][0]=3;//长度为三的时候   t=0;//临时变量   for(  i = 3; i<=250 ;i++ )//一共250行   储存250种   {       for(j=0; j<=100 ; j++)//用数组储存       {           t += str[i-1][j] + 2*str[i-2][j];//递推公式   (这里储存的顺序是从个位开始的,方便相加)  (包括了进位)           str[i][j] = t%10;//储存当前位数的数,如果大于10  取余 进位           t=t/10;//看看是否需要进位       }   }   int n;   while(~scanf("%d",&n))   {       for(i=100;i>=0;i--)       {             if(str[n][i]!=0)                  break;       }       for(;i>=0;i--)        printf("%d",str[n][i]);       printf("\n");   }    return 0;}

0 0