题目描述

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);
}
}

P3539 [POI2012]ROZ-Fibonacci Representation的更多相关文章

  1. 洛谷 P3539 [POI2012]ROZ-Fibonacci Representation 解题报告

    P3539 [POI2012]ROZ-Fibonacci Representation 题意:给一个数,问最少可以用几个斐波那契数加加减减凑出来 多组数据10 数据范围1e17 第一次瞬间yy出做法, ...

  2. BZOJ2796[Poi2012]Fibonacci Representation——贪心+二分查找

    题目描述 给出一个正整数x,问x最少能由多少个Fibonacci数加减算出. 例如1070=987+89-5-1,因此x=1070时答案是4. 输入 第一行一个正整数q (q<=10),表示有q ...

  3. 【bzoj2796】 [Poi2012]Fibonacci Representation

    给出一个数字,用FIB数列各项加加减减来得到. 问最少要多少个(可以重复使用) 大概试了一下,fibonacci数列的增长是很快的,大概到了90+项就超过了题目范围…… 所以每次找一个最近的fibon ...

  4. [BZOJ2796][Poi2012]Fibonacci Representation

    由于是斐波那契数列,所以$x_i+x_j<=x_k,i<j<k$ 所以猜测可以贪心选择两边近的数处理. #include<cstdio> #include<algo ...

  5. 洛谷P3539 [POI2012] ROZ-Fibonacci Representation

    题目传送门 转载自:five20,转载请注明出处 本来看到这题,蒟蒻是真心没有把握的,还是five20大佬巨orz 首先由于斐波拉契数的前两项是1,1 ,所以易得对于任何整数必能写成多个斐波拉契数加减 ...

  6. BZOJ [Poi2012]Fibonacci Representation

    找最近的数 记忆化 (我也不知道为什么对的) #include<cstdio> #include<algorithm> #include<map> using na ...

  7. LUOGU P3539 [POI2012]ROZ-Fibonacci Representation

    传送门 解题思路 打了个表发现每次x只会被比x大的第一个fab或比x小的第一个fab表示,就直接写了个爆搜骗分,结果过了.. 代码 #include<iostream> #include& ...

  8. bzoj 2796: [Poi2012]Fibonacci Representation

    结论貌似是,,,肯定只有没有重复的数字.http://hzwer.com/6426.html 一开始猜的是贪心,感觉也是可以的啊...(想想都有道理,然而看到是神奇的(dp类)记忆化搜索,直接虚的不敢 ...

  9. POI2012题解

    POI2012题解 这次的完整的\(17\)道题哟. [BZOJ2788][Poi2012]Festival 很显然可以差分约束建图.这里问的是变量最多有多少种不同的取值. 我们知道,在同一个强连通分 ...

随机推荐

  1. Java语言利用Collections.sort对Map,List排序

    1.main方法包含TreeMap排序1,TreeMap排序2,HashMap排序,List<Integer>排序,List<Bean>排序,List<Map>排序 ...

  2. python pandas库——pivot使用心得

    python pandas库——pivot使用心得 2017年12月14日 17:07:06 阅读数:364 最近在做基于python的数据分析工作,引用第三方数据分析库——pandas(versio ...

  3. CodeTimer 代码性能计数器

    收集整理老赵 的”CodeTimer“. 用于测试代码性能.详见可参考 老赵原文 代码如下: using System; using System.Diagnostics; using System. ...

  4. php sign签名实例

    1:实现签名代码: /** * 签名生成算法 * @param array $params API调用的请求参数集合的关联数组,不包含sign参数 * @param string $secret 签名 ...

  5. css常用样式属性详细介绍

    对于初学css的来说,肯定会觉得这么多样式不好记,而且记住了也容易忘,其实刚开始我们不用去记这么多的样式,确实是记了也会忘,刚开始只需记住一些常用的就可以了,然后在慢慢的使用过程当中接触并学习一些高级 ...

  6. Rmarkdown:输出html设置

    在Rstudio中可自行更改主题样式 --- title: "题目" author: "name" date: "`r format(Sys.time ...

  7. ABAP CDS ON HANA-(4)ヘッダー行編集

    Explicit Name List use in CDS We create a simple  CDS View like below. @AbapCatalog.sqlViewName: ‘ZS ...

  8. 【转】Android开发之ListView+EditText-要命的焦点和软键盘问题解决办法

    Android开发之ListView+EditText-要命的焦点和软键盘问题解决办法 [原文链接] 这篇文章完美的解决了我几个月没结论的bug... 感谢热爱分享的技术达人~ 我是怎么走进这个大坑的 ...

  9. flask与javascript及ajax

    flask与javascript及ajax 1.      flask+js 1.1.    最简单的 最简单的元素信息改变. {% block content %} <h1>我的第一张网 ...

  10. LeetCode:9. Palindromic Number(Medium)

    原题链接:https://leetcode.com/problems/palindrome-number/description/ 1. 题目要求:判断一个int类型整数是否是回文,空间复杂度O(1) ...