Coin Toss(UVA

来源:互联网 发布:ios多线程编程 面试 编辑:程序博客网 时间:2024/06/06 01:38
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.

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

题意:n个硬币,抛了以后,至少k个相邻情况的个数;

思路:dp[i][j] 为ig硬币,至少j个相邻的情况,如果i>j+1,dp[i][j]=dp[i-1][j]*2-dp[i-j-2][j],如果i==j+1,dp[i][j]=dp[i-1][j]*2-1;

ps:数太大,也没有mod,所以用java大数写简单;

import java.util.*;import java.math.*;public class Main {public static void main(String[] args) {// TODO Auto-generated method stubScanner input=new Scanner(System.in);BigInteger dp[][]=new BigInteger[105][105];for(int i=0;i<=100;i++){dp[i][0]=BigInteger.ONE;dp[0][i]=BigInteger.ONE;dp[1][i]=BigInteger.valueOf(2);}for(int i=2;i<=100;i++){for(int j=1;j<=100;j++){dp[i][j]=dp[i-1][j].multiply(BigInteger.valueOf(2));if(i==j+1)dp[i][j]=dp[i][j].subtract(BigInteger.ONE);else if(i>j+1)dp[i][j]=dp[i][j].subtract(dp[i-j-2][j]);}}while(input.hasNext()){int n=input.nextInt();int k=input.nextInt();System.out.println(dp[n][n].subtract(dp[n][k-1]));}}}


原创粉丝点击