Portal

Description

定义\(k\)-bonacci数列\(\{F_n\}\):\(F_i=0 \ (i<k),F_i=1 \ (i=k),F_i=\sum_{j=i-k}^{i-1}F_j\)

给出\(s(s\leq10^9)\)和\(k(k\leq10^9)\),将\(s\)拆成若干个\(k\)-bonacci数之和。

Solution

结论:重复从\(s\)中减掉最大的\(F_i\),一定能使\(s=0\)。

可以用数学归纳法证明。

若对于正整数\(k\),\(\forall s\in [0,F_k-1]\)该结论成立,则\(\forall s\in [F_k,F_{k+1}-1]\),其下最大的\(F_i\)为\(F_k\),而\(s-F_k\in [0,F_{k-1}-1]\),其必然也能按上述方法减至0。

而因为\(k=1\)时该结论成立,所以\(\forall s\)该结论均成立。

Code

//Well-known Numbers
#include <cstdio>
#include <algorithm>
using namespace std;
int const N=1e5+10;
long long f[N];
int n,m,ans[N];
int main()
{
int s,k; scanf("%d%d",&s,&k);
int n; f[1]=1;
for(n=2;f[n-1]<s;n++)
for(int j=max(1,n-k);j<=n-1;j++) f[n]+=f[j];
int m=0;
for(int i=n-1;i>=1&&s;i--) if(f[i]<=s) ans[++m]=f[i],s-=f[i];
if(m<2) ans[++m]=0;
printf("%d\n",m);
for(int i=1;i<=m;i++) printf("%d ",ans[i]);
puts("");
return 0;
}

P.S.

看标签猜结论系列binary search greedy number theory。不过根本不需要binary search啊!

Codeforces225B - 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. Suricata的初始化脚本

    见官网 https://suricata.readthedocs.io/en/latest/initscripts.html

  2. Unity坐标系统

    Unity使用了几种不同的坐标系统,分别如下: 1.屏幕坐标(Screen Space):屏幕坐标是以像素来定义的,以屏幕左下角为(0, 0)点,右上角为(Screen.width, Screen.h ...

  3. Log4net系列二:Log4net邮件日志以及授权码

    Log4net邮件发送 上篇文章我们主要介绍Log4net生成文本格式,本篇文章主要配置邮箱发送.关于项目的引用,搭建我们就不在描述,如果不太清楚,请看上篇文章, 老规矩,我们现在配置文件中添加一个a ...

  4. 初次使用引用外部js心得

    在外部引用自己编辑的js时建立链接写在头部中是会出错的,如下图 错误如下: 这是一个是我初学时遇到的一个算是低级错误吧,看到这个错误,我以为的是我引用的js中编辑的代码是不是哪里写错了,但是看了好多遍 ...

  5. SqlSessionFactory

    源码: public interface SqlSessionFactory { SqlSession openSession(); SqlSession openSession(boolean va ...

  6. jmeter的JVM参数设置

    JMeter用户可根据运行的计算机配置,来适当调整JMeter.bat中的JVM调优设置,如下所示: set HEAP=-Xms512m -Xmx512m set NEW=-XX:NewSize=12 ...

  7. java工作流activiti的步骤

    链接:activiti 表名称的解释 工作流从流程定义到创建一个流程实例完成执行步骤(省略bpmn的画法) 工作流的所有操作都是使用流程引擎来进行操作的,流程引擎只是存储流程的过程,而不存储具体的业务 ...

  8. Python 中列表、元祖、字典

    1.元祖: 对象有序排列,通过索引读取读取, 对象不可变,可以是数字.字符串.列表.字典.其他元祖 2.列表: 对象有序排列,通过索引读取读取, 对象是可变的,可以是数字.字符串.元祖.其他列表.字典 ...

  9. 在Eclipse中设置自动补全

    在Eclipse中菜单中,Window->Preferences->Java->Editor->Content Assist中的Auto activation triggers ...

  10. IntelliJ IDEA使用eclipse compiler(ecj)解决lombok编译问题

    1:为什么要使用在idea使用eclipse compiler(ecj)? 因为idea默认使用javac,而javac当在编译时发现一个编译错误就直接停止编译了.而ecj支持proceed on e ...