【bzoj2796】 [Poi2012]Fibonacci Representation

给出一个数字,用FIB数列各项加加减减来得到。

问最少要多少个(可以重复使用)

大概试了一下,fibonacci数列的增长是很快的,大概到了90+项就超过了题目范围……

所以每次找一个最近的fibonacci数试一下就好,实测跑得飞快。

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 ll f[100],n,m;
 5 inline ll read(){
 6     ll f=1,x=0;char ch;
 7     do{ch=getchar();if(ch=='-')f=-1;}while(ch<'0'||ch>'9');
 8     do{x=x*10+ch-'0';ch=getchar();}while(ch>='0'&&ch<='9');
 9     return f*x;
10 }
11 int work(ll x){
12     if(!x)return x;int i;
13     for(i=1;f[i]<x;i++);
14     return work(min(f[i]-x,x-f[i-1]))+1;
15 }
16 int main(){
17     f[0]=1;f[1]=1;
18     for(int i=2;i<=91;i++)f[i]=f[i-1]+f[i-2];
19     int T=read();
20     while(T--){
21         n=read();
22         printf("%d\n",work(n));
23     }
24     return 0;
25 }

 

转载于:https://www.cnblogs.com/zcysky/p/6881338.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值