警示后人!十年OI一场空,不开longlong见祖宗
查看原帖
警示后人!十年OI一场空,不开longlong见祖宗
778708
Jiziyang520楼主2024/9/7 11:42

在数组定义处和函数定义处都要开longlong,因为计算规模大。同时不要忘了记忆化。


#include <bits/stdc++.h>
using namespace std;
long long int lst[1000];

long long int solve(int n)
{
	if (lst[n])
	{
//		cout << 114514;
		return lst[n];
	}
	else if (n == 1 || n == 2)
	{
		return lst[n] = n;
	}
	else if (n == 3)
	{
		return lst[n] = 4;
	}
	else
	{
		return lst[n] = (solve(n - 1) + solve(n - 2) + solve(n - 3));
	}
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int n;
	cin >> n;
	cout << solve(n);
	return 0;
}
2024/9/7 11:42
加载中...