Problem C. Not So Random
Small input 11 points |
|
Large input 20 points |
|
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
In sample test case #1, the final output will be 5 if AND or OR happens and 0 if XOR happens. So the probability of getting 5 is (0.1 + 0.5) and the probability of getting 0 is 0.4. So the expected final output is 5 * 0.6 + 0 * 0.4 = 3.
In sample test case #2, the final output will be 5 with probability 0.72, and 0 otherwise.
思路:按照一般求期望的方法把式子列出来,然后把输入值x,k拆成二进制每位之和的形式做乘法分配率变换,重组发现可以转化为每位来算,而且异或,按位与,按位或这些运算都是位与位独立的。
样例1:
(5&5)* a/100 + (5|5) * b/100 + (5^5) * c/100
二进制表示:= (0101&0101)*a/100 + (0101|0101)*b/100 + (0101^0101)*c/100
=( (0100 + 1)&(0101))*a/100 + ( (0100 + 1)|(0101))*b/100 + ( (0100 + 1)^(0101))*c/100
=((0100&0100 + 1&0001)*a/100)) + ((0100|0100 + 1|0001)*b/100)) + ((0100^0100 + 1^0001)*c/100
=((1*2^2 + 1*2^0)*a/100 + ((1*2^2 + 1*2^0)*b/100+(1*2^2 + 1*2^0)*c/100
= 1*2^2*(a/100 + b/100 + c/100) + 1*2^0*(a/100 + b/100 + c/100)
这样每一位就独立出来了
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
const int N = 100010;
typedef long long ll;
using namespace std;
typedef long long ll;
int mod = 1000000007;
double dp[N][2] = {0};
int main()
{
freopen("in.txt","r",stdin);
freopen("output.txt", "w", stdout);
int n, x, k, a, b, c, T, cas = 1;
scanf("%d", &T);
while (T--)
{
double ans = 0;
scanf("%d%d%d%d%d%d", &n, &x, &k, &a, &b, &c);
int bit = 1;
while (x || k)
{
memset(dp, 0, sizeof(dp));
int tog = 0, cx = x&1, ck = k&1;
dp[0][cx] = 1;
for (int i = 0; i < n; i++)
{
dp[i+1][ck & 0] += dp[i][0] * a / 100;
dp[i+1][ck & 1] += dp[i][1] * a / 100;
dp[i+1][ck | 0] += dp[i][0] * b / 100;
dp[i+1][ck | 1] += dp[i][1] * b / 100;
dp[i+1][ck ^ 0] += dp[i][0] * c / 100;
dp[i+1][ck ^ 1] += dp[i][1] * c / 100;
}
ans += dp[n][1]*bit;
bit <<= 1;
x >>= 1;
k >>= 1;
}
printf("Case #%d: %.10f\n", cas++, ans);
}
return 0;
}