Hdu 4696 reverse compression + Dynamic Planning

Source: Internet
Author: User

# Include <iostream> # include <stdio. h> # include <algorithm> # include <cmath> # include <string. h> using namespace std; # define Maxn 210int A [Maxn]; double p [Maxn]; char str [Maxn]; int Arr [Maxn] [25]; double f [Maxn] [2]; int n; void Bit (int x) <SPAN style = "WHITE-SPACE: pre "> </SPAN> // convert the number of current locations to binary {int t = 0; int tt = A [x]; while (tt) {Arr [x] [t ++] = tt & 1; tt >>=1 ;}} void Solve (int x) {int I, j; memset (f, 0, sizeof (f); if (Arr [0] [x]) F [0] [1] = 1; else f [0] [0] = 1; for (I = 1; I <= n; I ++) {f [I] [1] = f [I-1] [1] * p [I]; <SPAN style = "WHITE-SPACE: pre "> </SPAN> // If no, then the probability of the Current Position 01 is the probability of the previous position 01 f [I] [0] = f [I-1] [0] * p [I]; if (str [I] = '&') {<SPAN style = "WHITE-SPACE: pre"> </SPAN> // according to &, |, ^ returns the probability of the current 01 if (Arr [I] [x]) {f [I] [1] + = f [I-1] [1] * (1-p [I]); f [I] [0] + = f [I-1] [0] * (1-p [I]);} else {f [I] [0] + = f [I-1] [1] * (1-p [I]); f [I] [0] + = f [I-1] [0] * (1-p [I]);} if (str [I] = '| '){ If (Arr [I] [x]) {f [I] [1] + = f [I-1] [1] * (1-p [I]); f [I] [1] + = f [I-1] [0] * (1-p [I]);} else {f [I] [1] + = f [I-1] [1] * (1-p [I]); f [I] [0] + = f [I-1] [0] * (1-p [I]);} if (str [I] = '^ ') {if (Arr [I] [x]) {f [I] [0] + = f [I-1] [1] * (1-p [I]); f [I] [1] + = f [I-1] [0] * (1-p [I]);} else {f [I] [0] + = f [I-1] [0] * (1-p [I]); f [I] [1] + = f [I-1] [1] * (1-p [I]); }}/ * Because bitwise operations do not affect carry, therefore, you only need to calculate the probability of 1 occurrence for each digit. When solving the expectation, convert it to a 10-digit system. */Int main () {int I, j, tt = 1; while (~ Scanf ("% d", & n) {memset (Arr, 0, sizeof (Arr); for (I = 0; I <= n; I ++) {scanf ("% d", & A [I]); Bit (I) ;}getchar (); for (I = 1; I <= n; I ++) {str [I] = getchar (); getchar () ;}for (I = 1; I <= n; I ++) scanf ("% lf ", & p [I]); double ans = 0; int tmp = 1; for (I = 0; I <20; I ++) {Solve (I ); ans + = tmp * f [n] [1]; tmp <= 1;} printf ("Case % d: \ n", tt ++ ); printf ("% 0.6lf \ n", ans);} return 0 ;}

 

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.