1. 程式人生 > >HRBUST - 2186 鋪地磚(思維)

HRBUST - 2186 鋪地磚(思維)

這次我們用2 * 1的地板磚來鋪地,地是一塊3 * n的矩形,你能告訴我有多少種鋪地的方案麼?

 

Input

本題有多組測試資料,每組測試資料包含一個正整數n(0 <= n <= 40)。

Output

對於每組測試資料輸出鋪地的種類數。

Sample Input

4

5

6

Sample Output

11

0

41

題意:略,正常人能看懂。

題解:看到例子5輸出0,仔細思考,只要是奇數都是0,偶數自己畫出2的來,然後結合例子,推出公式,公式是什麼呢,看程式碼,就能懂,上程式碼:

#include <iostream>
using namespace std;
typedef long long ll;
ll a[50];
void init(){
	a[0]=1;//這個需要初始化為1,很容易想到
	a[1]=0;
	a[2]=3;
	for (int i = 3; i <= 40;i++){
		if(i&1) a[i]=0;
		else a[i]=a[i-2]*4-a[i-4];//遞推式
	}
}
int main(){
	init();
	int n;
	while(cin >> n){
		cout << a[n] << endl;
	}
	return 0;
}