正推,到达i的概率为p[i],要注意除了1和n外,到达i的概率并不一定为1
概率表达式为p[i] += p[j] / min(n - j, 6)
从j带过来的期望为exp[i] += exp[j] / min(n - j, 6)
又到达i时有价值val[i],到达i的概率为p[i],故exp[i] += val[i] * p[i]
#include<cstdio> #include<iostream> #include<cstdlib> #include<cstring> #include<string> #include<algorithm> #include<map> #include<queue> #include<vector> #include<utility> using namespace std; typedef long long LL; const int N = 1008, INF = 0x3F3F3F3F; #define MS(a, num) memset(a, num, sizeof(a)) #define PB(A) push_back(A) #define FOR(i, n) for(int i = 0; i < n; i++) double exp[N], p[N]; int val[N]; int main(){ int t; cin>>t; for(int cas= 1; cas <= t;cas++){ int n; cin>>n; for(int i =1; i <= n; i++){ scanf("%d", &val[i]); } p[1] = 1; exp[1] = val[1]; for(int i = 2; i <= n; i++){ exp[i] = p[i] = 0; for(int j = max(1, i - 6); j < i; j++){ exp[i] += exp[j] / min(n - j, 6); p[i] += p[j] / min(n - j, 6); } exp[i] += val[i] * p[i]; } printf("Case %d: %.10f\n", cas, exp[n]); } return 0; }
时间: 2024-11-04 15:27:09