题目描述

The Fibonacci sequence is a sequence of integers, called Fibonacci numbers, defined as follows:

Fib0=0,Fib1=1,Fibn=Fibn−2+Fibn−1 for n>1Fib_{0}=0,Fib_{1}=1,Fib_{n}=Fib_{n-2}+Fib_{n-1}\ for\ n>1Fib0​=0,Fib1​=1,Fibn​=Fibn−2​+Fibn−1​ for n>1

Its initial elements are: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, ...

Byteasar investigates representations of numbers as sums or differences of Fibonacci numbers. Currently he is wondering what is the minimum representation, i.e., one with the minimum number of (not necessarily different) Fibonacci numbers, for a given positive integer kkk . For example, the numbers 10, 19, 17, and 1070 can be minimally represented using, respectively, 2, 2, 3, and 4 Fibonacci numbers as follows:

10=5+510=5+510=5+5

19=21−219=21-219=21−2

17=13+5−117=13+5-117=13+5−1

1070=987+89−5−11070=987+89-5-11070=987+89−5−1

Help Byteasar! Write a program that, for a given positive integer kkk determines the minimum number of Fibonacci numbers required to represent kkk as their sum or difference.

给一个数,问最少可以用几个斐波那契数加加减减凑出来

例如 10=5+5 19=21-2

17=13+5-1

1070=987+89-5-1

输入输出格式

输入格式:

In the first line of the standard input a single positive integer ppp is given (1≤p≤101\le p\le 101≤p≤10 ) that denotes the number of queries. The following ppp lines hold a single positive integer kkk each (1≤k≤1×10171\le k\le 1\times 10^{17}1≤k≤1×1017 ).

多组数据

输出格式:

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

输入输出样例

输入样例#1:

1
1070
输出样例#1:

4

说明

给一个数,问最少可以用几个斐波那契数加加减减凑出来

Solution:

  贪心水题,刷了那么多道斐波拉契,看到本题感觉简直水到爆了(红题难度)。

  首先由于斐波拉契数的前两项是$1,1$,所以易得对于任何整数必能写成多个斐波拉契数加减的形式。

  对于一个数$x$,我们贪心找到与$x$差值最小的斐波拉契数,将新的$x$赋为差值,每次进行这个操作,统计次数,直到$x$为$0$为止,输出次数。

  证明上述过程也很简单:由于我们知道任何整数必能写成多个斐波拉契数加减的形式,所以我们显然使$x$每次变得越小越好(即减的越多越好),因为每个斐波拉契数都等于前面两项的和,所以我们完全没必要将一步操作改为两步操作。

  举个例子:当$n=8$,答案是$1$(即$8=8$,$8$为第6项),而我们不需要将前面的$3,5$什么的记录进去,因为这样会多$1$步操作。当$n=11$,答案是$2$(即$11=8+3$或$11=13-2$),显然不用将$8$拆为更小的斐波拉契数之和,也不用将$13$拆为更小的斐波拉契数之和,这样必然会徒增次数。

  那么具体实现时,直接预处理斐波拉契数,然后对于每次询问,二分出第一个大于等于该值的位置$p$,然后第一个小于该值的值位置$p-1$,则$x=min(f[p]-x,x-f[p-1])$。

代码:

#include<bits/stdc++.h>
#define il inline
#define ll long long
using namespace std;
ll f[],n,t;
il void getans(ll x){
ll p=lower_bound(f+,f+,x)-f,q=p-,tot=;
while(x){
x=min(f[p]-x,x-f[q]);
p=lower_bound(f+,f+,x)-f;
q=p-;
tot++;
}
cout<<tot<<endl;
}
int main()
{
ios::sync_with_stdio();
cin>>n;
f[]=f[]=;
for(int i=;i<=;i++)f[i]=f[i-]+f[i-];
while(n--){
cin>>t;
getans(t);
}
}

最新文章

  1. hdu 5237 二进制
  2. Web Service和WCF的到底有什么区别
  3. Insert BLOB &amp;&amp; CLOB from PL/SQL and JDBC
  4. jquery中的json操作
  5. iOS开发:使用Block在两个界面之间传值(Block高级用法:Block传值)
  6. 关于 VS2013监视窗口的内存面板及寄存器面板
  7. UVA 11235 Frequent values(RMQ)
  8. 特殊函数(__all__)
  9. [SDOI2005]反素数
  10. python测试断言
  11. 2018-02-04 AppleScript类自然语言与非英语语法设计
  12. 多种方式判断PC端,IOS端,移动端
  13. 【转】Android Studio开发应用桌面出现两个或多个图标
  14. tensorflow里面共享变量、name_scope, variable_scope等如何理解
  15. Material Design系列第八篇——Creating Lists and Cards
  16. jquery的closest方法和parents方法的区别
  17. 从零开始学JavaScript四(数据类型)
  18. shell命令之at 执行一次性定时任务的用法
  19. 定时器应用-点击按钮,div向右移动
  20. Android 使用Scroller实现绚丽的ListView左右滑动删除Item效果

热门文章

  1. react脚手架搭建1
  2. NOIP模拟 candy
  3. Qt——信号与槽
  4. 嵌入式框架Zorb Framework搭建三:列表的实现
  5. 微信公众号--JS-SDK
  6. HyperLedger Fabric 1.4 区块链应用场景(3.1)
  7. IDEA Java Web(Spring)项目从创建到打包(war)
  8. ionic打包apkFailed to execute shell command &quot;input,keyevent,82&quot;&quot; on device: Error: adb: Command failed with exit code 137
  9. Windows 10 下如何彻底关闭 Hyper-V 服务(翻外篇)
  10. docker windows container的一些注意点