计蒜客:https://nanti.jisuanke.com/t/43468
题目描述:
Recently your town has been infested by swindlers who convince unknowing tourists to play a simple dice game with them for money. The game works as follows: given is an n-sided die,whose sides have 1, 2, . . . , n pips, and a positive integer k. You then roll the die, and then have to make a choice. Option 1 is to stop rolling. Option 2 is to reroll the die, with the limitation that the die can only be rolled k times in total. Your score is the number of pips showing on your final roll.
Obviously the swindlers are better at this game than the tourists are. You, proud supporter of the Battle Against ProbabilisticCatastrophes, decide to fight this problem not by banning the swindlers but by arming the tourists with information.
You create pamphlets on which tourists can find the maximum expected score for many values of n and k. You are sure that the swindlers will soon stop their swindling if the tourists are better prepared than they are!
The layout of the flyers is done, and you have distribution channels set up. All that is left to do is to calculate the numbers to put on the pamphlet.
Given the number of sides of the die and the number of times you are allowed to roll, calculate the expected (that is, average) score when the game is played optimally.
思路:
一个筛子有1~n个点面,一个游客最多可以掷k次,最后的得分是最后一次掷得的结果。求游客得分的期望值。
首先这道题里游客是个聪明的人(类似博弈),如果继续下去的期望小于当前的掷得的结果,就停止游戏。
假设剩余n次机会的期望值为dp[n],有:
即剩余的n种可能,每次取i和dp[n-1]种最大的结果(这是游客可以决定的)。
举个例子:n=6,k=2,dp[1]=(1+6)*6/2/6=3.5
第一次如果掷得的结果为1~3就继续游戏,因为1~3<3.5;
如果掷得的结果为4~6就结束游戏,因为3.5<4~6.
代码:
#include<iostream> #include<string.h> #include<cstdio> #include<cmath> #include<algorithm> #include<queue> #include<vector> #include<string> #include<set> #include<map> using namespace std; typedef pair<int,int> PII; typedef long long LL; const int N=110; double dp[N]; int sum(int x,int y){ return (x+y)*(y-x+1)/2; } int main(){ int n,k; cin>>n>>k; double ans=0; dp[0]=0; for(int i=1;i<=k;++i){ dp[i]=1.0*(1.0*int(dp[i-1])*dp[i-1]+1.0*sum(int(dp[i-1])+1,n))/(n*1.0); } printf("%.7lf",dp[k]); return 0; }