【POI2012】【BZOJ2796】Fibonacci Representation
来源:互联网 发布:windowsrt软件 编辑:程序博客网 时间:2024/06/05 08:36
Description
Fib数列0,1,1,2,3,5,8,13,21。
给出一个数字,用FIB数列各项加加减减来得到。例如
10=5+5
19=21-2
17=13+5-1
1070=987+89-5-1
Input
In the first line of the standard input a single positive integer is given (1 <=P<=10) that denotes the number of queries. The following lines hold a single positive integer K each 1<=K<=10^17.
Output
For each query your program should print on the standard output the minimum number of Fibonacci numbers needed to represent the number k as their sum or difference.
Sample Input
1
1070
Sample Output
4
HINT
Source
这种题都是一样的,考虑递归然后Fib数分解就行了
只不过多了个减号
#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>#include<map>#define LL long long#define MAXINT 1LL<<62using namespace std;int T;LL n,fib[100]={1,1};map<LL,int> f;int get_ans(LL x){ if (f[x]) return f[x]; int pos=lower_bound(fib,fib+88,x)-fib; if (fib[pos]==x) return 1; return f[x]=min(get_ans(x-fib[pos-1]),get_ans(fib[pos]-x))+1;}int main(){ scanf("%d",&T); for (int i=2;i<=89;i++) fib[i]=fib[i-1]+fib[i-2]; while (T--) cin>>n,printf("%d\n",get_ans(n));}
0 0
- 【POI2012】【BZOJ2796】Fibonacci Representation
- BZOJ2796: [Poi2012]Fibonacci Representation
- BZOJ2796 [Poi2012]Fibonacci Representation
- BZOJ2796/POI2012 Fibonacci Representation
- BZOJ2796: [Poi2012]Fibonacci Representation
- 【bzoj2796】 [Poi2012]Fibonacci Representation
- bzoj2796 [Poi2012]Fibonacci Representation
- bzoj2796 [Poi2012]Fibonacci Representation 贪心
- 【BZOJ2796】【贪心】 Fibonacci Representation题解
- 2796: [Poi2012]Fibonacci Representation
- [BZOJ 2796]POI2012 Fibonacci Representation
- BZOJ 2796: [Poi2012]Fibonacci Representation
- 2796: [Poi2012]Fibonacci Representation 思路题 map+记忆化搜索
- bzoj 2796: [Poi2012]Fibonacci Representation 记忆化搜索
- [POI 2012]Fibonacci Representation(记忆化DFS)
- POI2012题解
- [Poi2012]Festival
- Fibonacci
- 二分查找算法
- C#面向对象编程:构造函数
- git和Github常用操作
- python encode和decode函数说明
- Android关于Log不能打印的问题
- 【POI2012】【BZOJ2796】Fibonacci Representation
- JAVA项目出现红色叹号
- 配置urlrewrite不生效
- Android打电话过程
- Android生成模糊化背景的图片
- rtmp中文手册
- Android--Telephony
- Linux swap分区
- Xcode7.0上面使用http进行请求错误