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. react antd form多组表单数据处理

    import React from 'react'; import {Form, InputNumber, Input, DatePicker, Button, Select, Icon} from ...

  2. Happening in delphi world

    Happy New Year! Delphi XE5 Update 2 Recent VCL enhancements New product features for old product use ...

  3. c printf打印格式

    关于小数点位数的举例:  <pre lang="c" escaped="true">#include <stdio.h> /* 当fah ...

  4. 网络爬虫必备知识之urllib库

    就库的范围,个人认为网络爬虫必备库知识包括urllib.requests.re.BeautifulSoup.concurrent.futures,接下来将结合爬虫示例分别对urllib库的使用方法进行 ...

  5. 核PCA投影平面公式推导

    样本方差推导 样本方差公式\[S = \frac{1}{n-1}\sum_{i=1}^n(x_i-\mu_i)^2\] 扩展开来得到\[S = \frac{1}{n-1}[(X-\frac{1}{n} ...

  6. ActiveMQ面试题

    什么是activemq activeMQ是一种开源的,实现了JMS1.1规范的,面向消息(MOM)的中间件,为应用程序提供高效的.可扩展的.稳定的和安全的企业级消息通信. activemq的作用以及原 ...

  7. mysql8之与标准sql的区别

    一  mysql8概述 在研究mysql8新特性的时候,越来越感觉mysql8朝sql server看齐.看来对于中小型企业级应用也挺有兴趣,但是没有企业级的应用套件,有知道的麻烦告知.本文不探讨my ...

  8. Weex入门篇——Mac 安装Weex

    相关文档:http://blog.csdn.net/jasonblog/article/details/51863173 前言 相比较于React Native的“Learn once, write ...

  9. 关于Gateway

    为什么要有gateway?就是因为如果没有,很多的功能需要在在每个微服务中都实现,这样成本很高.

  10. bzoj 4671 异或图——容斥+斯特林反演+线性基

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4671 考虑计算不是连通图的方案,乘上容斥系数来进行容斥. 可以枚举子集划分(复杂度是O(Be ...