Problem Description

Fibonacci numbers are well-known as follow:

Now given an integer N, please find out whether N can be represented as the sum of several Fibonacci numbers in such a way that the sum does not include any two consecutive Fibonacci numbers.

Input

Multiple test cases, the first line is an integer T (T<=10000), indicating the number of test cases.

Each test case is a line with an integer N (1<=N<=109).

Output

One line per case. If the answer don’t exist, output “-1” (without quotes). Otherwise, your answer should be formatted as “N=f1+f2+…+fn”. N indicates the given number and f1, f2, … , fn indicating the Fibonacci numbers in ascending order. If there are multiple ways, you can output any of them.

Sample Input

4
5
6
7
100

Sample Output

5=5
6=1+5
7=2+5
100=3+8+89 题解:贪心求解,此题需要注意的是相邻的两个数不能选择;
#include<string.h>
#include<stdio.h>
#include<math.h>
using namespace std;
long long int arr[]={,,};
int main()
{ long long int i,j,a,b[],k,m,l,kk;
for(i=;i<=;i++)
arr[i]=arr[i-]+arr[i-];//先打下表
long long int count=;
while(scanf("%lld",&m)!=-)
{
for(kk=;kk<m;kk++){
scanf("%lld",&a);
k=;
l=;count=;int pp=;
for(i=;i>=;i-=)
{
pp=;
count+=arr[i];
if(count>a)
{
count-=arr[i];
pp=;
}
else if(count==a)
{
k=;
b[l++]=arr[i];
break;
}
else if(count<a)
b[l++]=arr[i];
if(pp==)
i++;//判断一下上一个数是否选择,若没选,下一个数可以选择
}
if(k==)
{
printf("%d=",a);
for(i=l-;i>=;i--)
{ if(i==)
printf("%lld\n",b[i]);
else
printf("%lld+",b[i]);
}
}
else
printf("-1\n");
}
}
return ;
}

HDU ACM Fibonacci的更多相关文章

  1. HDU 3117 Fibonacci Numbers(围绕四个租赁斐波那契,通过计++乘坐高速动力矩阵)

    HDU 3117 Fibonacci Numbers(斐波那契前后四位,打表+取对+矩阵高速幂) ACM 题目地址:HDU 3117 Fibonacci Numbers 题意:  求第n个斐波那契数的 ...

  2. hdu acm 1028 数字拆分Ignatius and the Princess III

    Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K ...

  3. hdu 3117 Fibonacci Numbers 矩阵快速幂+公式

    斐波那契数列后四位可以用快速幂取模(模10000)算出.前四位要用公式推 HDU 3117 Fibonacci Numbers(矩阵快速幂+公式) f(n)=(((1+√5)/2)^n+((1-√5) ...

  4. ACM HDU 1021 Fibonacci Again

    #include<iostream> using namespace std; int main() { int n; while(cin>>n) { if((n+1)%4== ...

  5. hdu 4786 Fibonacci Tree (2013ACMICPC 成都站 F)

    http://acm.hdu.edu.cn/showproblem.php?pid=4786 Fibonacci Tree Time Limit: 4000/2000 MS (Java/Others) ...

  6. hdu 1021 Fibonacci Again(找规律)

    http://acm.hdu.edu.cn/showproblem.php?pid=1021 Fibonacci Again Time Limit: 2000/1000 MS (Java/Others ...

  7. HDU 4786 Fibonacci Tree 最小生成树

    Fibonacci Tree 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=4786 Description Coach Pang is intere ...

  8. hdu 1021 Fibonacci Again(变形的斐波那契)

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1021 Fibonacci Again Time Limit: 2000/1000 MS (Java/Ot ...

  9. HDU 4786 Fibonacci Tree(生成树,YY乱搞)

    http://acm.hdu.edu.cn/showproblem.php? pid=4786 Fibonacci Tree Time Limit: 4000/2000 MS (Java/Others ...

随机推荐

  1. android xml绘图p113-p117

    1.Bitmap <?xml version="1.0" encoding="utf-8"?> <bitmap xmlns:android=& ...

  2. LeetCode OJ:Sqrt(x)(平方根)

    Implement int sqrt(int x). Compute and return the square root of x. 简单的二分法,注意mid应该选为long,否则容易溢出: cla ...

  3. react use simditor

    1.install simditor 2.import simditor && scss import $ from "jquery" import Simdito ...

  4. linux中的阻塞机制及等待队列

    阻塞与非阻塞是设备访问的两种方式.驱动程序需要提供阻塞(等待队列,中断)和非阻塞方式(轮询,异步通知)访问设备.在写阻塞与非阻塞的驱动程序时,经常用到等待队列. 一.阻塞与非阻塞 阻塞调用是没有获得资 ...

  5. 当导用模块与包的import与from的问题(模块与包的调用)

    当在views.py里写impor models会不会报错呢? 1.Python里面的py文件都是每一行的代码. 2.Python解释器去找一个模块的时候,只去sys.path的路径里找 3.djan ...

  6. 5款实用的硬盘、SSD固态硬盘、U盘、储存卡磁盘性能测试工具绿色版

    http://www.iplaysoft.com/disk-benchmark-tools.html/comment-page-1#comment-149425

  7. SQL—数据分析

    留存分析——左连接 矩阵分析——group by 结构分析:分析每种产品占据总销售量的比例 排序分析

  8. 剑指offer-第六章面试中的各项能力(二叉树的深度)

    题目:1:输入一个二叉树,求二叉树的深度.从根节点开始最长的路径. 思路:我们可以考虑用递归,求最长的路径实际上就是求根节点的左右子树中较长的一个然后再加上1. 题目2:输入一颗二叉树的根节点,判断该 ...

  9. runtime获取对象所有属性(变量)和方法

    1.包含运行时头文件 <objc/runtime.h> 2.获取某个类的成员变量或者属性: unsigned int numIvars; //成员变量个数 Ivar *vars = cla ...

  10. 十二、python沉淀之路--内置函数

    1.abs函数,求绝对值. a = abs(-3) print(a) 返回:3 2.all函数:判断是否是可迭代对象. 官方解释:Return True if bool(x) is True for ...