POJ2506(递推+高精度)

题目大意:2xN放2x1和2x2的方块多少种放法。

分析:a[n]=a[n-1]+a[n-2]*2

          2xN相当于2x(n-1)加一个竖着的方块,和2x(n-2)加两个竖着的,两个横着的,一个大方块,其中放两个竖着的重复。

代码

#include <iostream>
#include <cstdio>
#include <string>
using namespace std;
string s[255];
string add(string x, string y)
{
	if (x.length() < y.length())
		swap(x, y);
	int i, j;
	for (i = x.length() - 1, j = y.length() - 1; i >= 0; i--, j--)
	{
		x[i] = x[i] + (j >= 0 ? y[j] - '0' : 0);
		if (x[i] - '0' >= 10)
		{
			x[i] = (x[i] - '0') % 10 + '0';
			if (i)
				x[i - 1]++;
			else
				x = '1' + x;
		}
	}
	return x;
}
int main()
{
	s[0] ="1";
	s[1] = "1";
	s[2] = "3";
	for (int i = 3; i <= 250; i++)
		s[i] = add(add(s[i-1],s[i-2]),s[i-2]);
	int m;
	while (scanf("%d", &m) != EOF)
		cout<<s[m]<<endl;
	return 0;
}


posted @ 2016-04-25 12:57  Nickqiao  阅读(206)  评论(0编辑  收藏  举报