HDU 4649 worker sor Tian (reverse state compression dp, probability)

Source: Internet
Author: User

 


Theme
At first, there is a number A0, and then a total of n numbers A1, a2.. An are given. Each number of these n numbers has An operator, &, |, ^

And the probability of each digit is pi.

If a number appears, it uses its operators to perform bitwise operations with the preceding number.

What is the final expectation?

 


Ideas
The official answer to this question is anti-state compression.

Once you know how to express the status, it is not difficult to do it. 1A after the game.

 


The official question solution is already very detailed and is no longer cumbersome:


Back-state compression-converts data to 20-bit 01 for Computation


Because there are only 20 bits, and &, |, ^ are not carried, then one bit is regarded as one, and each bit is either 0 or 1, in this way, the probability of each digit being 1 is obtained, multiplied by the decimal number of the digit, and accumulated, which gives the overall expectation.


For each bit, the state transition equation is as follows:


F [I] [j] indicates the number of I prior to this bit. The probability of j (0 or 1) is calculated.


F [I] [1] = f [I-1] [1] * p [I] + calculate the probability of 1 based on the values of different operators and I bits.


F [I] [0] is the same.


Initial status: f [0] [0 ~ 1] = 0 or 1 (set based on the first number)

F [n] [1]

 

 

 

 

Code

/** =================================================== ===== * This is a solution for ACM/ICPC problem ** @ source: HDU 4649 worker sor Tian * @ type: dp * @ author: shuangde * @ blog: blog.csdn.net/shuangde800 * @ email: zengshuangde@gmail.com * =================================================== =====*/# include <iostream> # include <cstdio> # include <cstring> # include <algorithm> # include <vector> # include <map> using namespace std; typedef lon G long int64; const int INF = 0x3f3f3f; const int MAXN = 210; int n, m; double f [MAXN] [2]; int A [MAXN]; char O [MAXN]; double p [MAXN]; int main () {char op [10]; int cas = 1; while (~ Scanf ("% d", & n) {for (int I = 0; I <= n; ++ I) scanf ("% d ", & A [I]); for (int I = 1; I <= n; ++ I) {scanf ("% s", op ); O [I] = op [0];} for (int I = 1; I <= n; ++ I) scanf ("% lf ", & p [I]); for (int I = 0; I <20; ++ I) f [0] [0] = (A [I]> I) & 1; double ans = 0; for (int I = 0; I <20; ++ I) {f [0] [1] = (A [0]> I) & 1; f [0] [0] =! F [0] [1]; for (int j = 1; j <= n; ++ j) {// The number j is not displayed, probability of 0 and 1 f [j] [0] = f [J-1] [0] * p [j]; f [j] [1] = f [J-1] [1] * p [j]; // Add the number j, probability of 0 and 1 if (O [j] = '^') {if (A [j]> I) & 1) {f [j] [0] + = f [J-1] [1] * (1-p [j]); f [j] [1] + = f [J-1] [0] * (1-p [j]);} else {f [j] [0] + = f [J-1] [0] * (1-p [j]); f [j] [1] + = f [J-1] [1] * (1-p [j]);} else if (O [j] = '&') {if (A [j]> I) & 1) {f [j] [0] + = f [J-1] [0] * (1-p [j]); f [j] [1] + = f [J-1] [1] * (1-p [j]);} else {f [j] [0] + = (f [J-1] [0] + f [J-1] [1]) * (1-p [j]); // f [j] [1]: if the j number is used, 1} else if (O [j] = '|') cannot appear here ') {if (A [j]> I) & 1) {// f [j] [0]: f [j] [1] + = (f [J-1] [0] + f [J-1] [1]) * (1-p [j]);} else {f [j] [0] + = f [J-1] [0] * (1-p [j]); f [j] [1] + = f [J-1] [1] * (1-p [j]);} ans = ans + f [n] [1] * (1 <I);} printf ("Case % d: \ n %. 6f \ n ", cas ++, ans);} return 0;} CODE piece from CODE

 

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.