CF_225B _Well-known Numbers
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 n, n > 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 ≤ 109; k > 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的更多相关文章
- Java 位运算2-LeetCode 201 Bitwise AND of Numbers Range
在Java位运算总结-leetcode题目博文中总结了Java提供的按位运算操作符,今天又碰到LeetCode中一道按位操作的题目 Given a range [m, n] where 0 <= ...
- POJ 2739. Sum of Consecutive Prime Numbers
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20050 ...
- [LeetCode] Add Two Numbers II 两个数字相加之二
You are given two linked lists representing two non-negative numbers. The most significant digit com ...
- [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 ...
- [LeetCode] Count Numbers with Unique Digits 计算各位不相同的数字个数
Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Examp ...
- [LeetCode] Bitwise AND of Numbers Range 数字范围位相与
Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers ...
- [LeetCode] Valid Phone Numbers 验证电话号码
Given a text file file.txt that contains list of phone numbers (one per line), write a one liner bas ...
- [LeetCode] Consecutive Numbers 连续的数字
Write a SQL query to find all numbers that appear at least three times consecutively. +----+-----+ | ...
- [LeetCode] Compare Version Numbers 版本比较
Compare two version numbers version1 and version1.If version1 > version2 return 1, if version1 &l ...
随机推荐
- create---创建表
create table table_name (列名 数据类型 是否非空 约束信息, 列名 数据类型 是否非空 约束信息, 列名 数据类型 是否非空 约束信息, ........); 例: crea ...
- hdoj1325 Is It A Tree?
Is It A Tree?题目链接 题意: 多组测试数据, 每组数据有多个数对, 表示一条有向边(即第一个数是第二个数的父节点), 以 0,0 为一组测试数据结束标志.当输入-1,-1时测试结束. 从 ...
- Android adapter适配器的使用
说起Adapter的使用,首先想到的就是listview或各种各样的Adapter.下面我们对常用的一些Adapter进行简单的使用讲解. 这是Adapter的关系图: 下面的所有例子均使用同一个布局 ...
- AFNetworking3.0+MBProgressHUD二次封装,一句话搞定网络提示
对AFNetworking3.0+MBProgressHUD的二次封装,使用更方便,适用性非常强: 一句话搞定网络提示: 再也不用担心网络库更新后,工程要修改很多地方了!网络库更新了只需要更新这个封装 ...
- Xcode8 Could not build Objective-C module 'FBSDKCoreKit'
解决方法是: 删除/Users/Rinpe/Library/Developer/Xcode/DerivedData下对应的文件夹即可.
- OC与Swift的区别四(条件语句)
12.条件语句的区别,此处只写区别,没有指出区别的其他方面oc与swift基本一致 12.1 oc中for if switch语句体如果只有一行代码,则{}可以省略 swift中for if swit ...
- [转] jQuery按键响应事件keypress对应的按键编码keycode
原文地址:http://blog.csdn.net/chenhj1988918/article/details/7534922 keypress api 文档:http://api.jquery.c ...
- 最近使用Qt遇到的一些小问题解决办法总结
1. 我想获取当前星期几这样的,我没在API里面找到这样的函数,但是我找到了今天是第几天这样的,所以自己转换一下就OK了: typedef struct { int numInWeek; QStrin ...
- SGU 解题报告
Volume 1 Volume 2
- ASP.NET中Json的处理
要使用.NET自带的JSON处理工具需要引用下面的命名空间: using System.Web.Script.Serialization; 1.编码 myConfig mc = new myConfi ...