P3539 [POI2012]ROZ-Fibonacci Representation

题目描述

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 #include<bits/stdc++.h>
 2 #define il inline
 3 #define ll long long
 4 using namespace std;
 5 ll f[100],n,t;
 6 il void getans(ll x){
 7     ll p=lower_bound(f+1,f+93,x)-f,q=p-1,tot=0;
 8     while(x){
 9         x=min(f[p]-x,x-f[q]);
10         p=lower_bound(f+1,f+93,x)-f;
11         q=p-1;
12         tot++;
13     }
14     cout<<tot<<endl;
15 }
16 int main()
17 {
18     ios::sync_with_stdio(0);
19     cin>>n;
20     f[1]=f[2]=1;
21     for(int i=3;i<=100;i++)f[i]=f[i-1]+f[i-2];
22     while(n--){
23         cin>>t;
24         getans(t);
25     }
26 }

原文地址:https://www.cnblogs.com/five20/p/8810419.html

时间: 2024-10-30 11:11:23

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

(记忆话搜索)POI Fibonacci Representation

Fibonacci Representation Memory limit: 64 MB The Fibonacci sequence is a sequence of integers, called Fibonacci numbers, defined as follows: Its initial elements are: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, ... Byteasar investigates representations of n

【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

[BZOJ2796][Poi2012]Fibonacci Representation

由于是斐波那契数列,所以$x_i+x_j<=x_k,i<j<k$ 所以猜测可以贪心选择两边近的数处理. 1 #include<cstdio> 2 #include<algorithm> 3 #define ll long long 4 #define mid (l+r>>1) 5 using namespace std; 6 ll f[505],tot=1; 7 inline ll findl(ll x) 8 { 9 int l=1,r=tot,ans

POI2012 (持续更新中)

Distance Well Vouchers Cloakroom A Horrible Poem Rendezvous Fibonacci Representation Squarks

三道题目

Q1. String to Integer implement a similar atoi function to convert a string to integer(int type) hint:the string may start with continuous whitespaces, you should ignore them. the string may end with continuous invalid characters, you should ignore t

poj 2116 Death to Binary? 模拟

Death to Binary? Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1707   Accepted: 529 Description The group of Absurd Calculation Maniacs has discovered a great new way how to count. Instead of using the ordinary decadic numbers, they us

[POI2012]ROZ-Fibonacci Representation (贪心)

大意: 给定数$n$, 求将$n$划分为最少的斐波那契数的和或差. 每次取相邻$n$的斐波那契数一定最优, 考虑证明. 结论1:存在一个最优解,使得每个斐波那契数使用不超过1次.(考虑$2F_n=F_{n-2}+F_{n+1}$) 结论2:存在一个最优解,使得同号数不相邻, 异号数间隔$\ge 2$. 根据结论1和2, 假设最优解所选最大斐波那契数为$F_k$, 那么 $n$的下界为$F_k-F_{k-3}-F_{k-5}-...$, $k$为奇时为$F_{k-1}$, $k$为偶时为$F_{k

[Coding Made Simple] Number without consecutive 1s in binary representation

Given a number n, find the total number of numbers from 0 to 2^n - 1 which do not have consecutive 1s in their binary representation. Solution.  This problem is the equivalence of fibonacci sequence. For a given n, f(n) = f(n - 1) + f(n - 2). 1 publi

【转】Fibonacci 斐波纳契堆优化 Dijkstra 最短路径算法

话不多说,拿来主义,直接上代码! PS:打印最短路径我还不晓得怎么加,如有哪位大神知道,还请mark一下! 1 /*********************************************************************** 2 * File: FibonacciHeap.java 3 * Author: Keith Schwarz ([email protected]) 4 * 5 * An implementation of a priority queue