7966. Another Dice Game
Constraints
Time Limit: 1 secs, Memory Limit: 256 MB , Special Judge
Description
In the game Pickomino one has to throw 8 dice to reach at least a certain target score. The rules are as follows:
- The dice contain the values 1, 2, 3, 4, 5 and worm. The dice are fair, so all outcomes are equally likely.
- The game is started by throwing all dice.
- After a throw, the player must pick one of the six possible values and put all dice with this value aside. There must be at least one die with this value.
- After putting some dice aside, the player may choose to either throw the remaining dice again or stop. The player may only stop after at least one worm has been put aside.
- Each possible value may only be chosen once during the game.
- When the player stops, his total score is the sum of the values of the dice that were put aside. A worm is worth 5 points.
- The player can get stuck by throwing only values that were already put aside, by having put all dice aside but not having a worm or by not having reached the target score.
- If the player is stuck he scores 0 points and the game is ended.
Jan is playing Pickomino and wants to score at least n points. When Jan uses an optimal strategy, what is the probability that he reaches this target?
Input
On the first line one positive number: the number of test cases, at most 100. After that per test case:
- one line with the integer n (1 <= n <= 40): the target value.
Output
Per test case:
- one line with a floating point number: the probability that you score at least n points when using an optimal strategy.
This number should be accurate up to 10-6 relative or absolute precision.
Sample Input
3 5 21 40
Sample Output
0.9934260978934218 0.8930267371507457 0.0001461079070016448
Hint
To reach 5 points it is enough to throw at least one worm. The optimal strategy in this case is therefore to stop as soon as you have a worm. If you did not throw a worm, you should put aside as few dice as possible to maximize the chance of throwing a worm in a later throw.
Problem Source
2013年每周一赛第五场暨校赛模拟赛III/BAPC 2012
// Problem#: 7966
// Submission#: 3593634
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
// Solution to Another Dice Game
// Author: Thomas Beuman
// Time complexity: O((n * dice * 2^6) * bin(n+5,5) * 6 )
// O( states * throws * options)
// Memory: O(n * dice * 2^6)
// @EXPECTED_RESULTS@: CORRECT
// Solution method: DFS with memorization
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
using namespace std;
typedef double dbl; // Use long double for greater accuracy
dbl Prob[41][9][64];
bool Seen[41][9][64];
int Points[6] = {1,2,3,4,5,5};
int Factorial[9] = {1,1,2,6,24,120,720,5040,40320};
// Returns probability of reaching target, given number of dice and values not yet put aside
dbl prob (int target, int dice, int available)
{ if (target <= 0 && !(available & 32)) // Target reached and worm put aside
return 1.0;
if (dice == 0) // Failed
return 0;
if (target < 0) // Only a worm left to get
target = 0;
if (Seen[target][dice][available]) // Already calculated
return Prob[target][dice][available];
int Number[6];
int i, perm;
dbl best, p = 0;
// Consider all possible outcomes of the throw
// First possibility: all ones
Number[0] = dice;
for (i = 1; i < 6; i++)
Number[i] = 0;
while (true)
{ // Determine number of permutations (= weight factor)
perm = Factorial[dice];
for (i = 0; i < 6; i++)
perm /= Factorial[Number[i]];
// Look for best strategy: try all options
best = 0;
for (i = 0; i < 6; i++)
if ((available & (1 << i)) && Number[i] > 0)
best = max(best, prob(target - Number[i]*Points[i], dice - Number[i], available ^ (1 << i)));
p += perm * best;
// Next possible outcome
for (i = 0; Number[i] == 0; i++); // Look for smallest value
if (i == 5)
break; // Done
Number[i+1]++; // Increase value of one die with smallest value by one
Number[0] = Number[i]-1; // Turn others into ones
if (i > 0)
Number[i] = 0;
}
Seen[target][dice][available] = true;
return Prob[target][dice][available] = p / pow(6.0, dice);
}
int main()
{ int cases, casenr, n;
scanf("%d\n", &cases);
memset(Seen, false, sizeof(Seen));
for (casenr = 1; casenr <= cases; casenr++)
{ scanf("%d", &n);
printf("%.10lf\n", prob(n, 8, 63));
}
return 0;
}