UVA_10328
This is very similar to the previous topic that calculates the probability that the number of consecutive victories is k, so I moved the idea used for that question.
We set f [I] [j] to indicate the total number of consecutive H numbers not exceeding j when throwing the I-th coin. Since the I-th coin can be positive or negative, therefore, you may first write f [I] [j] = 2 * f [I-1] [j].
But in some cases, this is incorrect. What is the situation? That is, the last j of the coin in front of the I-1 is H, then the I if it is H again, the total length will become j + 1.
When subtracting this part of the number, we will discuss the situation in detail. If I = j + 1, then there is only one case where j + 1 H is displayed, that is, all I coins are H, so subtract 1. If I> j + 1, then if j H is in front of the I coin, then the i-j-1 coin must be T, otherwise, the continuous number of H will exceed j, which cannot be included in f [I-1] [j]. Therefore, we just need to subtract an f [i-j-2] [j.
The final result should obviously be Output f [n] [n]-f [n] [k-1].
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
BigInteger[][] f = new BigInteger[110][110];
for(int i = 0; i <= 100; i ++)
f[0][i] = new BigInteger("1");
f[1][0] = new BigInteger("1");
for(int i = 1; i <= 100; i ++)
f[1][i] = new BigInteger("2");
for(int i = 2; i <= 100; i ++)
for(int j = 0; j <= 100; j ++)
{
f[i][j] = f[i - 1][j].multiply(BigInteger.valueOf(2));
if(i == j + 1)
f[i][j] = f[i][j].add(BigInteger.valueOf(-1));
else if(i > j + 1)
f[i][j] = f[i][j].add(f[i - j - 2][j].negate());
}
while(cin.hasNext())
{
int n = cin.nextInt();
int k = cin.nextInt();
System.out.println(f[n][n].add(f[n][k - 1].negate()));
}
}
}