LightOj 1030 (Discovering Gold)

#lightoj #cp #problem_solving #dp #expected_value #probability

Idea


Expected Value

  • The expectation at i is E(i) = (E(i+1) + E(i+2) + E(i+3) + E(i+4) + E(i+5) + E(i+6)) / cnt + gold[i]
    where cnt is how many of E(i+x) is valid
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93

/**Which of the favors of your Lord will you deny ?**/

#include<bits/stdc++.h>
using namespace std;

#define LL long long
#define PII pair<int,int>
#define PLL pair<LL,LL>
#define MP make_pair
#define F first
#define S second
#define INF INT_MAX

inline void optimizeIO()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
}

const int nmax = 1e2+7;
const LL LINF = 1e17;

string to_str(LL x)
{
stringstream ss;
ss<<x;
return ss.str();
}

long double ara[103];
long double dp[103];
int n;

long double solve(int pos)
{
if(pos==n-1)
return ara[pos];

long double &ret = dp[pos];

if(ret>=0.0)
return ret;

ret = 0;
int cnt = 0;

for(int i=1; i<=6; i++)
{
if(pos+i<n)
{
ret += ara[pos] + solve(pos+i);
//cout<<i<<" -> "<<ret<<endl;
}
else
cnt++;

}

ret /= (6.0-cnt);

return ret;
}


int main()
{
optimizeIO();

int tc;
cin>>tc;

for(int q=1; q<=tc; q++)
{
cin>>n;

for(int i=0; i<n; i++)
cin>>ara[i];

memset(dp,-1,sizeof dp);

long double ans = solve(0);

cout << fixed << showpoint;
cout << setprecision(10);

cout<<"Case "<<q<<": "<<ans<<endl;

}

return 0;
}