https://code.google.com/codejam/contest/5254486/dashboard#s=p2 i am unable to solve the large subtask of this problem.. i solved the small one using brute force.. please help me understand the logic behind this problem.. thanks.
# | User | Rating |
---|---|---|
1 | tourist | 4009 |
2 | jiangly | 3823 |
3 | Benq | 3738 |
4 | Radewoosh | 3633 |
5 | jqdai0815 | 3620 |
6 | orzdevinwang | 3529 |
7 | ecnerwala | 3446 |
8 | Um_nik | 3396 |
9 | ksun48 | 3390 |
10 | gamegame | 3386 |
# | User | Contrib. |
---|---|---|
1 | cry | 167 |
2 | Um_nik | 163 |
3 | maomao90 | 162 |
3 | atcoder_official | 162 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 157 |
8 | TheScrasse | 154 |
9 | Dominater069 | 153 |
9 | nor | 153 |
https://code.google.com/codejam/contest/5254486/dashboard#s=p2 i am unable to solve the large subtask of this problem.. i solved the small one using brute force.. please help me understand the logic behind this problem.. thanks.
Name |
---|
I will try to explain my approach which I used to solve it. Since, the three operations involved are all bitwise operations, the final result of each bit in the answer depends only on its own initial value and not on any other bit. So, we can try to find the probability of getting a '1' in each bit. Since, the maximum value can be 10**9, I used 32 bits.
Now, for finding the probability of getting a '1' in a particular bit, can be solved using DP. Lets have an array P[2] in which P[0] denotes the probability of the bit being 0 and P[1] denotes the probability of the bit being 1. Initially, before passing it to any of the machines, the values of P[0] & P[1] will either be 0.0 and 1.0 or 1.0 and 0.0 respectively based on whether the particular bit is 1 or 0 in the given value x. Now we run it through all the N machines.
Now to find P[0] and P[1] after we pass it through a machine, we need to use the same bit in 'k' for which we are finding the answer since only this bit determines the answer. Also, the machine can either do 'AND', 'OR' or 'XOR' operation and so using these information we can find the new P[0] and P[1] value after we pass through a single machine. We repeat this process for all N machines and for all 32 bits. So, the total complexity will be O(N).
After we find the individual probability of each bit, the answer will be sum of pow(2,i)*P[i][1] for 0≤i≤31. Here P[i][1] denotes probability of ith bit being '1'.
My C++ code
thanks for such a nice and clear explanation.. :)
Thanks for the explanation but i couldn't understand the login used inside for loop for N machines to calculate n0, n1; Thanks;
Consider the following block in the code:
If the current bit we are considering in 'k' is a '1' then this block will be executed. As we know there are 3 operations — AND, OR and XOR. When we AND '1' with 'x', it becomes 'x'. So, with probability p[0]*(a/100) '0' will become '0' itself and with probability p[1]*(a/100) '1' will become '1' itself.
Next we will consider the OR operation with probability (b/100). When we OR '1' with 'x' we get '1'. So, the new probability of '1' will be (b/100)*(sum of p[0] and p[1]) since both '0' and '1' will result in '1' when ORed.
Finally, when we XOR '1' with 'x' we get '1-x' and so n0 will be p[1]*(c/100) since '1' will change to '0' when we XOR it and vice versa for n1.
With similar logic we do the else part when the current bit is not set in 'k' and we are done. Hope it is clear now.
very well explained! thanks a lot!