hdu 2046 骨牌铺方格(斐波那契数列)

Problem Description

在2×n的一个长方形方格中,用一个1× 2的骨牌铺满方格,输入n ,输出铺放方案的总数.
例如n=3时,为2× 3方格,骨牌的铺放方案有三种,如下图:

Input

输入数据由多行组成,每行包含一个整数n,表示该测试实例的长方形方格的规格是2×n (0<n<=50)。

Output

对于每个测试实例,请输出铺放方案的总数,每个实例的输出占一行。

Sample Input

1
3
2

Sample Output

1
3
2
解:乍一看以为是状压dp,仔细看发现行数只有2,那么对于n列来说,所能形成的情况就是n-1的情况最后一列反竖着的。或者n-2的情况最后两列放横着的。也就是斐波那契的公式。
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <queue>
#include <deque>
#include <cmath>
#include <map>

using namespace std;
typedef long long ll;

const double inf=1e20;
const int maxn=100+10;
const int mod=1e9+7;

ll a[maxn];

int main(){
    a[0]=0;
    a[1]=a[2]=1;
    for(int i=3;i<60;i++){
        a[i]=a[i-1]+a[i-2];
    }
    int n;
    while(scanf("%d",&n)!=EOF){
        printf("%lld\n",a[n+1]);
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/wz-archer/p/12450249.html