写几组数据就会发现规律了啊。

。但是我是竖着看的。。

。还找了半天啊、、、

只是要用高精度来写,水题啊。就当熟悉一下java了啊。

num[i] = 2*num[i-1]-num[i-2-k]。

1513. Lemon Tale

Time limit: 1.0 second

Memory limit: 64 MB

Background

For each programmer a point comes when the last contest is lost, and it is time to retire. Even Three Programmers themselves could not escape the common lot. But the Programmers also wanted to keep
a good memory about themselves. For this noble purpose they created problems and organized extremely popular programming contests from time to time. Of course, this work was not well paid, but for true programmers a glory was more important than money.
However it is only the first half of a job to think out a brilliant problem. The second one is to create a politically correct statement for it.

Problem

The matter is the statement of some problem for the upcoming contest was written by the Third Programmer, who knew nothing about political correctness. He just wrote a story about citrus plants growing.
As a result a word "lemon" was mentioned N times in the statement.
Besides, the problem is to be looked through by famous censor Alexander K. right before the contest. And it is a known fact, that lemons remind him of oranges he hates furiously. It worries the First
and the Second Programmers greatly - they know exactly, that if a word "lemon" occurs more than Ktimes successively, the problem will be immediately disqualified from the contest.
That is why the First and the Second Programmers connived secretly to login to the server at the eve of the contest and replace some "lemons" with much more politically correct "bananas" so that the
problem could not be disqualified. How many ways are there to do it?

Input

The only line contains the integer numbers N (1 ≤ N ≤ 10000) and K (0 ≤ K ≤ N).

Output

You should output the desired number of ways.

Sample

input output
5 2
24

Hint

Let us denote a word "lemon" by a letter "L" and a word "banana" by a letter "B". So in the sample the initial sequence of words "LLLLL" might be transformed into the following politically correct sequences:
"LLBLL", "LLBLB", "LLBBL", "LLBBB", "LBLLB", "LBLBL", "LBLBB", "LBBLL", "LBBLB", "LBBBL", "LBBBB", "BLLBL", "BLLBB", "BLBLL", "BLBLB", "BLBBL", "BLBBB", "BBLLB", "BBLBL", "BBLBB", "BBBLL", "BBBLB", "BBBBL" and "BBBBB".
import java.math.BigInteger;
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner cin = new Scanner(System.in);
int n, k;
BigInteger num[] = new BigInteger [100005];
while(cin.hasNext())
{
n = cin.nextInt();
k = cin.nextInt();
BigInteger ans;
BigInteger p = BigInteger.valueOf(2);
ans = p.pow(n);
if(k == 0)
{
System.out.println(1);
continue;
}
if(n == k)
{
System.out.println(ans);
continue;
}
num[0] = BigInteger.valueOf(1);
for(int i = 1; i <= k+1; i++)
{
num[i] = p.pow(i-1);
}
for(int i = k+2; i <= n; i++)
{
num[i] = num[i-1].multiply(BigInteger.valueOf(2));
num[i] = num[i].subtract(num[i-2-k]);
}
ans = BigInteger.valueOf(0);
for(int i = n; i >= (n-k); i--)
{
ans = ans.add(num[i]);
}
System.out.println(ans);
}
}
}

