KingXMagicSpells 期望dp (记忆化搜索)
Posted nowing
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了KingXMagicSpells 期望dp (记忆化搜索)相关的知识,希望对你有一定的参考价值。
Used as: Division One - Level Two:
Value 500
Submission Rate 281 / 941 (29.86%)
Success Rate 226 / 281 (80.43%)
High Score wata for 473.56 points (6 mins 47 secs)
Average Score 323.08 (for 226 correct submissions)
As the XOR operation involves working with numbers in their binary form (base 2), this is the key to the solution. As we are looking for the expected number, and the operation are "bit independent" (ie a bit in position i isn‘t affected by other in position j iff i != j ), we can handle each bit separately (by invoking the awesome linearity of expectation). Hence, we just need to calculate the probability that a bit in the answer is ON (Or OFF).
Our algorithm will work on a set of ducks that initially in a particular room. That is, if a permutation occurs, we do not explicitly permute the rooms. Instead, we "move with the permutation" and remember the new location of our ducks instead. We then notice that this way, the XOR change is independent of the amount of ducks present in other rooms during the spell‘s effect.
Subproblem: Given a set of ducks initially located in a particular room and a bit position B, what is the probability that those ducks will end in room 0 and the bit B is ON?
This subproblem cames from the idea of independency. Both bits and ducks are independent and we can treat each case separately. This subproblem is much easier than the original problem, in fact, it can be solved by an elegant Dynamic Programming (DP) solution.
What is the DP state? There are 3 variables: The position of the set of ducks (Note that this changes with spell 2), the number of magick spells left and the value of the bit we are (Changes with spell 1).
How can we solved that state? We have two options, each with 50% probability (Or 0.5), cast the spell 1 or cast the spell 2. The solution is the sum of both probabilities.
What is the base case? The base case is when K = 0, ie when there aren‘t any spells left. If the duck is at position 0, the answer IS the bit in B-position
If you don‘t know how to work with bits, or some nice tricks you can do, this tutorial will certainly help you
bool isBitOn(int mask, int bit){
return ((mask & (1LL<<bit)) != 0LL);
}
vector
double &ret = dp[pos][K][isBitOn(val,B)];
if(ret != -1.0) return ret;
ret = 0.5 * rec(pos, K-1, B, val^spellOne[pos]); // first spell
ret += 0.5 * rec(spellTwo[pos], K-1, B, val); // second spell
return ret;
}
Back to the main problem
We have to merge all subproblems into the main one. It isn‘t difficult, if the probability a bit B is ON is P, 2^B * P will be the expected number. For instance, if P is 1, then it will always be on and the value will be 2^B.
double expectedNumber(vector
double ret = 0.0;
for(int bit = 0; bit<30; ++bit){
fill(&dp[0][0][0], &dp[54][0][0], -1.0);
for(int i = 0; i<ducks.size(); ++i)
ret += (1<<bit) * rec(i, K, bit, ducks[i]);
}
return ret;
}
以上是关于KingXMagicSpells 期望dp (记忆化搜索)的主要内容,如果未能解决你的问题,请参考以下文章