poj2506 Tiling
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 8177 | Accepted: 3960 |
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
Source
/*高精度+递推
#include<stdio.h>
#include<string.h>
struct node
{
char ch[501];
} a[300];
void sum(char *a, char *b, char *c)
{
int i, j, k, len1, len2;
len1 = strlen(a);
len2 = strlen(b);
k = (len1 > len2) ? len1 : len2;
memset(c, '0', sizeof(c));
c[k + 1] = '\0';
int temp, jin;
jin = 0;
for(i = len1 - 1, j = len2 - 1; i >= 0 || j >= 0; i--, j--)
{
temp = jin;
if(i >= 0)
temp += a[i] - '0';
if(j >= 0)
temp += b[j] - '0';
if(temp >= 10)
{
c[k] = temp - 10 + '0';
jin = 1;
}
else
{
c[k] = temp + '0';
jin = 0;
}
k--;
}
c[k] = jin + '0';
}
int main()
{
int n, i;
char st[501], *p;
strcpy(a[0].ch,"1");
strcpy(a[1].ch, "1");
strcpy(a[2].ch, "3");
for(i = 3; i <= 250; i++)
{
sum(a[i - 1].ch, a[i - 2].ch, st);
sum(a[i - 2].ch, st, a[i].ch);
}
while(scanf("%d", &n) != EOF)
{
p = a[n].ch;
while(*p == '0')
p++;
printf("%s\n", p);
}
}