POJ 2506 Tiling (递推+高精度)

题目链接click here~~

题目大意

In how many ways can you tile a 2xn rectangle by 2x1 or 2x2 tiles? 
Here is a sample tiling of a 2x17 rectangle. 

解题思路】:

(1)一个2*2的格子有三种填充方法:

两个横着放,

两个竖着放,

放一个2*2

(2)得出递推公式F[i]=F[i-1]+F[i-2]*2

然后就是套高精度模板了

代码;

/*
Author:HRW
递推+高精度!
*/
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
const int Base=1e9;
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];}
};
int compare(const BigInt &A, const BigInt &B){
     if(A.Len!=B.Len) return A.Len>B.Len ? 1:-1;
     int i;
     for(i=A.Len-1;i>=0 && A[i]==B[i];i--);
     if(i<0)return 0;
     return A[i]>B[i] ? 1:-1;
}

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;
}

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

BigInt operator*(const BigInt &A,const int &B){
     int i;
     huge Carry(0);
     BigInt R;
     for(i=0;i<A.Len||Carry>0;i++){
         if(i<A.Len) Carry+=huge(A[i])*B;
         R[i]=Carry%Base;
         Carry/=Base;
     }
     R.Len=i;
     return R;
}
istream &operator>>(istream &In,BigInt &V){
     char Ch;
     for(V=0;In>>Ch;){
         V=V*10+(Ch-'0');
         if(In.peek()<=' ') break;
     }
     return In;
}
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 fa[10000];
int main()
{
    fa[0]=fa[1]=1,fa[2]=3;
    for(int i=3;i<=250;i++)
    {
        fa[i]=fa[i-1]+fa[i-2]+fa[i-2];
    }
    unsigned long long  n;
    while(cin>>n)
    {
       cout<<fa[n]<<endl;
    }
    return 0;
}



  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值