【BZOJ2796】【贪心】 Fibonacci Representation题解

9 篇文章 1 订阅

Description

Fib数列0,1,1,2,3,5,8,13,21。

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

10=5+5

19=21-2

17=13+5-1

1070=987+89-5-1

Input

In the first line of the standard input a single positive integer is given (1 <=P<=10) that denotes the number of queries. The following lines hold a single positive integer K each 1<=K<=10^17.
Output

For each query your program should print on the standard output the minimum number of Fibonacci numbers needed to represent the number k as their sum or difference.
Sample Input

1
1070
Sample Output

4

#include<iostream>
#include<cstdio>
#define LL long long
#ifdef WIN32
#define AUTO "%I64d"
#else
#define AUTO "%lld"
#endif

using namespace std;

const int maxn = 1010;
LL t,x;
LL f[maxn];

int cal(LL x){  
    if(!x) return 0;
    int i;
    for(i = 1; f[i] < x; i++);
    return cal(min(x-f[i-1], f[i]-x))+1;
}

int main() {
    f[0] = f[1] = 1;
    for(int i = 2; i <= 91; i++) f[i] = f[i-1]+f[i-2];
    scanf(AUTO,&t);
    while(t--) scanf(AUTO,&x), printf("%d\n",cal(x));
    return 0;  
}  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值