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. DELETE---删除记录

    DELETE FROM table_name [WHERE conditions]; 例: DELETE FROM userinfo WHERE name='zhangsan'; 说明 1.不添加条件 ...

  2. create---创建表

    create table table_name (列名 数据类型 是否非空 约束信息, 列名 数据类型 是否非空 约束信息, 列名 数据类型 是否非空 约束信息, ........); 例: crea ...

  3. FWFT FIFO读操作注意

    FWFT:First Word Fall Through的缩写,好像是Xilinx的说法,Altera对应的概念是Show-ahead synchronous(SASO).即数据在rdreq有效之前就 ...

  4. 破解SQLYog30天试用方法

    开始-运行-regedit ,进入注册表,在 \HEYK_CURRENT_USER\Software\{FCE28CE8-D8CE-4637-9BC7-93E4C0D407FA}下的InD保存着SQL ...

  5. XML操作 之获取指定节点值

    根据节点名称快速查找 指定节点值 using (TextReader stringReader = new StringReader(clearPassResponse)) { XmlReaderSe ...

  6. uploadify上传控件使用

    uploadify是JQuery的一个上传插件,实现的效果非常不错,并且带进度显示,我将给大家演示如何使用uploadify进行图片上传, 1.点我下载http://www.uploadify.com ...

  7. poj2299 二分思想

    poj2299   http://poj.org/problem?id=2299题意: 一个含有n个数的数组, 每次只能交换相邻的两个数, 求最少操作多少次可以使该数组变成一个有序数组(从小到大).  ...

  8. C10K问题和Libevent库介绍

    http://blog.chinaunix.net/uid-20761674-id-75056.html 一.C10K的问题 C10K的问题在上个世纪90年代就被提出来了.大概的意思是当用户数超过1万 ...

  9. Oracle数据库导入imp命令导入时1659错误处理

    今天在自己的电脑上在给数据库导入表结构及数据时报1659错误,错误如下: IMP-00017:由于oracle错误1659,以下语句失败: “create table “T_TELETE” ..... ...

  10. 利用ORACLE ADV 功能完成SQL TUNING 调优(顾问培训) “让DBA失业还是解脱?”

    oracle自动判断SQL性能功能. 11G的ADV,建议.SNAPSHOT,数据集合, 存储在oracle sys $_开头的表(10几条).  创建SNAPSHOT时选择天数, 默认14天. sq ...