题干:
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.
题目大意:
给出N个硬币(N<=400),开始均反面朝上。每次挑出其中一个抛,连续抛K次(K<=400),求正面朝上的最大数学期望。
解题报告:
因为他说了是采取最佳策略,而不是每轮操作都随机选择一个,所以这题可以dp而不是直接算期望。
.
特别的,你会发现这个维度中,只有只有0.5倍转移了,还有0.5没有被转移
(不然你试试会发现最后)那么那0.5去哪了呢?
其实是因为每轮操作必须选择一个(这样才是古典概型嘛,如果已经都正面朝上就可以结束了,那就是几何概型了就不能dp了?我猜),所以转移到 那里去了。
例:n=2 时的dp转移表格(抛币次数为2)
AC代码:
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define F first
#define S second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX = 2e5 + 5;
int n,k;
double dp[555][555];
int main()
{cin>>n>>k;dp[0][0]=1;for(int i = 1; i<=k; i++) {for(int j = 0; j<=n; j++) {dp[i][j] = (j>=1 ? dp[i-1][j-1]*0.5 : 0) + dp[i-1][j]*0.5;}dp[i][n-1] += dp[i-1][n]*0.5;}double ans = 0;for(int i = 0; i<=n; i++) ans += dp[k][i]*i;printf("%.10f\n",ans);return 0 ;
}
举一反三:
n个正面朝下的硬币,每轮随机挑选一个硬币并上抛让它自由下落,这样进行K轮,问正面朝上的硬币个数的期望是多少?
n个正面朝下的硬币,每轮随机挑选一个硬币并翻转,这样进行K轮,问正面朝上的硬币个数的期望是多少?
答:dp(i,j)表示i次后有j个朝上的概率,因为有j个朝上,所以转移到dp(i+1,j-1)的概率是j/n,另一个转移类似。这种dp的方法不需要太动脑,至于更好的数学方法,推起来还是麻烦的。
n个正面朝下的硬币,每轮随机挑选一个硬币并翻转,进行K轮停止或硬币全部朝上停止,问正面朝上的硬币个数的期望是多少?