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;
}
时间: 2024-10-21 01:13:01

POJ 2506 Tiling (递推+高精度)的相关文章

poj 2506 Tiling 递推

题目链接: http://poj.org/problem?id=2506 题目描述: 有2*1和2*2两种瓷片,问铺成2*n的图形有多少种方法? 解题思路: 利用递推思想,2*n可以由2*(n-1)的状态加上一块竖放2*1的瓷片转移得来,也可以由2*(n-2)的状态加上一块2*2的瓷片或者加上两块横放的2*1的瓷片转移得来. 可得出递推公式:dp[n] = dp[n-1] + dp[n-2]*2: ac秘诀: (1):从输出样例可以看出要用大数来表示,大概需要90位左右. (2):2*0不是零种

POJ 2506 Tiling (递推 + 大数加法模拟 )

Tiling Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7965   Accepted: 3866 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. Input Input is a sequence of lines,

[ACM] POJ 2506 Tiling (递推,大数)

Tiling Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7487   Accepted: 3661 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. Input Input is a sequence of lines,

递推 + 高精度 --- Tiling

Tiling Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7264   Accepted: 3528 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. Input Input is a sequence of lines,

poj 2506 Tiling(java解法)

题目链接:http://poj.org/problem?id=2506 本题用的java解的,因为涉及到大数问题,如果对java中的大数操作不熟悉请点这儿:链接 思路:地推公式f[i]=f[i-1]+2*f[i-2] code: import java.math.*; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner cin=new Scanner(Syst

POJ 2506 -TILING

水题,一个小模拟,规律也好找 f3 = f1 * 2 + f2; #include <iostream> #include <cstdlib> #include <cstdio> #include <cstring> #include <algorithm> const int INF = 1e8; const int N = 100; #define ll long long using namespace std; int a[251][N]

递推+高精度+找规律 UVA 10254 The Priest Mathematician

题目传送门 1 /* 2 题意:汉诺塔问题变形,多了第四个盘子可以放前k个塔,然后n-k个是经典的汉诺塔问题,问最少操作次数 3 递推+高精度+找规律:f[k]表示前k放在第四个盘子,g[n-k]表示经典三个盘子,2 ^ (n - k) - 1 4 所以f[n] = min (f[k] * 2 + g[n-k]),n<=10000,所要要用高精度,另外打表能看出规律 5 */ 6 /************************************************ 7 * Auth

BZOJ 1089 严格n元树 (递推+高精度)

题解:用a[i]表<=i时有几种树满足度数要求,那么这样就可以递归了,a[i]=a[i-1]^n+1.n个节点每个有a[i-1]种情况,那么将其相乘,最后加上1,因为深度为0也算一种.那么答案就是a[n]-a[n-1].然后就是高精度的问题了,发现很久没有现码高精度没手感了,连高进度加法进位都出了些问题,需要特别注意. #include <cstdio> #include <cstring> #include <algorithm> using namespace

[luogu]P1066 2^k进制数[数学][递推][高精度]

[luogu]P1066 2^k进制数 题目描述 设r是个2^k 进制数,并满足以下条件: (1)r至少是个2位的2^k 进制数. (2)作为2^k 进制数,除最后一位外,r的每一位严格小于它右边相邻的那一位. (3)将r转换为2进制数q后,则q的总位数不超过w. 在这里,正整数k(1≤k≤9)和w(k<W≤30000)是事先给定的. 问:满足上述条件的不同的r共有多少个? 我们再从另一角度作些解释:设S是长度为w 的01字符串(即字符串S由w个“0”或“1”组成),S对应于上述条件(3)中的q