[BZOJ 2796]POI2012 Fibonacci Representation

来源:互联网 发布:centos 7和ubuntu 编辑:程序博客网 时间:2024/05/17 01:21

大概就是说假设 F(x)<=a<=F(x+1),答案d[n]=min( d[a-F(x)] , d[F(x+1)-a] )+1

记忆化一下就行了。

证明吗。。。这个坑再填吧。。。敲打

#include <map> #include <cstdio>#include <cstring>#include <algorithm>using namespace std;typedef long long LL;LL f[110],n;int Q,i;map <LL,int> hash;int dfs(LL x){  if (hash[x]!=0) return hash[x];  int t=lower_bound(f+1,f+90,x)-f;  if (f[t]==x) return 1;  hash[x]=min(dfs(x-f[t-1]),dfs(f[t]-x))+1;  return hash[x];}int main(){  freopen("roz.in","r",stdin);  freopen("roz.out","w",stdout);  f[0] = f[1] = 1;  for (i=2;i<=90;i++)    f[i]=f[i-1]+f[i-2];  scanf("%d",&Q);  while (Q--){  scanf("%I64d\n",&n);  hash.clear();  printf("%d\n", dfs(n) );  }   return 0;}


0 0
原创粉丝点击