Millionaire (APAC local onsites c)

Source: Internet
Author: User
Tags rounds

---restore content starts---

Test instructions: At first you have x dollars, you want to carry on M-rounds of gambling. The probability of each round of win is p, you can choose to bet and not bet, if you bet can also be held any part of the money as a bet (can be an integer, it can be a decimal). If you win, you'll double the bet, and you'll lose the bet. At the end of M-wheel gambling, if you hold more than $1 million, you can take the money home. Q: When you take the optimal strategy, you get more than 1 million yuan of money and take home the probability of how much.

Genre: Dynamic Planning & discretization Ideas

Analysis: Because each round of bets is arbitrary, not necessarily an integer, so there is an infinite number of possibilities, so even if you want to exhaustion search can not start. But if it can be dispersed continuously, then it may be limited. The following: Assuming that before the M-1 round of gambling, still hold X ' Yuan. For the final round, there are 3 things to consider. If X ' >= 1 million, then there is no need to bet the probability of the last round of 0, if 50<= x ' < 1 million, as long as the participation in gambling and bet >= 500,000 is the probability of winning p; if X ' < 500,000, then whether or not to participate in the last round of gambling, The probability of winning a bet is 0. We might as well take a look at the relationship between the penultimate and the last round, with the money held at X in the penultimate round. If x >= 1 million, the probability of winning is 1; if x < 250,000, even if the last two rounds of gambling win the total amount of money will be less than 1 million, so the probability of winning is 0; otherwise, just choose to participate in at least one round of gambling and bet at least 250,000 has the probability of winning. Assuming that the second-to-last bet is y (y = 0 or y >= 250,000), the final round holds the money x ' = (x + y) or x ' = (XY). And the second-to-last consideration of the situation can be divided into 5 kinds. In conclusion, when participating in M-wheel gambling, there are a total of 2^m + 1, which can be solved by DP. Define a two-dimensional DP array, Dp[i][j]: = Participate in the first round of gambling, holding the money in the module is J and take the optimal strategy when the probability of winning. Initialize: dp[n][1 << m] = 1, state transition equation Dp[i][j] = max (P * dp[i + 1][j + K] + (1-p) * dp[i + 1][j-k]/0 <= k <= min (j , N-j)). Time complexity O (m*2^2m).

Code implementation: The optimization of spatial complexity has been completed by using a rolling array loop

#include <iostream>
#include <memory.h>
#include <algorithm>
#include <cstdio>
using namespace Std;

int M, X;
Double P;
Double dp[2][(1 << 15) + 1];

void Solve ()
{
int n = 1 << M;
Double *pre = dp[0], *nxt = dp[1];
memset (pre, 0, sizeof (double) * (n + 1));
Pre[n] = 1.0;//: module n corresponds to the funds >= 1 million
for (int r = 0; r < M; r++)
{
for (int i = 0; I <= N; i++)
{
int step = min (i, n-i);//Avoid i + J > N
Double T = 0.0;

for (int j = 0; J <= Step; j + +)
t = max (t, P * pre[i + j] + (1-p) * pre[i-j]);//M-wheel gambling, minimum gambling should be 1 million/n
Nxt[i] = t;//with the funds of module I for R + 1 rounds of gambling, the maximum probability of winning
}
Swap (pre, NXT);
}
int i = x * n/1000000;//Capital x module
printf ("%.6lf\n", Pre[i]);
}

int main ()
{
CIN >> M >> P >> X;
Solve ();
return 0;
}

---restore content ends---

Millionaire (APAC local onsites c)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.