概率dp
题目:http://codeforces.com/gym/101606
Problem F Flipping Coins
Here’s a jolly and simple game: line up a row of N identical coins, all with the heads facing down onto the table and the tails upwards, and for exactly K times take one of the coins, toss it into the air, and replace it as it lands either heads-up or heads-down. You may keep all of the coins that are face-up by the end. Being, as we established last year, a ruthless capitalist, you have resolved to play optimally to win as many coins as you can. Across all possible combinations of strategies and results, what is the maximum expected (mean average) amount you can win by playing optimally?
Input • One line containing two space-separated integers: – N (1 ≤ N ≤ 400), the number of coins at your mercy; – K (1 ≤ K ≤ 400), the number of flips you must perform.
Output
Output the expected number of heads you could have at the end, as a real number. The output must be accurate to an absolute or relative error of at most 10−6.
Sample Input 1 Sample Output 1 2 1 0.5
Sample Input 2 Sample Output 2 2 2 1
Sample Input 3 Sample Output 3 2 3 1.25
Sample Input 4 Sample Output 4 6 10 4.63476563
Sample Input 5 Sample Output 5 6 300 5.5
题意:
输入n个硬币和投n次求最多硬币正面朝上的期望
ps;当所有面朝上时还会继续投
思路:dp(表示只会这一种dp)
代码:
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<queue> #include<set> #include<algorithm> #include<map> #define maxn 200005 using namespace std; int main() { int n,b; cin>>n>>b; double dp[405][405]; dp[0][0]=1; for(int i=1;i<=b;i++) for(int j=0;j<=i;j++){ if(j<n) { dp[i][j]+=dp[i-1][j]/2; dp[i][j+1]+=dp[i-1][j]/2; } else if(j==n) { dp[i][j-1]+=dp[i-1][j]/2; dp[i][j]+=dp[i-1][j]/2; } } double ans=0; for(int i=0;i<=n;i++) ans+=dp[b][i]*i; printf("%.8lf\n",ans); return 0; }
这里分享n种概率dp
http://www.cnblogs.com/kuangbin/archive/2012/10/02/2710606.html