题目链接
http://main.edu.pl/en/archive/oi/19/roz
题目大意
给出一个数字,用FIB数列各项加加减减来得到。例如
10=5+5
19=21-2
17=13+5-1
1070=987+89-5-1
T
次询问数字
思路
显然对于一个数字
代码
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <map>
#define MAXN 11000
using namespace std;
typedef long long int LL;
LL f[MAXN];
map<LL,int>dp;
int tot=0;
int DFS(LL n)
{
if(dp[n]) return dp[n];
int p=lower_bound(f+1,f+100+1,n)-f;
if(f[p]==n) return 1;
return dp[n]=min(DFS(n-f[p-1]),DFS(f[p]-n))+1;
}
int main()
{
int T;
LL n;
scanf("%d",&T);
f[1]=f[2]=1;
tot=2;
for(int i=3;f[tot]<=2e18;i++) f[++tot]=f[i-1]+f[i-2];
while(T--)
{
scanf("%lld",&n);
printf("%d\n",DFS(n));
}
return 0;
}