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.
H
H
H
H
H
T
H
T
H
H
T
T
T
H
H
T
H
T
T
T
H
T
T
T
HHH \ HHT \ HTH \ HTT \ THH \ THT \ TTH \ TTT
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.
In the above example we see 1 sequence 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 2n times. How many sequence you will get which contains a sequence of H of length at least k.
Input
The input will start with two positive integer, n and k (1 ≤ k ≤ n ≤ 100). Input is terminated by EOF.
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
问题链接:UVA10328 Coin Toss
问题简述:(略)
问题分析:DP问题,不解释。
程序说明:(略)
参考链接:(略)
题记:(略)
AC的Java语言程序如下:
/* UVA10328 Coin Toss */
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int N = 100;
BigInteger a, b;
BigInteger dp[][] = new BigInteger[N + 1][N + 1];
for (int i = 0; i <= N; i++) {
dp[0][i] = BigInteger.ONE;
dp[i][0] = BigInteger.ONE;
dp[1][i] = BigInteger.valueOf(2);
}
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= N; j++) {
if (i <= j)
dp[i][j] = dp[i - 1][j].multiply(BigInteger.valueOf(2));
else if (i == j + 1)
dp[i][j] = dp[i - 1][j].multiply(BigInteger.valueOf(2)).subtract(BigInteger.ONE);
else
dp[i][j] = dp[i - 1][j].multiply(BigInteger.valueOf(2)).subtract(dp[i - j - 2][j]);
}
}
Scanner in = new Scanner(System.in);
while (in.hasNext()) {
a = BigInteger.valueOf(in.nextInt());
b = BigInteger.valueOf(in.nextInt()).subtract(BigInteger.ONE);
System.out.println(dp[a.intValue()][a.intValue()].subtract(dp[a.intValue()][b.intValue()]));
}
in.close();
}
}