题目传送:蓝桥杯 - 概率计算
思路:概率DP,好久没做DP题了,居然1A了,好愉快,定义状态dp[i][j]表示取i个数时和为j的概率,则可以知道初始时dp[0][0] = 1;递推具体看代码
AC代码:
#include <cstdio> #include <cstring> #include <string> #include <cstdlib> #include <iostream> #include <algorithm> #include <cmath> #include <queue> #include <stack> #include <vector> #include <map> #include <set> #include <deque> #include <cctype> #define LL long long #define INF 0x7fffffff using namespace std; double dp[105][5055];//dp[i][j]表示取i个数时和为j的概率 int n, a, b, x; int main() { scanf("%d %d %d %d", &n, &a, &b, &x); memset(dp, 0, sizeof(dp)); dp[0][0] = 1; /*for(int i = 1; i < 5055; i ++) { dp[0][i] = 0; }*/ int sum = b - a + 1; for(int i = 1; i <= n; i ++) { for(int j = 0; j <= x; j ++) { for(int k = a; k <= b; k ++) { if(j >= k) dp[i][j] += dp[i-1][j-k] / sum; } } } printf("%.4lf\n", dp[n][x]); return 0; }
原文地址:http://blog.csdn.net/u014355480/article/details/45843437