24h購物| | PChome| 登入
2013-06-04 09:04:43| 人氣1,224| 回應0 | 上一篇 | 下一篇

[UVA][dp、大數] 10328 - Coin Toss

推薦 0 收藏 0 轉貼0 訂閱站台


 Coin Toss 

Toss is an important part of any event. When everything becomes equal toss is the ultimate decider. Normally a fair coin is used for Toss. A coin has two sides head(H) and tail(T). Superstition may work in case of choosing head or tail. If anyone becomes winner choosing head he always wants to choose head. Nobody believes that his winning chance is 50-50. However in this problem we will deal with a fair coin and n times tossing of such a coin. The result of such a tossing can be represented by a string. Such as if 3 times tossing is used then there are possible 8 outcomes.


HHH HHT HTH HTT THH THT TTH TTT

As the coin is fair we can consider that the probability of each outcome is also equal. For simplicity we can consider that if the same thing is repeated 8 times we can expect to get each possible sequence once.

The Problem

In the above example we see 1 sequnce has 3 consecutive H, 3 sequence has 2 consecutive H and 7 sequence has at least single H. You have to generalize it. Suppose a coin is tossed n times. And the same process is repeated 2^n times. How many sequence you will get which contains a consequnce of H of length at least k.

The Input

The input will start with two positive integer, n and k (1<=k<=n<=100). Input is terminated by EOF.

The Output

For each test case show the result in a line as specified in the problem statement.

Sample Input

4 1
4 2
4 3
4 4
6 2

Sample Output

15
8
3
1
43

Md. Kamruzzaman



dp[length][k][tail]

記錄骰了 length 次,曾經出現過連續最大 k 次,當前尾巴有連續 tail 次。

則如果下一步是 'H'

dp[length+1][max(k, tail+1)][tail+1] += dp[length][k][tail]

如果下一步是 'T'

dp[length+1][k][0] += dp[length][k][0]

最後進行大數處理。


#include <stdio.h>
#include <algorithm>
using namespace std;


short dp[105][105][105][40];//[length][k][tail]
short g[105][105][40];
int main() {
int i, j, k, p, q;
dp[0][0][0][0] = 1;
for(i = 0; i <= 100; i++) {
for(j = 0; j <= i; j++) {
for(k = 0; k <= i; k++) {
for(p = 0; p < 40; p++) {
g[i][j][p] += dp[i][j][k][p];
if(g[i][j][p] >= 10) {
g[i][j][p+1] += g[i][j][p]/10;
g[i][j][p] %= 10;
}
}
// H
for(p = 0; p < 40; p++) {
dp[i+1][max(j, k+1)][k+1][p] += dp[i][j][k][p];
if(dp[i+1][max(j, k+1)][k+1][p] >= 10) {
dp[i+1][max(j, k+1)][k+1][p+1] += dp[i+1][max(j, k+1)][k+1][p]/10;
dp[i+1][max(j, k+1)][k+1][p] %= 10;
}
}
// T
for(p = 0; p < 40; p++) {
dp[i+1][j][0][p] += dp[i][j][k][p];
if(dp[i+1][j][0][p] >= 10) {
dp[i+1][j][0][p+1] += dp[i+1][j][0][p]/10;
dp[i+1][j][0][p] %= 10;
}
}
}
}
}
/*for(i = 0; i <= 100; i++) {
for(j = 0; j <= i; j++) {
for(k = 0; k <= i; k++) {
g[i][j] += dp[i][j][k];
// H
dp[i+1][max(j, k+1)][k+1]
+= dp[i][j][k];
// T
dp[i+1][j][0]
+= dp[i][j][k];
}
}
}
for(i = 0; i <= 100; i++)
for(j = i; j >= 0; j--)
g[i][j] += g[i][j+1];*/
for(i = 0; i <= 100; i++) {
for(j = i; j >= 0; j--) {
for(p = 0; p < 40; p++) {
g[i][j][p] += g[i][j+1][p];
if(g[i][j][p] >= 10) {
g[i][j][p+1] += g[i][j][p]/10;
g[i][j][p] %= 10;
}
}
}
}
while(scanf("%d %d", &i, &j) == 2) {
p = 39;
while(g[i][j][p] == 0) p--;
while(p >= 0) {
printf("%d", g[i][j][p]);
p--;
}
puts("");
}
return 0;
}

台長: Morris
人氣(1,224) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][窮舉] 11205 - The broken pedometer
此分類上一篇:[UVA][最短路] 12295 - Optimal Symmetric Paths

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文