HDU 1143 Tri Tiling

Source: Internet
Author: User

Links: http://acm.hdu.edu.cn/showproblem.php?pid=1143

Tri tilingtime limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others) total submission (s): 2799 Accept Ed Submission (s): 1585
problem Description
In what 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 was a line containing an integer 0≤n≤30.

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

Sample Input
2
8
12
-1

Sample Output
3
153
2131

Source

University of Waterloo Local Contest 2005.09.24

Recommend

Eddy

The rectangular squares of--3*n, covered with 2*1 dominoes. Q: Given an n, the total number of scenarios is solved.

Idea-obviously this is a recursive topic. It is obvious that when n is odd, there is no solution. and f (0) =1,f (2) = 3, now we examine the last two columns: if the last two columns are covered, there are 3 scenarios, so the number of scenarios is 3*f (n-2), and if the last two columns are not covered, then there must be four columns in the back, with 2 schemes, so the number of scenarios is f (n-4). And so on, when N>=4, f (n) =3*f (n-2) +2*f (n-4) +2*f (n-6) + +2*f (0). Make 2*m=n, then f (m) =3*f (m-1) +2*f (m-2) + +2*f (0), F (m-1) =3*f (m-2) +2*f (m-3) + +2*f (0), subtract and collate two, F (m) =4*f (M-1)-F (m-2). Thus, f (n) =4*f (n-2)-F (n-4), n>=4, and N is an even number. And because N is the maximum of 30, the direct recursion can be.

Complexity analysis--time complexity: O (n), spatial complexity: O (n)

Attach the AC code:


#include <iostream> #include <cstdio> #include <string> #include <cmath> #include <iomanip > #include <ctime> #include <climits> #include <cstdlib> #include <cstring> #include < algorithm>using namespace std;typedef unsigned int ui;typedef long long ll;typedef unsigned long long ull;typedef long Double Ld;const double PI = 3.14159265;const double E = 2.71828182846; LL num[31] = {1, 0, 3, 0};void init (); int main () {Ios::sync_with_stdio (false); Init (); Short N;while (CIN >> N &&am P n! =-1) {cout << num[n] << Endl;} return 0;} void Init () {for (int i=4; i<31; i++) num[i] = 4*num[i-2]-num[i-4];}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU 1143 Tri Tiling

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.