Problem
There is a certain "random number generator" (RNG) which takes one nonnegative integer as input and generates another nonnegative integer as output. But you know that the RNG is really not very random at all! It uses a fixed number K, and always performs one of the following three operations:
- with probability A/100: return the bitwise AND of the input and K
- with probability B/100: return the bitwise OR of the input and K
- with probability C/100: return the bitwise XOR of the input and K
(You may assume that the RNG is truly random in the way that it chooses the operation each time, based on the values of A, B, and C.)
You have N copies of this RNG, and you have arranged them in series such that output from one machine will be the input for the next machine in the series. If you provide X as an input to the first machine, what will be the expected value of the output of the final machine in the series?
Input
The first line of the input gives the number of test cases, T. T test cases follow; each consists of one line with six integers N, X, K, A, B, and C. Respectively, these denote the number of machines, the initial input, the fixed number with which all the bitwise operations will be performed (on every machine), and 100 times the probabilities of the bitwise AND, OR, and XOR operations.
Output
For each test case, output one line containing "Case #x: y", where x is the test case number (starting from 1) and y is the expected value of the final output. y will be considered correct if it is within an absolute or relative error of 10-9 of the correct answer. See the FAQ for an explanation of what that means, and what formats of real numbers we accept.
Limits
1 ≤ T ≤ 50.
0 ≤ A ≤ 100.
0 ≤ B ≤ 100.
0 ≤ C ≤ 100.
A+B+C = 100.
Small dataset
1 ≤ N ≤ 10.
0 ≤ X ≤ 104.
0 ≤ K ≤ 104.
Large dataset
1 ≤ N ≤ 105.
0 ≤ X ≤ 109.
0 ≤ K ≤ 109.
Sample
动态规划,X和K的位对齐,依次处理X和K的每一位通过N个转换器,例如X=1100,K=101,它们要处理的第一位是0和1;
经过第一个转换器:
0&1=0;0|1=1;0^1=1;
所以p(1)=b+c, p(0)=a;
经过第二个转换器:
1&1=1;1|1=1;1^1=0;
0&1=0;0|1=1;0^1=1;
所以p(1)=(b+c)*a+(b+c)*b+a*c+a*b; p(0)=(b+c)*c+a*a;
所以经过N个转换器后这一位贡献的期望是p(1)*bit;
#include <iostream>
#include <string>
#include <stdio.h>
using namespace std;
double dp[100005][2];
int main()
{
//freopen("/Users/fengguowen/Downloads/C-large-practice.in","r",stdin);
//freopen("/Users/fengguowen/Downloads/C-large-practice.out","w",stdout);
int T,t,N,X,K,A,B,C;
int i;
cin>>T;
for(t=1;t<=T;t++)
{
double ans=0;
cin>>N>>X>>K>>A>>B>>C;
double a=A*1.0/100;
double b=B*1.0/100;
double c=C*1.0/100;
int bx,bk;
int bit=1;
while(X||K)
{
bx=X&1;
bk=K&1;
X>>=1;
K>>=1;
dp[0][0]=0,dp[0][1]=0;
dp[0][bx]=1;
for(i=1;i<=N;i++)
{
dp[i][0]=0;
dp[i][1]=0;
dp[i][bk&1]+=a*dp[i-1][1];
dp[i][bk&0]+=a*dp[i-1][0];
dp[i][bk|1]+=b*dp[i-1][1];
dp[i][bk|0]+=b*dp[i-1][0];
dp[i][bk^1]+=c*dp[i-1][1];
dp[i][bk^0]+=c*dp[i-1][0];
}
ans+=dp[N][1]*bit;
bit<<=1;
}
printf("Case #%d: %.10lf\n",t,ans);
}
return 0;
}