Tri Tiling HDU 杭電1143 【規律題】
阿新 • • 發佈:2019-02-20
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
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; }