Have you ever gambled? I wish you not. Being a professional gambler for three years, I learned every trick they play. Gambling is has nothing to do with luck. It is all about cheating.
One of the most elementary tricks is "mercury dice". Instead of being solid, there is a hollow chamber, filled with mercury, inside of the dice. This little modification makes the dice asymmetric, so that, the probability of each outcome is not equal. Imprecisely, which face is up, depends on the previous state.
To simplify the model, we use "mercury coin" here. As we mentioned, the state (head or tail) after tossing depends on the state before. The probability of that the two states are same, is p. For example, if the head side is up now, the probability that, the coin is still head up after tossing is p. And the probability that the tail side come up is 1-p. If p=0.5, it is a common coin. When p≠ 0.5, this coin is mercury coin. Assume the coin is head up now. After we toss Ntimes, what is the probability that, the number of heads is less than a certain number K?
Each test cases consists of three numbers
N K p
N and K are integers, and p is a float number.
N ≤ 20000
K ≤ N
0 ≤ p ≤ 1
3 2 0.6
0.448
#include<stdio.h>
#include<string.h>
const int MAXN=20010;
int arr[MAXN];
int k=0;
int n=0;
float p=0;
float sum=0;
void zuhe(int num,int start,int len,int *arr)
{
if(num==0)
{
int pre=1;
float psum=1;
int i=0;
for(;i<n;++i)
{
if(arr[i]==pre)
{
psum*=p;
}
else{
psum*=(1-p);
}
pre=arr[i];
//printf("%d ",arr[i]);
}
sum+=psum;
//printf("\n");
}
else if(num>len)
{
return;
}
else
{
arr[start]=1;
zuhe(num-1,start+1,len-1,arr);
arr[start]=0;
zuhe(num,start+1,len-1,arr);
}
}
int main()
{
scanf("%d%d%f",&n,&k,&p);
memset(arr,0,sizeof(arr));
int j=0;
for(;j<k;++j)
{
zuhe(j,0,n,arr);
}
printf("%.3f\n",sum);
return 0;
}