P1409 dice

Title Description

n individuals in a row, the m-th row you. The first team of each round had cast a dice:

If the throw to 1, the first team who wins;

If the throw to 2,4,6, the head of the queue of people discharged to the tail;

If the throw to 3,5, the head of the team of people from the team.

The winner only one person, if one person remaining in the queue, that person wins. Ask your chances of winning.

Input Format

Row of two integers n, m

Output Format

A real number represents the probability of winning (9 decimal place)

Sample input and output

Input # 1
2 1
Output # 1
0.375000000

Description / Tips

【data range】

For 30% of the data: n <= 10

To 100% of the data: n <= 1000, m <= n

Code

#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

const int N=10010;

int n,m;
double f[N][N];

int main () {
	scanf("%d%d",&n,&m);
	f[1][1]=1;
	for(int i=2; i<=n; i++) {
		double x=(double)1/2,c=(double)1/6;
		for(int j=2; j<=i; j++) {
			x=x/2;
			c=c/2+f[i-1][j-1]/3;
		}
		f[i][i]=c/(1-x);
		f[i][1]=f[i][i]/2+(double)1/6;
		for(int j=2; j<i; j++)
			f[i][j]=f[i][j-1]/2+f[i-1][j-1]/3;
	}
	printf("%.9lf\n",f[n][m]);
	return 0;
}

 

Guess you like

Origin www.cnblogs.com/mysh/p/11486027.html