Skip to content

Instantly share code, notes, and snippets.

@kaikai2
Last active December 10, 2015 02:39
Show Gist options
  • Save kaikai2/4369450 to your computer and use it in GitHub Desktop.
Save kaikai2/4369450 to your computer and use it in GitHub Desktop.
A丢100个6面骰子,B丢160个4面骰子。那么A的点数总和比B的点数总和大的几率以及相等的几率分别该怎么计算? http://weibo.com/2039631434/zaMYL2jOj
/*
A丢100个6面骰子,B丢160个4面骰子。那么A的点数总和比B的点数总和大的几率以及相等的几率分别该怎么计算?
http://weibo.com/2039631434/zaMYL2jOj
*/
#include <cstdio>
#include <cassert>
template<int n,int dice>
double f(int d)
{
static double fn[n+1][n*dice+1] = {0};
static bool inited = false;
if (!inited)
{
fn[0][0] = 1;
for (int j = 1; j <= n; j++)
{
for (int i = j; i <= j*dice; i++)
{
double p = 0;
for (int k = 1; k <= dice; k++)
{
if (i >= k)
p += fn[j-1][i - k] / (double)dice;
}
fn[j][i] = p;
}
}
inited = true;
}
assert(d <= n*dice+1);
return fn[n][d];
}
double p_equal()
{
double p = 0;
for (int d = 160; d <= 600; d++)
{
p += f<100, 6>(d) * f<160, 4>(d);
}
return p;
}
double p_6_gt_4()
{
double p = 0;
for (int a = 161; a <= 600; a++)
{
for (int b = 160; b < a; b++)
{
p += f<100, 6>(a) * f<160, 4>(b);
}
}
return p;
}
int main()
{
printf("%lf\n%lf\n", p_equal(), p_6_gt_4());
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment