POJ 2663 Tri Tiling 递推

Tri Tiling
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 8812   Accepted: 4592

Description

In how many ways can you tile a 3xn rectangle with 2x1 dominoes? 
Here is a sample tiling of a 3x12 rectangle. 

Input

Input consists of several test cases followed by a line containing -1. Each test case is a line containing an integer 0 <= n <= 30.

Output

For each test case, output one integer number giving the number of possible tilings.

Sample Input

2
8
12
-1

Sample Output

3
153
2131

Source

Waterloo local 2005.09.24

[Submit]   [Go Back]   [Status]   [Discuss]

算出前8个找规律,当n为奇数时候方案为0,当n为偶数时 f(n)=4*f(n-2)-f(n-4) n>=4

ACcode:

#pragma warning(disable:4786)//使命名长度不受限制
#pragma comment(linker, "/STACK:102400000,102400000")//手工开栈
#include <map>
#include <set>
#include <queue>
#include <cmath>
#include <stack>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#define rd(x) scanf("%d",&x)
#define rd2(x,y) scanf("%d%d",&x,&y)
#define rds(x) scanf("%s",x)
#define rdc(x) scanf("%c",&x)
#define ll long long int
#define maxn 100005
#define mod 1000000007
#define INF 0x3f3f3f3f //int 最大值
#define FOR(i,f_start,f_end) for(int i=f_start;i<=f_end;++i)
#define MT(x,i) memset(x,i,sizeof(x))
#define PI  acos(-1.0)
#define E  exp(1)
using namespace std;
int ans[maxn],n;
void init(){
    MT(ans,0);
    ans[0]=1;
    ans[2]=3;
    for(int i=4;i<33;i+=2)
        ans[i]=ans[i-2]*4-ans[i-4];
}
void doit(){
    printf("%d\n",ans[n]);
}
int main(){
    init();
    while(rd(n)&&n+1)
        doit();
    return 0;
}
/*
2
8
12
-1
*/


你可能感兴趣的:(C++,poj)