[BZOJ2796][Poi2012]Fibonacci Representation
由于是斐波那契数列,所以$x_i+x_j<=x_k,i<j<k$
所以猜测可以贪心选择两边近的数处理。
#include<cstdio>
#include<algorithm>
#define ll long long
#define mid (l+r>>1)
using namespace std;
ll f[],tot=;
inline ll findl(ll x)
{
int l=,r=tot,ans=;
while(l<=r)
{
if(f[mid]<=x)ans=mid,l=mid+;
else r=mid-;
}
return f[ans];
}
inline ll findr(ll x)
{
int l=,r=tot,ans=;
while(l<=r)
{
if(f[mid]>=x)ans=mid,r=mid-;
else l=mid+;
}
return f[ans];
}
int solve(ll x)
{
ll l=findl(x),r=findr(x);
if(l==r)return ;
if(x-l<=r-x)return solve(x-l)+;
return solve(r-x)+;
}
int main()
{
f[]=f[]=;
while(f[tot-]<=4e17)
f[++tot]=f[tot-]+f[tot-];
int t;scanf("%d",&t);
ll x;
while(t--)scanf("%lld",&x),
printf("%d\n",solve(x));
}
[BZOJ2796][Poi2012]Fibonacci Representation的更多相关文章
- BZOJ2796[Poi2012]Fibonacci Representation——贪心+二分查找
题目描述 给出一个正整数x,问x最少能由多少个Fibonacci数加减算出. 例如1070=987+89-5-1,因此x=1070时答案是4. 输入 第一行一个正整数q (q<=10),表示有q ...
- 【bzoj2796】 [Poi2012]Fibonacci Representation
给出一个数字,用FIB数列各项加加减减来得到. 问最少要多少个(可以重复使用) 大概试了一下,fibonacci数列的增长是很快的,大概到了90+项就超过了题目范围…… 所以每次找一个最近的fibon ...
- BZOJ [Poi2012]Fibonacci Representation
找最近的数 记忆化 (我也不知道为什么对的) #include<cstdio> #include<algorithm> #include<map> using na ...
- bzoj 2796: [Poi2012]Fibonacci Representation
结论貌似是,,,肯定只有没有重复的数字.http://hzwer.com/6426.html 一开始猜的是贪心,感觉也是可以的啊...(想想都有道理,然而看到是神奇的(dp类)记忆化搜索,直接虚的不敢 ...
- 洛谷 P3539 [POI2012]ROZ-Fibonacci Representation 解题报告
P3539 [POI2012]ROZ-Fibonacci Representation 题意:给一个数,问最少可以用几个斐波那契数加加减减凑出来 多组数据10 数据范围1e17 第一次瞬间yy出做法, ...
- P3539 [POI2012]ROZ-Fibonacci Representation
题目描述 The Fibonacci sequence is a sequence of integers, called Fibonacci numbers, defined as follows: ...
- [POI2012]ROZ-Fibonacci Representation (数学)
大意:给定n, 求至少要多少个斐波那契数相加减后能得到n (可以重复, 重复的算多次) 假设$dp(x)$为$x$的最小划分, 有$dp(x)=dp(x-F_k)+1$, 其中$F_k$为最接近$x ...
- 洛谷P3539 [POI2012] ROZ-Fibonacci Representation
题目传送门 转载自:five20,转载请注明出处 本来看到这题,蒟蒻是真心没有把握的,还是five20大佬巨orz 首先由于斐波拉契数的前两项是1,1 ,所以易得对于任何整数必能写成多个斐波拉契数加减 ...
- [POI2012]ROZ-Fibonacci Representation (贪心)
大意: 给定数$n$, 求将$n$划分为最少的斐波那契数的和或差. 每次取相邻$n$的斐波那契数一定最优, 考虑证明. 结论1:存在一个最优解,使得每个斐波那契数使用不超过1次.(考虑$2F_n=F_ ...
随机推荐
- C# 使用Conditional特性而不是#if条件编译
概述 #if/#endif 语句常用来基于同一份源码生成不同的编译结果,其中最常见的就是debug版和release版.但是这些工具在实际应用中并不是非常友好,因为它们容易被滥用,其代码页进而难以理解 ...
- 【翻译一】java-并发
Lesson: Concurrency Computer users take it for granted that their systems can do more than one thing ...
- soapUI 使用Property
DRY 原则是一个比较普适的东西,在使用soapUI测试的时候,为了make life easy,我们必须要使用Property来集中化一些设置. 比如说从dev->test->uat 的 ...
- [Mobile] 手机浏览器输入框-数字输入框
手机浏览器的输入框,一直都是以web的方式进行开发的,没有关注到用户体验,领导提出了输入框要弹出数字输入框,想来应该有这种技术能实现. 搜索之后发现可以使用type="number&qu ...
- ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statemen
转自:http://www.cnblogs.com/iosdev/archive/2013/07/15/3190431.html mysql 配置文件目录:/etc/my.cnf root 密码为空的 ...
- C语言里面捕获错误机制
在C语言中异常处理一般有这么几种方式: 1.使用标准C库提供了abort()和exit()两个函数,它们可以强行终止程序的运行,其声明处于<stdlib.h>头文件中. 2.使用asser ...
- linux中预留的$变量
$0表示bash脚本的文件名 $1表示第一个参数 $*表示参数列表$0, $1, $2… $@表示"$1"/"$2"...每个变量都是独立的,用双引号括起来 $ ...
- VIM学习笔记
参考: http://linux.chinaunix.net/techdoc/beginner/2009/12/20/1150108.shtml VIM命令大全 光标控制命令 命令 ...
- 4.0 和4.5 app 和generic,xaml的问题
4.0里面不支持Generic.xaml里面 <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source=& ...
- hdu1114 完全背包
题意:给出钱罐的重量,然后是每种钱的价值和重量,问钱罐里最少可能有多少钱. 完全背包. 代码: #include<iostream> #include<cstdio> #inc ...