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. [转]VC中调用外部exe程序方式

    本文转自:http://blog.sina.com.cn/s/blog_486285690100ljwu.html 目前知道三种方式:WinExec,ShellExecute ,CreateProce ...

  2. Docker 容器镜像操作

    1.停止所有的container,这样才能够删除其中的images:docker stop $(docker ps -a -q)如果想要删除所有container的话再加一个指令: docker rm ...

  3. 关于 a 标签 jquery的trigger("click"),无法触发问题。

    这个问题的原因不是jquery的trigger("click"), 函数的问题, 而是 a标签之间要有其他子标签,要对这个子标签调用trigger("click" ...

  4. AJPFX谈JAVA新手问题之异常处理使用不当

    ★空的 catch 语句块 犯这种错误的人比较少,一般发生在刚学会 Java 或者刚参加工作不久的人身上. 所谓“空 catch 语句块”就是在 catch 语句块中没有对异常作任何处理(比如记错误日 ...

  5. CF765C Table Tennis Game 2

    题意: Misha and Vanya have played several table tennis sets. Each set consists of several serves, each ...

  6. JavaScript中,有三种常用的绑定事件的方法

    要想让 JavaScript 对用户的操作作出响应,首先要对 DOM 元素绑定事件处理函数.所谓事件处理函数,就是处理用户操作的函数,不同的操作对应不同的名称. 在JavaScript中,有三种常用的 ...

  7. java8的lambda表达式,将List<DTO> 转为 List<DO>

    将List<PhoneDTO>转为List<PhoneDO>,通过java8的lambda表达式来操作,比传统的for循环精简很多: /** * List<PhoneDT ...

  8. pandas中loc-iloc-ix的使用

    转自:https://www.jianshu.com/p/d6a9845a0a34 Pandas中loc,iloc,ix的使用 使用 iloc 从DataFrame中筛选数据 iloc 是基于“位置” ...

  9. Unity复杂的旋转-欧拉角和四元数

    一.欧拉角欧拉角最容易表示,用三个变量X,Y,Z可以直观的表示绕着某个轴的旋转角度. 在Unity里就是Transform组件的Rotation里的X Y Z三个变量代表了欧拉角 二.四元数四元数相比 ...

  10. 事件捕获 & 事件冒泡

    <body> <div id="div1"> <div id="div2"> <div id="div3&q ...