[dp专题000]uva 10328 Coin Toss (java 大数+dp)(Unsolved)

题目链接

题意:问长度为n,每个位置由且仅有‘H’和’T’组成的序列中,至少有连续k个‘H’出现的方案数。

思路:不会做,参考了题解 不过没有完全搞懂。

根据题解,正面考虑比较麻烦,所以反面考虑。[j]

dp[i][j]表示长度为i,前面最后连续的‘H’的个数不超过j个的方案数。

考虑转移方程为:

总的情况为:dp[i][j] = dp[i-1][j] * 2;

但是其中有多考虑的情况,就是第i位是’H’,且i位之前的最后j个位置都是’H’(即从i-j位到第i-1位都是‘H’,此时第i-j-1位必然是’T’)

有i个硬币时,如果i < j+1,那么它对于i-1的情况来说,最后一个硬币的位置就可以随便放了。

如果i > j + 1,dp[ i ] [ j ]  = dp [ i – 1 ] [ j ] * 2 – x(不满足条件的部分)

然后我们来考虑这个x怎么求,既然是不满足条件,那么肯定是第i的位置放了H,前面的都是H,从而这些连续的H大于j。那么就考虑dp[ i – 1 – j – 1 ](中间这 j – 1 个(kk:疑似作者笔误。应该位j个)全为H,加上第i个H,就不满足条件了),所以:

dp [ i ] [ j ] = dp [ i – 1 ] [ j ] * 2 – dp [ i – j – 2 ] [ j ](kk:仍然不是很懂这个式子…)

那么我们就差最后一个了:i == j + 1的情况了。(因为 i – j – 2 就等于 -1 了,用递推公式会RE的)

这种情况只有一种,即是全是H。

那么它就为:dp [ i ] [ j ] = dp [ i – 1 ] [ j ] * 2 – 1

 

 

 

最后,要用到高精度,干脆写了java

 

 

 

 

 

 

BZOJ 1655: [Usaco2006 Jan] Dollar Dayz 奶牛商店 (母函数,高精度)

1655: [Usaco2006 Jan] Dollar Dayz 奶牛商店

Time Limit: 5 Sec  Memory Limit: 64 MB
Submit: 353  Solved: 190
[Submit][Status][Discuss]

Description

Farmer John goes to Dollar Days at The Cow Store and discovers an unlimited number of tools on sale. During his first visit, the tools are selling variously for $1, $2, and $3. Farmer John has exactly $5 to spend. He can buy 5 tools at $1 each or 1 tool at $3 and an additional 1 tool at $2. Of course, there are other combinations for a total of 5 different ways FJ can spend all his money on tools. Here they are: 1 @ US$3 + 1 @ US$2 1 @ US$3 + 2 @ US$1 1 @ US$2 + 3 @ US$1 2 @ US$2 + 1 @ US$1 5 @ US$1 Write a program than will compute the number of ways FJ can spend N dollars (1 <= N <= 1000) at The Cow Store for tools on sale with a cost of $1..$K (1 <= K <= 100).

    约翰到奶牛商场里买工具.商场里有K(1≤K≤100).种工具,价格分别为1,2,…,K美元.约翰手里有N(1≤N≤1000)美元,必须花完.那他有多少种购买的组合呢?

Input

A single line with two space-separated integers: N and K.

    仅一行,输入N,K.

Output

A single line with a single integer that is the number of unique ways FJ can spend his money.

    不同的购买组合数.

Sample Input

5 3

Sample Output

5
思路:母函数裸题,还卡个高精度。。差评。。。