码迷,mamicode.com
首页 > 其他好文 > 详细

UVa 10918 - Tri Tiling

时间:2015-04-16 01:31:58      阅读:312      评论:0      收藏:0      [点我收藏+]

标签:

题目:给你一个3*n的地面,用1*2的地板砖铺满,问有几种方法。

分析:组合数学,动态规划。首先找到地推关系。

            只有偶数才有意义,奇数的总面积为奇数一定不成立。一次我们以两列为一个单位考察。

            如果,最后2列构成一个整体的部分(3种情况,2*3的3中实现),则有3*f(n-2)种方法;

            如果,最后4列构成一个整体的部分(2种情况,上下翻转实现),则有2*f(n-4)种方法;

            ...

            因此,有递推公式: f(n)= 3*f(n-2)+ 2(f(n-4)+ f(n-6)+ ...);

            整理化简可以得到:f(n)- 4*f(n-2)+ f(n-4) = 0;

            可以利用动态规划或者母函数求解。

#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>

using namespace std;

int f[32];

int main()
{
	memset(f, 0, sizeof(f));
	f[0] = 1;f[2] = 3;
	for (int i = 4; i < 31; i += 2)
		f[i] = 4*f[i-2] - f[i-4];
	
	int n;
	while (cin >> n && n >= 0) {
		cout << f[n] << endl;
		//母函数 
		//cout << (int)(((3+sqrt(3.0))*pow(2+sqrt(3.0), n/2)+(3-sqrt(3.0))*pow(2-sqrt(3.0), n/2))/6+0.01) << endl;
	}
	
    return 0;
}


UVa 10918 - Tri Tiling

标签:

原文地址:http://blog.csdn.net/mobius_strip/article/details/45066763

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!