UVA 10359 Tiling

题意:求几种摆成2*n矩形的方法,可用的矩形有1*2,2*2的,对于摆到长度为n的情况有:1.在n-1的情况下,再放一个1*2,2.在n-2的情况下,放2个1*2,1个2*2的,大数

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int MAXN = 300;

int n,f[MAXN][MAXN],len[MAXN];

void mul(int a,int b){
    int i,c = 0,l = max(len[a],len[b]);
    for (i = 0; i < l; i++){
        f[a][i] += f[b][i]*2 + c;
        c = f[a][i] / 10;
        f[a][i] %= 10;
    }
    if (c){
        f[a][i] = c;
        l++;
    }
    len[a] = l;
}

void add(int a,int b){
    int l = max(len[a],len[b]);
    int i,c = 0;
    for (i = 0; i < l; i++){
        f[a][i] += f[b][i] + c;
        c = f[a][i] / 10;
        f[a][i] %= 10;
    }
    if (c){
        f[a][i] = c;
        l++;
    }
    len[a] = l;
}

void init(){
    memset(f,0,sizeof(f));
    memset(len,0,sizeof(len));
    f[0][0] = 1,len[0] = 1;
    f[1][0] = 1,len[1] = 1;
    f[2][0] = 3,len[2] = 1;
    for (int i = 3; i < MAXN; i++){
        mul(i,i-2);
        add(i,i-1);
    }
}

int main(){
    init();
    while (scanf("%d",&n) !=  EOF){
       for (int i = len[n]-1; i >= 0; i--)
           printf("%d",f[n][i]);
       printf("\n");
    }
    return 0;
}



你可能感兴趣的:(UVA 10359 Tiling)