高精度算法-高精度加法

发布时间 2023-03-22 21:16:24作者: 什么时候才能不困

为什么要用高精度

因为有的题目的数据很大,超出long long的范围,所以我们需要用高精度来计算:

首先是高精度加法:

高精度加法就是仿照我们竖式加法进行操作,逐位相加,注意进位!!!

题目传送门

Tiling
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 16595   Accepted: 7263

Description

In how many ways can you tile a 2xn rectangle by 2x1 or 2x2 tiles?
Here is a sample tiling of a 2x17 rectangle.

Input

Input is a sequence of lines, each line containing an integer number 0 <= n <= 250.

Output

For each line of input, output one integer number in a separate line giving the number of possible tilings of a 2xn rectangle.

Sample Input

2
8
12
100
200

Sample Output

3
171
2731
845100400152152934331135470251
1071292029505993517027974728227441735014801995855195223534251

题解:

首先,找到递推关系式
f(n)=f(n-1)+2*f(n-1);
递推解释:
当第n-1个确定的时候,那么第n个只有一种放法,所以是f(n-1);
当前n-2个确定的时候,那么剩下两个有两种放法:和为一块(2*2)或者分为两块横着放(上下垒起来),所以是f(n-2)*2;
递推关系式出来了这题解了一半了,那么剩下的就是写代码了,看着数据就知道,要用高精度了,long long是存不了的。
所以,上代码:
代码(点开)
 #include<iostream>
#include<algorithm>
using namespace std;
const int N = 20;
int ans[N][N];
int main()
{
	int n;
	cin >> n;
	for (int i = 1; i <= n; i++)
	{
		ans[i][0] = 1;
	}
	ans[0][1] = 1;
	for (int i = 2; i <= n; i++)
	{
		for (int j = i - 1; j >= 1; j--)
		{
			ans[j][i - j] = ans[j - 1][i - j] + ans[j + 1][i - j - 1];
		}
		ans[0][i] = ans[1][i - 1];
	}
	cout << ans[0][n];
	return 0;
}
血的教训,这题有一个数据超出250了!!气死了,debug半天!!!