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

。但是我是竖着看的。。

。还找了半天啊、、、

只是要用高精度来写,水题啊。就当熟悉一下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. POJ 1990 MooFest【 树状数组 】

    题意:给出n头牛,每头牛有一个听力v,坐标x,两头牛之间的能量为max(v1,v2)*dist(v1,v2),求总的能量值 先将每头牛按照v排序,排完顺序之后,会发现有坐标比当前的x小的,会有坐标比当 ...

  2. Ueditor富编辑器

    坑多的Ueditor富编辑器 第一步:修改serverUrl: window.BASEPATH + "notice/word" 第二部:添加依赖包 <dependency&g ...

  3. JS自定义全局Error

    <script> ///自定义错误 onerror=handleErr; function handleErr(msg,url,l) { var txt=""; txt ...

  4. PHP通过DOM操作XML

    PHP XML操作类DOMDocument属性及方法 注意大小写一定不能弄错. 属性: Attributes 存储节点的属性列表(只读) childNodes 存储节点的子节点列表(只读) dataT ...

  5. BZOJ 3166 [HEOI2013]Alo (可持久化01Trie+链表)

    题目大意:给你一个长度为$n$的序列,让你找出一段子序列,求其中的 次大值 异或 序列里一个数 能得到的最大值 先对序列建出可持久化$Trie$ 按元素的值从小到大遍历,设当前元素的位置是i,找出它左 ...

  6. (QT)在命令行编译ui文件和程序

    1.新建helloworld_2文件夹,将helloworld里的main.cpp和hellodialog.cpp两个文件复制过来. 2.打开控制台.此时不能用cmd,否则不能出最后的结果(lz在运行 ...

  7. Object-C,数组NSArray

    晚上回来,写了2个iOS应用程序. 就是在界面中,展示标签.一种是手动构造界面,然后绑定事件.另外一种是,使用自带的界面作为容器,但是手动向里面放其它界面元素. 书中的观点是,使用图形化界面,构造界面 ...

  8. CSDN 轻松周赛赛题:能否被8整除

    轻松周赛赛题:能否被8整除 题目详情 给定一个非负整数,问能否重排它的全部数字,使得重排后的数能被8整除. 输入格式: 多组数据,每组数据是一个非负整数.非负整数的位数不超过10000位. 输出格式 ...

  9. C++容器(二):关联容器简介

    关联容器(associative container)与顺序容器的本质区别在于:关联容器通过键(Key)存储和读取元素,而顺序容器则通过元素在容器中的位置顺序存储和访问元素.虽然,关联容器的大部分行为 ...

  10. Regular Expressions Syntax

    https://support.syslogwatcher.com/support/solutions/articles/8000033627-regular-expressions-syntax