ZOJ--3640(概率DP)
2015-01-02 19:15:59
思路:不难的概率DP,用记忆化写就非常方便了....(至于递推我想了好久没结果orz.....)
1 #include <cstdio> 2 #include <cstring> 3 #include <cstdlib> 4 #include <cmath> 5 #include <vector> 6 #include <map> 7 #include <set> 8 #include <stack> 9 #include <queue> 10 #include <iostream> 11 #include <algorithm> 12 using namespace std; 13 #define lp (p << 1) 14 #define rp (p << 1|1) 15 #define getmid(l,r) (l + (r - l) / 2) 16 #define MP(a,b) make_pair(a,b) 17 typedef long long ll; 18 typedef unsigned long long ull; 19 typedef pair<int,int> pii; 20 const int INF = (1 << 30) - 1; 21 const int maxn = 110; 22 const int maxval = 20010; 23 24 int N,f,c[maxn],t[maxn]; 25 int vis[maxn]; 26 double dp[maxval]; 27 28 double Solve(int p){ 29 if(dp[p]) return dp[p]; 30 double res = 0.0; 31 for(int i = 1; i <= N; ++i){ 32 if(p <= c[i]) res += (Solve(p + c[i]) + 1.0) / (double)N; 33 else res += t[i] / (double)N; 34 } 35 return dp[p] = res; 36 } 37 38 int main(){ 39 while(scanf("%d%d",&N,&f) != EOF){ 40 for(int i = 1; i <= N; ++i){ 41 scanf("%d",c + i); 42 t[i] = (int)((sqrt(5.0) + 1.0) / 2.0 * c[i] * c[i]); 43 } 44 memset(vis,0,sizeof(vis)); 45 memset(dp,0,sizeof(dp)); 46 printf("%.3f\n",Solve(f)); 47 } 48 return 0; 49 }