CDOJ 1139 菲波拉契数制升级版 dp

唔,做法的话,当年的PPT上讲的挺详细的,回头再来补一发吧


贴代码 ,好久没见的1A了:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
using namespace std;
#define ll long long
#define maxn 1000000000000000000
ll Fib[88];
int A[90], Num[88];
int dp[90][2];
inline void MakeFib()
{
	Fib[0] = 0;
	Fib[1] = 1;
	Fib[2] = 2;
	for (int i = 3; i < 88; i++)
	{
		Fib[i] = Fib[i - 1] + Fib[i - 2];
		if (Fib[i]>maxn)
			break;
	}
}
int main()
{
	int T;
	ll n;
	MakeFib();
	scanf("%d", &T);
	while (T--)
	{
		scanf("%lld", &n);
		int l = 0;
		for (int i = 87; i > 0; --i)
		{
			if (n >= Fib[i])
			{
				Num[l++] = i;
				n -= Fib[i];
			}
		}
		Num[l] = 0;
		for (int i = l - 1; i >= 0; --i)
		{
			A[l - 1 - i] = Num[i] - Num[i + 1] - 1;
		}
		dp[0][0] = A[0] / 2, dp[0][1] = 1;
		for (int i = 1; i < l; ++i)
		{
			dp[i][1] = dp[i - 1][1] + dp[i - 1][0];
			dp[i][0] = dp[i - 1][1] * (A[i] / 2) + dp[i - 1][0] * ((A[i] + 1) / 2);
		}
		printf("%d\n", dp[l - 1][0] + dp[l - 1][1]);
	}

	return 0;
}

你可能感兴趣的:(CDOJ 1139 菲波拉契数制升级版 dp)