Va 10328 Coin Toss

Source: Internet
Author: User

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()));
}
}
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.