Dice war is a board game. In each turn, the attacker will choose a pile of dices to attack the defender's pile of dices. They will roll the all dices, and the attacker will win if and only if the total number on all his dices is larger than the defenders. After a successful attack, the attacker's pile of dices will split into two piles, while one of them has only one dice. If the attacker loses, this pile will lose all dices except one. An attack cannot be made with a pile of one single dice.
Input
Multiple cases. Each case contains two number of dices in the attacker's pile and the diffender's pile. In the game, number of dices in a pile will be no more than 8.Output
For each case, output the probability of attack success in a single line. Error no more than 1e-4 will be accepted.Sample Input
3 2 2 1
Sample Output
0.7785493827160493 0.8379629629629629
题意:给出n个骰子和m个骰子,问n个骰子赢过m个骰子的概率
思路:骰子范围不大,8个以内,可8个for循环暴力,可递归。不过写写还是dp简单。用dp[i][j]表示在i个骰子的情况下,和是j的情况有多少种。这个可以从0个的时候开始推,即dp[0][0]=1,然后加上一颗骰子推到下一层,每一层都由当层所能到的数值加上一颗骰子推到下一层。
然后就是算出所有答案ans[i][j],即i个骰子赢过j个骰子的概率。只要枚举i个骰子所能到的所有情况(i~i*6),和每一种情况对应j个骰子比它小的情况(j~i-1),两者想乘一直累加到最后,除以6^(i+j),即是最后答案
#include<string>
#include<cstdio>
#include<cstring>
#include<cmath>
long long dp[9][70];
double ans[9][9];
void init(){
memset(dp,0,sizeof(dp));
dp[0][0]=1;
for(int i=0;i<=7;i++)
for(int j=i;j<=i*6;j++)
for(int k=1;k<=6;k++)
dp[i+1][j+k]+=dp[i][j];
for(int i=1;i<=8;i++)
for(int j=1;j<=8;j++){
long long res=0;
for(int k=i;k<=6*i;k++)
for(int l=j;l<k;l++)
res+=dp[i][k]*dp[j][l];
ans[i][j]=res*1.0/pow(6,i+j);
}
}
int main(void){
init();
int a,b;
while(~scanf("%d%d",&a,&b)){
printf("%lf\n",ans[a][b]);
}
return 0;
}