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

探讨了通过一系列固定操作的随机数生成器(RNG),输入整数经过多个相同RNG串联后的输出期望值计算方法,采用动态规划解决。

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;
}


### ### 常见原因分析 命令 `cd "D:\VSproject\ASL practice\ASL practice"` 未生效,可能的原因包括路径中存在空格或特殊字符、当前 shell 不支持直接跳转到深层嵌套目录,或者权限问题导致无法访问目标路径。此外,Windows 命令行环境对路径的解析方式与 Linux 有所不同,特别是在使用引号和转义字符时需要特别注意。 --- ### ### 路径中包含空格 如果路径 `D:\VSproject\ASL practice\ASL practice` 中包含空格,例如 `"ASL practice"`,则必须使用双引号包裹整个路径,否则命令会失败。虽然用户已经使用了引号,但在某些旧版本的 CMD 或批处理脚本中仍可能出现解析错误。确保路径中的每一级目录名称都被正确识别,并且没有隐藏字符或非法符号[^1]。 ```cmd cd "D:\VSproject\ASL practice\ASL practice" ``` 建议尝试将文件夹重命名为不含空格的形式(如 `ASL_practice`),以排除潜在的路径解析问题。 --- ### ### 当前工作目录不在目标盘符下 在 Windows 命令提示符中切换盘符时,需要单独执行盘符切换命令。例如,若当前位于 `C:` 盘而目标路径位于 `D:` 盘,则需先切换盘符再进入目录: ```cmd D: cd "VSproject\ASL practice\ASL practice" ``` 如果在同一盘符下操作,可以直接使用 `cd` 进入目标路径;但跨盘符时必须显式切换盘符,否则当前工作目录不会改变[^2]。 --- ### ### 权限不足或路径不存在 如果目标路径 `D:\VSproject\ASL practice\ASL practice` 实际上并不存在,或者当前用户没有访问该路径的权限,`cd` 命令也会失败。可以使用 `dir` 命令检查目标路径是否存在,以及是否具有读取权限: ```cmd dir D:\VSproject\ASL practice\ASL practice ``` 如果路径不存在,应手动创建目录结构或确认路径拼写是否正确。同时,部分受保护的系统路径可能需要管理员权限才能访问,此时应以管理员身份运行命令提示符[^3]。 --- ### ### 使用 PowerShell 替代 CMD 如果上述方法仍然无法解决问题,建议尝试使用 PowerShell 替代传统的 CMD 环境。PowerShell 对路径的支持更强大,能够更好地处理长路径和空格问题: ```powershell Set-Location "D:\VSproject\ASL practice\ASL practice" ``` PowerShell 提供了更灵活的路径操作函数,例如自动补全和路径验证功能,有助于减少因路径格式问题导致的错误[^4]。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值