Tiling
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 9413 | Accepted: 4480 |
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
题意:有三种可选择的矩形块,2x2,1x2,2x1三种,有2xn的一个空间,有几种方法填满;
题解:s[i] = s[i - 1] + s[i - 2] * 2;
大数题有坑点。s[0]=1;
#include <iostream>
#include <string>
#include <string.h>
using namespace std;
string f(string a, string b)
{
int al=a.length();
int bl=b.length();
int c[100];
memset(c,0,sizeof(c));//建立一个c数组用来存两个串的和的各位数;
int x, y, k=0, i , j;
string s;
for(i=al-1, j=bl-1; i>=0||j>=0; i--, j--)//从个位开始(逆序)比较
{
if(i>=0)
{
x=a[i]-'0';
}
else
{
x=0;
}
if(j>=0)
{
y=b[j]-'0';
}
else
{
y=0;
}//存在数字就记为整数,否则记为0;
c[k++]=x+y;//加和存入c数组
}
for(i=0; i<k; i++)//处理C数组的每一项都为个位;
{
c[i+1]+=(c[i]/10);
c[i]%=10;
}
while(c[i]==0)//消去前边的0,即000112中的前三个0;
{
i--;
}
for( ; i>=0; i--)//赋值给s串;
{
s+=c[i]+'0';
}
return s;
}
int main()
{
string s[251];
int n, i;
s[0]="1";
s[1]="1";
s[2]="3";
for(i=3; i<251; i++)
{
s[i]=f(s[i-1],f(s[i-2],s[i-2]));//调用f(a, b)函数,即a+b;
}
while(cin>>n)
{
cout<<s[n]<<endl;
}
return 0;
}