本文共 1348 字,大约阅读时间需要 4 分钟。
题目:
用到了概率论中的,而中须要用到的概率须要用dp方法求解。
代码:
#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std;#define CHECKTIME() printf("%.2lf\n", (double)clock() / CLOCKS_PER_SEC)typedef pair pii;typedef long long llong;typedef pair pll;#define mkp make_pair/*************** Program Begin **********************/const int MAX_SCORE = 50 * 50;class FixedDiceGameDiv1 {public: double dp1[MAX_SCORE + 1], dp2[MAX_SCORE + 1]; // dp[i]: roll a b-sieded dice 最后总得分为i的概率 void calc(int a, int b, double dp[]) { for (int i = 0; i < MAX_SCORE + 1; i++) { dp[i] = 0.0; } dp[0] = 1.0; for (int i = 0; i < a; i++) { for (int j = a * b; j >= 0; j--) { if (dp[j] == 0) { continue; } for (int k = 1; k <= b; k++) { dp[j + k] += dp[j] / b; } dp[j] = 0; } } } double getExpectation(int a, int b, int c, int d) { double res = 0.0; calc(a, b, dp1); calc(c, d, dp2); // 贝叶斯公式 double up = 0, down = 0; for (int i = a; i <= a * b; i++) { for (int j = 0; j < i; j++) { up += dp1[i] * dp2[j] * i; down += dp1[i] * dp2[j]; } } if (down == 0) { return -1; } res = up / down; return res; }};/************** Program End ************************/
转载地址:http://aoxgx.baihongyu.com/