poj 2506 Tiling(高精度)

时间:2023-03-08 19:41:57

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. 
poj 2506 Tiling(高精度)

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 刚开始没推出来,还是看了Discuss才知道递推公式是:f[n]=f[n-1]+2*f[n-2],推论方法如下:
首先,无论任何一种方案,最左边的要么是一根竖的,要么是两根横的,要么是一个方的,对吧?所以:
当是一根竖时剩下的方案数是OPT[i-1]
当是两根横时剩下的方案数是OPT[i-2]
当是一个方时剩下的方案数是OPT[i-2]
故OPT[i]=OPT[i-1]+2*OPT[i-2]
转化为二阶齐次常系数线性方程为:
f(n-2)-f(n-1)-2f(n-2)=0
其特征方程是:
x^2-x-2=0
解得特征方程的根式:x=-1 或 x=2
故得
f(n)=a*(-1)^n+b*2^n
将f(0)=1,f(1)=1的值代入,解得f(n)=1/3*(-1)^n+2/3*2^n
可简化为:
if(n%2==0)
opt[n]=(2^(n+1)+1)/3
else
opt[n]=(2^(n+1)-1)/3
不要问我为什么,我也不知道。然后尝试着自己写,于是乎就把自己绕进去了,看了大神的代码才知道什么叫巧妙。。。。
 #include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
struct pos
{
int a[];
int len;
} path[];
void c2(pos &x) //乘2操作
{
int len=x.len;
for(int i=; i<=len; i++)
x.a[i]*=;
for(int i=; i<=len; i++)
{
if(x.a[i]>)
{
x.a[i]-=;
x.a[i+]++;
}
}
if(x.a[len+]!=)
x.len++;
}
void add(pos &a,pos b,pos c)/*加操作*/
{
int Max=max(b.len,c.len);
for(int i=; i<=Max; i++)
a.a[i]=b.a[i]+c.a[i];
for(int i=; i<=Max; i++)
{
if(a.a[i]>)
{
a.a[i]-=;
a.a[i+]++;
}
}
if(a.a[Max+]!=)
a.len=Max+;
else
a.len=Max;
}
int main()
{
int n;
while(cin>>n)
{
memset(path,,sizeof(path));
path[].a[]=;
path[].a[]=;
path[].len=path[].len=;
for(int i=; i<=n; i++)
{
c2(path[i-]);
add(path[i],path[i-],path[i-]);
}
for(int i=path[n].len; i>=; i--)
cout<<path[n].a[i];
cout<<endl;
}
return ;
}
贴上一位大神的代码如下:
 #include <iostream>
#define base 100000000
int a[][];
int main(int argc, char** argv) {
a[][]=a[][]=;
for(int i=;i<=;i++){
for(int j=;a[i-][j];j++){
a[i][j]+=a[i-][j]+*a[i-][j];
if(a[i][j]>=base){
a[i][j+]+=a[i][j]/base;
a[i][j]%=base;
}
}
}
int n;
while(~scanf("%d",&n)){
int j;
for(j=;a[n][j];j++);
j--;
printf("%d",a[n][j--]);
while(j>=)
printf("%08d",a[n][j--]);
printf("\n");
}
return ;
}