URAL 1513. Lemon Tale(简单的递推)
写几组数据就会发现规律了啊。
。但是我是竖着看的。。
。还找了半天啊、、、
只是要用高精度来写,水题啊。就当熟悉一下java了啊。
num[i] = 2*num[i-1]-num[i-2-k]。
1513. Lemon Tale
Memory limit: 64 MB
Background
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.
Problem
As a result a word "lemon" was mentioned N times in the statement.
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.
problem could not be disqualified. How many ways are there to do it?
Input
Output
Sample
| input | output |
|---|---|
5 2 |
24 |
Hint
"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(简单的递推)的更多相关文章
- 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 ...
- HDU 2569(简单的递推)
彼岸 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submissi ...
- 【图灵杯 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项的值对 ...
- URAL 1009 K-based numbers(DP递推)
点我看题目 题意 : K进制的N位数,不能有前导零,这N位数不能有连续的两个0在里边,问满足上述条件的数有多少个. 思路 : ch[i]代表着K进制的 i 位数,不含两个连续的0的个数. 当第 i 位 ...
- Flags-Ural1225简单递推
Time limit: 1.0 second Memory limit: 64 MB On the Day of the Flag of Russia a shop-owner decided to ...
- UVa 825【简单dp,递推】
UVa 825 题意:给定一个网格图(街道图),其中有一些交叉路口点不能走.问从西北角走到东南角最短走法有多少种.(好像没看到给数据范围...) 简单的递推吧,当然也就是最简单的动归了.显然最短路长度 ...
- UVA10943简单递推
题意: 给你两个数字n,k,意思是用k个不大于n的数字组合(相加和)为n一共有多少种方法? 思路: 比较简单的递推题目,d[i][j]表示用了i个数字的和为j一共有多少种情况,则 ...
- [LeetCode] 递推思想的美妙 Best Time to Buy and Sell Stock I, II, III O(n) 解法
题记:在求最大最小值的类似题目中,递推思想的奇妙之处,在于递推过程也就是比较求值的过程,从而做到一次遍历得到结果. LeetCode 上面的这三道题最能展现递推思想的美丽之处了. 题1 Best Ti ...
- 算法技巧讲解》关于对于递推形DP的前缀和优化
这是在2016在长沙集训的第三天,一位学长讲解了“前缀和优化”这一技巧,并且他这一方法用的很6,个人觉得很有学习的必要. 这一技巧能使线性递推形DP的速度有着飞跃性的提升,从O(N2)优化到O(N)也 ...
随机推荐
- lua中调用C++函数
lua中调用C++函数 我们产品中提供了很多lua-C API给用户在lua中调用,之前一直没用深究其实现原理,只是根据已有的代码在编码.显然这不是一个好的习惯,没用达到知其所以然的目的. 一.基本原 ...
- Mateclass
Mateclass 一切皆对象: Eg: class Foo: pass f=Foo() In [60]: print(type(f)) <class '__main__.Foo'> In ...
- [NOIP补坑计划]NOIP2014 题解&做题心得
六道普及组题,没啥好说的 场上预计得分:100+100+100+100+100+100=600(省一分数线490) (AK是不可能AK的,这辈子不可能AK的) 题解: D1T1 生活大爆炸版石头剪刀布 ...
- PHP XML To Array将XML转换为数组
// Xml 转 数组, 包括根键,忽略空元素和属性,尚有重大错误 function xml_to_array( $xml ) { $reg = "/<(\\w+)[^>]*?& ...
- POJ 2228 Naptime(DP+环形处理)
题解 这题一眼望去DP. 发现自己太智障了. 这题想的是O(n^3m)的. 环形处理只会断环成链....然后DP也想的不好. 我们先考虑如果除去环这题该怎么做? dp[i][j][0/1]代表到第i小 ...
- 永远不要在MySQL中使用“utf8”
最近我遇到了一个 bug,我试着通过 Rails 在以“utf8”编码的 MariaDB 中保存一个 UTF-8 字符串,然后出现了一个离奇的错误: Incorrect string value: ‘ ...
- 计数排序(counting-sort)
计数排序是一种稳定的排序算法,它不是比较排序.计数排序是有条件限制的:排序的数必须是n个0到k的数,所以计数排序不适合给字母排序.计数排序时间复杂度:O(n+k),空间复杂度:O(k),当k=n时,时 ...
- 题解 P3200 【[HNOI2009]有趣的数列】
说起来这是今天第三道卡特兰数了... 楼上的几篇题解好像都是直接看出这是卡特兰数,所以我就写一下为什么这道题可以用卡特兰数吧. 考察这样相邻的两项:\(a_{2i-1}\)与\(a_{2i}\),根据 ...
- Android使用Fragment,不能得到Fragment内部控件,findViewById()结果是Null--已经解决
程序很easy.好长时间没有搞定.郁闷.... . .... . . . 在论坛咨询,最终找到答案. 描写叙述: 一个Activity:MainActivity.内部是一个Fragment:Fragm ...
- SPOJ 题目705 New Distinct Substrings(后缀数组,求不同的子串个数)
SUBST1 - New Distinct Substrings no tags Given a string, we need to find the total number of its di ...