您的位置:首页 > 其它

POJ - 2506 Tiling (递推+高精度运算)@

2017-04-15 10:46 387 查看
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
2
8
12
100
200


Sample Output
3
171
2731
845100400152152934331135470251
1071292029505993517027974728227441735014801995855195223534251


a
=2*a[n-2]+a[n-1];

#include <algorithm>
#include <string.h>
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <set>
using namespace std;
typedef long long LL;
const int N = 2010;
int a[300][300];

int main()
{
memset(a,0,sizeof(a));
a[0][0]=1, a[1][0]=1,a[2][0]=3;
int n;
while(scanf("%d", &n)!=EOF)
{
if(n==2) cout<<3<<endl;
else
{
int digit=1;
for(int i=3; i<=n; i++)
{
int sum=0;
for(int j=0; j<digit; j++)
{
a[i][j]=(sum+a[i-1][j]+2*a[i-2][j])%10000;
sum=(sum+a[i-1][j]+2*a[i-2][j])/10000;
}
if(sum!=0)
{
a[i][digit++]=sum;
}
}
cout<<a
[digit-1];
for(int i=digit-2; i>=0; i--) printf("%04d",a
[i]);
cout<<endl;
}
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: