[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=1;
10     while(l<=r)
11     {
12         if(f[mid]<=x)ans=mid,l=mid+1;
13         else r=mid-1;
14     }
15     return f[ans];
16 }
17 inline ll findr(ll x)
18 {
19     int l=1,r=tot,ans=1;
20     while(l<=r)
21     {
22         if(f[mid]>=x)ans=mid,r=mid-1;
23         else l=mid+1;
24     }
25     return f[ans];
26 }
27 int solve(ll x)
28 {
29     ll l=findl(x),r=findr(x);
30     if(l==r)return 1;
31     if(x-l<=r-x)return solve(x-l)+1;
32     return solve(r-x)+1;
33 }
34 int main()
35 {
36     f[0]=f[1]=1;
37     while(f[tot-1]<=4e17)
38     f[++tot]=f[tot-1]+f[tot-2];
39     int t;scanf("%d",&t);
40     ll x;
41     while(t--)scanf("%lld",&x),
42     printf("%d\n",solve(x));
43 }

时间: 2024-10-10 16:00:00

[BZOJ2796][Poi2012]Fibonacci Representation的相关文章

【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

(记忆话搜索)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

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 It

[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

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

[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