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

很坑的一道高精度模板题

用dp[n]保存2×n对应结果

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

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

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

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

所以dp[n] = 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[n] << endl;
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值