hdu4649 probability DP

Source: Internet
Author: User

http://acm.hdu.edu.cn/showproblem.php?pid=4649

Problem Descriptiontimer took the probability and mathematical Statistics course in the of the, but he bad attendance anger Ed Professor Tian who was in charge of the course. Therefore, Professor Tian decided to let Timer face a hard probability problem, and announced that if he fail to slove the Problem there would be no-on-Timer to pass the final exam.
As a result, Timer passed.
Now, also angered the Professor Tian when September Ends. You had to faced the problem too. The problem comes that there are an expression and you should calculate the excepted value of it. And the operators It may contains is ' & ' (and), ' | ' (or) and ' ^ ' (XOR) which is all bit operators. For Example:7&3=3, 5&2=0, 2|5=7, 4|10=14, 6^5=3, 3^4=7.
Professor Tian declares the operator Oi with its coming number ai+1 could disappear, and the probability that it Happe NS is Pi (0<i<=n).

Inputthe input contains several test cases. For each test case, there is a integer n (0<n<=200) on the first line. In the second line, there is n+1 integers, stand for {Ai}. The next line contains n operators, stand for {Oi}. The Forth line contains {Pi}.
Ai'll is less than, 0<=pi<=1.
Outputfor Each text case, you should print the number of the text case in the first line. Then output the excepted value of the expression, round to 6 decimal places.

Sample Input
2 3^ ^0.1 0.228 9 10^ ^0.5 0.7811 2&0.5

Sample Output
Case 1:0.720000case 2:4.940000case 3:0.500000

/**hdu4649 probability DP title to the main idea: given a bitwise operation of the expression of the connection, the first operator and the number after the occurrence and disappearance of the probability of its disappearance is pi, asking the expression of the last expected value is how much problem-solving ideas: Because of the effect of the calculation without carry, with dp[i][j][0~ 1] to indicate the number of pre-J 0 or 1 of the expectation of how much, then the final dp[i][n][1]* (1<<i) added the final result, the state transfer equation See code */#include <string.h> #include <io stream> #include <stdio.h> #include <algorithm>using namespace Std;int n,a[1003];d ouble dp[25][500][2],    P[500];char S[1003][2];int Main () {int tt=0;        while (~SCANF ("%d", &n)) {for (int i=0; i<=n; i++) {scanf ("%d", &a[i]);        } for (int i=1; i<=n; i++) {scanf ("%s", S[i]);        } for (int i=1; i<=n; i++) {scanf ("%lf", &p[i]);        } double ans=0;        Memset (Dp,0,sizeof (DP));            for (int i=0; i<=20; i++) {if (a[0]& (1<<i)) dp[i][0][1]=1;            else dp[i][0][0]=1;   for (int j=1; j<=n; J + +) {///J symbols and the numbers after them do not consider dp[i][j][0]+=dp[i][j-1][0]*p[j];              DP[I][J][1]+=DP[I][J-1][1]*P[J]; The first J symbol and the number following it do not take into account if (a[j]& (1<<i))///J number of the I bit is 1 {if (s[j][0]==                        ' & ') {dp[i][j][1]+=dp[i][j-1][1]* (1-p[j]);                    dp[i][j][0]+=dp[i][j-1][0]* (1-p[j]);                    } else if (s[j][0]== ' | ')                        {dp[i][j][1]+=dp[i][j-1][0]* (1-p[j]);                    dp[i][j][1]+=dp[i][j-1][1]* (1-p[j]);                        } else {dp[i][j][1]+=dp[i][j-1][0]* (1-p[j]);                    dp[i][j][0]+=dp[i][j-1][1]* (1-p[j]);                    }} else///the number of the first J is 0 {if (s[j][0]== ' & ')                        {dp[i][j][0]+=dp[i][j-1][1]* (1-p[j]);         dp[i][j][0]+=dp[i][j-1][0]* (1-p[j]);           } else if (s[j][0]== ' | ')                        {dp[i][j][0]+=dp[i][j-1][0]* (1-p[j]);                    dp[i][j][1]+=dp[i][j-1][1]* (1-p[j]);                        } else {dp[i][j][0]+=dp[i][j-1][0]* (1-p[j]);                    dp[i][j][1]+=dp[i][j-1][1]* (1-p[j]);        }}} ans+= (1<<i) *dp[i][n][1];    } printf ("Case%d:\n%.6lf\n", ++tt,ans); } return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

hdu4649 probability DP

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.