Tri Tiling hdu1143__ Recursion

Source: Internet
Author: User
Tri Tiling Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 2301 accepted Submission (s): 1314


Problem Description in many ways can, tile a 3xn rectangle with 2x1 dominoes? This is a sample tiling of 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

When n>=4, there are two special combinations that need special consideration. F (N) ' =3*f (n-2) +2f (n-4) +2+ ....     2*f (0); So you can simplify this equation F (n-2) =3*f (n-4) +2*f (n-6) +2*f (n-8) + ... 2*f (0);

F (N)-f (n-2) =3*f (n-2)-F (n-4);

The value of such an operation must exceed the int type range so that the __int64 and long long types are the same, and the OJ system defaults to the A[0]=1;

#include <stdio.h>
#define LIM
long Long  a[lim]={1,0,3,0,11};
int main ()
{

	int n,i,j;
	for (i=5;i<lim;i++)
	{
		a[i]=4*a[i-2]-a[i-4];
	}
	while (scanf ("%d", &n))
	{
		if (n==-1) break
		;
		printf ("%lld\n", A[n]);
	return 0;
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.