您的位置:首页 > 其它

POJ2506 高精度+递推

2017-05-29 13:43 204 查看
2017年3月23日 | ljfcnyali

题目大意

有2×1和2×2两种规格的地板,现要拼2×n的形状,共有多少种情况。

Sample Input

2
8
12
100
200
1
2
3
4
5
2
8
12
100
200


Sample Output

3
171
2731
845100400152152934331135470251
1071292029505993517027974728227441735014801995855195223534251
1
2
3
4
5
3
171
2731
845100400152152934331135470251
1071292029505993517027974728227441735014801995855195223534251


题目分析

高精度是首先需要使用的(数据太大),不会用高精度戳这里

接下来就需要发现递推了

递推

假设我们已经铺好了2×(n-1)的情形,则要铺到2×n则只能用2×1的地板

假设我们已经铺好了2×(n-2)的情形,则要铺到2×n则可以选择1个2×2或两个2×1,故可能有三种铺法

其中要注意到第三个会与铺好2×(n-1)的情况重复,故不可取,故可以得到递推式

a[i]=2*a[i-2]+a[i-1];


AC代码

/*************************************************************************
> File Name: POJ2506.cpp
> Author: ljf-cnyali
> Mail: ljfcnyali@gmail.com
> Created Time: 2017/3/23 19:45:18
************************************************************************/

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<map>
#include<set>
#include<vector>
#include<queue>

using namespace std;

#define REP(i, a, b) for(int i = (a), _end_ = (b);i <= _end_; ++ i)
#define mem(a) memset((a), 0, sizeof(a))
#define str(a) strlen(a)

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 ans[300];

int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ans[0] = 1;
ans[1] = 1;
REP(i, 2, 250)
ans[i] = ans[i - 2] + ans[i - 1] + ans[i - 2];
int n;
while(cin >> n)
cout << ans
<< endl;
return 0;
}


本文转自:http://ljf-cnyali.cn/index.php/archives/99
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息