URAL 1513. Lemon Tale(简单的递推)的更多相关文章

  1. URAL 1513 Lemon Tale

    URAL 1513 思路: dp+高精度 状态:dp[i][j]表示长度为i末尾连续j个L的方案数 初始状态:dp[0][0]=1 状态转移:dp[i][j]=dp[i-1][j-1](0<=j ...

  2. HDU 2569(简单的递推)

    彼岸 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submissi ...

  3. 【图灵杯 F】一道简单的递推题(矩阵快速幂,乘法模板)

    Description 存在如下递推式: F(n+1)=A1*F(n)+A2*F(n-1)+-+An*F(1) F(n+2)=A1*F(n+1)+A2*F(n)+-+An*F(2) - 求第K项的值对 ...

  4. URAL 1009 K-based numbers(DP递推)

    点我看题目 题意 : K进制的N位数,不能有前导零,这N位数不能有连续的两个0在里边,问满足上述条件的数有多少个. 思路 : ch[i]代表着K进制的 i 位数,不含两个连续的0的个数. 当第 i 位 ...

  5. Flags-Ural1225简单递推

    Time limit: 1.0 second Memory limit: 64 MB On the Day of the Flag of Russia a shop-owner decided to ...

  6. UVa 825【简单dp,递推】

    UVa 825 题意:给定一个网格图(街道图),其中有一些交叉路口点不能走.问从西北角走到东南角最短走法有多少种.(好像没看到给数据范围...) 简单的递推吧,当然也就是最简单的动归了.显然最短路长度 ...

  7. UVA10943简单递推

    题意:      给你两个数字n,k,意思是用k个不大于n的数字组合(相加和)为n一共有多少种方法? 思路:       比较简单的递推题目,d[i][j]表示用了i个数字的和为j一共有多少种情况,则 ...

  8. [LeetCode] 递推思想的美妙 Best Time to Buy and Sell Stock I, II, III O(n) 解法

    题记:在求最大最小值的类似题目中,递推思想的奇妙之处,在于递推过程也就是比较求值的过程,从而做到一次遍历得到结果. LeetCode 上面的这三道题最能展现递推思想的美丽之处了. 题1 Best Ti ...

  9. 算法技巧讲解》关于对于递推形DP的前缀和优化

    这是在2016在长沙集训的第三天,一位学长讲解了“前缀和优化”这一技巧,并且他这一方法用的很6,个人觉得很有学习的必要. 这一技巧能使线性递推形DP的速度有着飞跃性的提升,从O(N2)优化到O(N)也 ...

随机推荐

  1. How Javascript works (Javascript工作原理) (三) 内存管理及如何处理 4 类常见的内存泄漏问题

    个人总结: 1.两种垃圾回收机制: 1)引用标记算法:如果检测到一个对象没有被引用了,就清除它. ***这种算法不能处理循环引用的情况*** 2)标记—清除算法:从根(全局变量)开始向后代变量检测,任 ...

  2. React 第三天

    第三天 01:在组件中使用style行内对象并封装样式对象: CmtItem.jsx: import React from 'react' //第一层封装 将样式对象和UI结构分离 // const ...

  3. NOIp2018模拟赛四十三

    有了昨天的经验,不慌,开题先看source ******** 再看看题,看到C题标题: ******** 有毒... B题的“显然”50分结论推了我一个小时,然后就弃疗了... 成绩:0+50+5=5 ...

  4. 越努力越幸运--2-LD_PRELOAD, fork ,僵尸进程

    开始新的工作了,做了爸爸之后感觉一直都是浑浑噩噩,希望老婆和宝宝一直健康开心~ 最近遇到的问题很多啊,哈哈 1. 装环境时候,需要的glibc 版本不对,我把本地的软链接改了个别名(惯性思维),然后一 ...

  5. 紫书 例题 11-3 UVa 1151 (有边集的最小生成树+二进制枚举子集)

    标题指的边集是说这道题的套餐, 是由几条边构成的. 思路是先做一遍最小生成树排除边, 因为如果第一次做没有加入的边, 到后来新加入了很多权值为0的边,这些边肯定排在最前面,然后这条边的前面的那些边肯定 ...

  6. 数组名作为函数参数以及sizeof用法

    来源:https://blog.csdn.net/jay_zhouxl/article/details/51745518 int f(int *p,char *a) { printf("p[ ...

  7. hadoop-05-mysql修改密码

    hadoop-05-mysql修改密码 su root 1,service mysqld start 2,vi /var/log/mysqld.log #在这里面查找密码 3, mysql -uroo ...

  8. Java排序之直接选择排序

    public class SelectSort { public static void selectSort(int [] a){ int min; int temp; if(a==null || ...

  9. angularjs --- ngResource 类似于 ajax发送请求。

    <!DOCTYPE HTML> <html ng-app="myApp"> <head> <meta http-equiv="C ...

  10. NET下Assembly的加载过程

    NET下Assembly的加载过程 最近在工作中牵涉到了.NET下的一个古老的问题:Assembly的加载过程.虽然网上有很多文章介绍这部分内容,很多文章也是很久以前就已经出现了,但阅读之后发现,并没 ...