Practice Round APAC test 2017——Problem C. Not So Random

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 AB, 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, TT test cases follow; each consists of one line with six integers NXKAB, 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


Input 
 

Output 
 
3
1 5 5 10 50 40
2 5 5 10 50 40
10 15 21 70 20 10

Case #1: 3.0000000000
Case #2: 3.6000000000
Case #3: 15.6850579098



动态规划,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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值