Tri Tiling HDU 杭电1143 【规律题】

Problem 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
 



首先是奇数的话为面积都为奇数,方式肯定为零,不为奇数,那就是2*3作为一个小单元,有两种情况,第一是  与前面没有联系的,分开的,有三种,f(n-2)*3

第二种是与前面有连接的,2*( f(n-4) + ..... + f(2)+f(0))

f(n)=f(n-2)*3+f(n-4)*2+...+f(2)*2+f(0)*2  ----  表达式1
    然后,将上式用n-2替换得:
        f(n-2)=f(n-4)*3+f(n-6)*2+...+f(2)*2+f(0)*2  ----  表达式2
    表达式1减去表达式2得:
        f(n)=4*f(n-2)-f(n-4)

#include <stdio.h>
int a[50];
void fun()
{
	a[0]=1;a[1]=0;a[2]=3;a[3]=0;;
	for(int i=4;i<=40;++i)
	{
		a[i]=4*a[i-2]-a[i-4];
	}
}
int main()
{
	int n;
	fun();
	while(scanf("%d",&n),n!=-1)
	{
		printf("%d\n",a[n]);
	}
	return 0;
}


你可能感兴趣的:(Tri Tiling HDU 杭电1143 【规律题】)