Tiling 分类: POJ 2015-06-17 15:15 8人阅读 评论(0) 收藏
Tiling
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 8091 | Accepted: 3918 |
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.
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 这个题是一个递推,a[i]=a[i-1]+2*a[i-2],不过要注意零a[0]=1.#include <cstdio> #include <string.h> #include <cmath> #include <stack> #include <iostream> #include <algorithm> #define WW freopen("output.txt","w",stdout) using namespace std; const int Mod=10; const int Max=200; int num[300][Max]; int main() { memset(num,0,sizeof(num)); num[0][0]=0; num[1][0]=1; num[2][0]=3; for(int i=3;i<300;i++)//模拟大数 { for(int j=0;j<Max;j++) { num[i][j]+=(num[i-1][j]+num[i-2][j]+num[i-2][j]); if(num[i][j]>=Mod) { num[i][j+1]=num[i][j+1]+(num[i][j]/Mod); num[i][j]=num[i][j]%Mod; } } } int n; while(~scanf("%d",&n)) { if(n==0) { printf("1\n"); continue; } bool flag=false; for(int i=Max-1;i>=0;i--) { if(flag)//去前导零 { printf("%d",num[n][i]); } else { if(num[n][i]) { flag=true; printf("%d",num[n][i]); } } } printf("\n"); } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。