您的位置:首页 > 其它

poj 2506 Tiling(数学:递推+高精度)

2014-08-23 21:32 302 查看
很坑的一道高精度模板题

用dp
保存2×n对应结果

对于2×n的矩形,分析最后两列

如果最后一列放1×2,则对应dp[n-1]种放法

如果最后两列放2×1,则对应dp[n-2]种放法

如果最后两列放2×2,则对应dp[n-2]种放法

所以dp
= dp[n-1]+dp[n-2]+dp[n-2]

比斐波那契增长的还快,所以要用高精度或者java大数

代码如下:

#include <cmath>
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#define MAXN 250
#define esp 1e-9
using namespace std;

const int Base=1000000000;
const int Capacity=100;
typedef long long huge;

struct BigInt{
int Len;
int Data[Capacity];
BigInt() : Len(0) {}
BigInt (const BigInt &V) : Len(V.Len) { memcpy (Data, V.Data, Len*sizeof*Data);}
BigInt(int V) : Len(0) {for(;V>0;V/=Base) Data[Len++]=V%Base;}
BigInt &operator=(const BigInt &V) {Len=V.Len; memcpy(Data, V.Data, Len*sizeof*Data); return *this;}
int &operator[] (int Index) {return Data[Index];}
int operator[] (int Index) const {return Data[Index];}
};

BigInt operator+(const BigInt &A,const BigInt &B){
int i,Carry(0);
BigInt R;
for(i=0;i<A.Len||i<B.Len||Carry>0;i++){
if(i<A.Len) Carry+=A[i];
if(i<B.Len) Carry+=B[i];
R[i]=Carry%Base;
Carry/=Base;
}
R.Len=i;
return R;
}

ostream &operator<<(ostream &Out,const BigInt &V){
int i;
Out<<(V.Len==0 ? 0:V[V.Len-1]);
for(i=V.Len-2;i>=0;i--) for(int j=Base/10;j>0;j/=10) Out<<V[i]/j%10;
return Out;
}

BigInt dp[MAXN];

int main(void) {
dp[0] = dp[1] = 1.0;
for(int i=2; i<=MAXN; ++i) {
dp[i] = (dp[i-1]+dp[i-2]+dp[i-2]);
}
int n;
while(scanf("%d", &n) != EOF) {
cout << dp
<< endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: