Numbers k-bonacci (k is integer, k > 1) are a generalization of Fibonacci numbers and are determined as follows

  • F(k, n) = 0, for integer n, 1 ≤ n < k;
  • F(k, k) = 1;
  • F(k, n) = F(k, n - 1) + F(k, n - 2) + ... + F(k, n - k), for integer nn > k.

Note that we determine the k-bonacci numbers, F(k, n), only for integer values of n and k.

You've got a number s, represent it as a sum of several (at least two) distinct k-bonacci numbers.

Input

The first line contains two integers s and k (1 ≤ s, k ≤ 109k > 1).

Output

In the first line print an integer m (m ≥ 2) that shows how many numbers are in the found representation. In the second line print mdistinct integers a1, a2, ..., am. Each printed integer should be a k-bonacci number. The sum of printed integers must equal s.

It is guaranteed that the answer exists. If there are several possible answers, print any of them.

题目背景是一个k—Fibonacci数列,也就是,第0,1项是1,然后后面的第i项为前面k项之和,即f[i]=f[i-1]+.....f[i-k],(i>=k+1),然后输入整数s,k,输出能使得加起来和为s的m(m>=2)个不同的k—Fibonacci数,1<=s<=10^9,k>1,考虑到k最小为2时,f[50]>=10^10,所以对于任意k,满足条件的整数不会超过10^9,只需要存储前50个就可以了。这样s依次减去小于它的最大Fibonacci值,直到s为0.

题目要求最少输出2个数,所以遇到恰好为Fibonacci数的s值,可以输出一个0。

代码:

 #include<stdio.h>
#define max(a,b) ((a)>(b)?(a):(b))
#define N 50
typedef long long ll;
ll f[N];
int main(void)
{
int s,k;
int i,j,ct=;
ll ans[N];
scanf("%d%d",&s,&k);
f[]=f[]=;
f[]=;
for(i=;i<N;i++)
{
if(i>=k+)
f[i]=*f[i-]-f[i-k-];//i>=k+1,递推公式:f[i]=2*f[i-1]-f[i-k-1]
else for(j=i-;j>=max(i-k,);j--)
f[i]+=f[j];//否则f[i]为前面k项之和
}
for(i=N-;i>;i--)
{
if((s>=f[i]))
{
s-=f[i];
ans[ct++]=f[i];
}
if(s==)
{
if(ct==){
printf("%d\n",ct+);
printf("0 ");
}
else printf("%d\n",ct);
for(i=;i<ct;i++)
printf("%I64d%c",ans[i],i==ct-?'\n':' ');
return ;
}
}
return ;
}

CF_225B _Well-known Numbers的更多相关文章

  1. Java 位运算2-LeetCode 201 Bitwise AND of Numbers Range

    在Java位运算总结-leetcode题目博文中总结了Java提供的按位运算操作符,今天又碰到LeetCode中一道按位操作的题目 Given a range [m, n] where 0 <= ...

  2. POJ 2739. Sum of Consecutive Prime Numbers

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20050 ...

  3. [LeetCode] Add Two Numbers II 两个数字相加之二

    You are given two linked lists representing two non-negative numbers. The most significant digit com ...

  4. [LeetCode] Maximum XOR of Two Numbers in an Array 数组中异或值最大的两个数字

    Given a non-empty array of numbers, a0, a1, a2, … , an-1, where 0 ≤ ai < 231. Find the maximum re ...

  5. [LeetCode] Count Numbers with Unique Digits 计算各位不相同的数字个数

    Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Examp ...

  6. [LeetCode] Bitwise AND of Numbers Range 数字范围位相与

    Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers ...

  7. [LeetCode] Valid Phone Numbers 验证电话号码

    Given a text file file.txt that contains list of phone numbers (one per line), write a one liner bas ...

  8. [LeetCode] Consecutive Numbers 连续的数字

    Write a SQL query to find all numbers that appear at least three times consecutively. +----+-----+ | ...

  9. [LeetCode] Compare Version Numbers 版本比较

    Compare two version numbers version1 and version1.If version1 > version2 return 1, if version1 &l ...

随机推荐

  1. eclipse引入jar类包的源代码

    为了方便查看源代码,有时候需要对jar类包,引入源代码. 下载源代码 源代码是zip或者rar的压缩包 引入到项目中

  2. Java RMI详解

    RMI:远程方法调用(Remote Method Invocation).能够让在某个java虚拟机上的对象像调用本地对象一样调用另一个java 虚拟机中的对象上的方法. RMI远程调用步骤: 1,客 ...

  3. laydate JS日期插件

    原文出处 简要介绍 你是时候换一款日期控件了,而layDate非常愿意和您成为工作伙伴.她致力于成为全球最用心的web日期支撑,为国内外所有从事web应用开发的同仁提供力所能及的动力.她基于原生Jav ...

  4. 取caml查询结果的前多少行

    取查询结果的前多少行的方法?spQuery.RowLimit = 1;             SPList list = SPContext.Current.Web.Lists[ListNames. ...

  5. WebSocket使用教程 - 带完整实例

    http://my.oschina.net/u/1266171/blog/357488 什么是WebSocket?看过html5的同学都知道,WebSocket protocol 是HTML5一种新的 ...

  6. php常用正则表达式函数

    执行一个正则表达式匹配: preg_match($pattern, $subject, [array &$matches]); 最多匹配一次,返回值0或1,把第一次匹配到的结果放入$match ...

  7. 7-2 DBA顾问第一次上次操作考试

    SQLPLUS执行:     1--@?/rdbms/admin/awrrpt 生产snapshot, 一个时间点, 再执行下一个时间点.     2--   附件作业第一次执行步骤: 1) SQLP ...

  8. mysql主从之主键冲突

    收到短信报警,两台数据库都报slave同步失败了,先说明一下环境,架构:lvs+keepalived+amoeba+mysql,主主复制,单台写入, 主1:192.168.0.223(写) 主2:19 ...

  9. 远程mysql出现ERROR 1130 (HY000): Host '172.17.42.1' is not allowed to connect to this MySQL server

    ERROR 1130: Host ***.***.***.*** is not allowed to connect to this MySQL server 说明所连接的用户帐号没有远程连接的权限, ...

  10. Ubuntu下gcc及g++环境配置

    直接在命令行中输入以下命令即可. sudo apt-get install build-essential 安装完成后输入 gcc 和 g++ 进行确认.