Binary Operations

Time Limit: 2000 ms Memory Limit: 65535 kB Solved: 56 Tried: 674

 

Description

 

Bob has a sequence of N integers. They are so attractive, that Alice begs to have a continued part of it(a continued part here also means a continued subsequence). However, Bob only allows Alice to chose it randomly. Can you give the expectation of the result of the bitwise AND, OR, XOR of all the chosen numbers?

 

Input

 

First line of the input is a single integer T(1 <= T <= 50), indicating there are T test cases.

The first line of each test case contains a single number N(1 <= N <= 50000).

The second line contains N integers, indicating the sequence.

All the N integers are fit in [0, 10^9].

 

Output

 

For each test case, print "Case #t: a b c" , in which t is the number of the test case starting from 1, a is the expectation of the bitwise AND, b is the expectation of OR and c is the expectation of XOR.

Round the answer to the sixth digit after the decimal point.

 

Sample Input

 

3
2
1 2
3
1 2 3
3
2 3 4

 

Sample Output

 

Case #1: 1.000000 2.000000 2.000000
Case #2: 1.333333 2.500000 1.666667
Case #3: 1.833333 4.333333 3.666667

 

Hint

 

AND is a binary operation, performed on two numbers in binary notation. First, the shorter number is prepended with leading zeroes until both numbers have the same number of digits (in binary). Then, the result is calculated as follows: for each bit where the numbers are both 1 the result has 1 in its binary representation. It has 0 in all other positions.

OR is a binary operation, performed on two numbers in binary notation. First, the shorter number is prepended with leading zeroes until both numbers have the same number of digits (in binary). Then, the result is calculated as follows: for each bit where the numbers are both 0 the result has 0 in its binary representation. It has 1 in all other positions.

XOR is a binary operation, performed on two numbers in binary notation. First, the shorter number is prepended with leading zeroes until both numbers have the same number of digits (in binary). Then, the result is calculated as follows: for each bit where the numbers differ the result has 1 in its binary representation. It has 0 in all other positions.

 

Source

 

Sichuan State Programming Contest 2012

 /*

 求期望,如果暴力,超时。上次比赛没有人做出来,看了解题思路,省赛完了再来写的

用位运算,每次处理32次。
如果暴力,虽然前面比较少,但是,随着n的增大,次数将增加很大。 在//!!!处,原来不知道怎么处理,后来推导过程得出的。 */ #include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std;
typedef long long LL; LL a[];//看开始用int,错误了。
LL f[];
LL tmp[];
LL vis[]; void make_init(LL x)
{
LL i;
memset(f,,sizeof(f));
i=;
while(x)
{
f[i]=x&;
x=x>>;
i--;
}
} void solve(LL t,LL n)
{
LL i,j;
LL Sum=n*(n+)/;
LL XOR=,AND=,OR=;
printf("Case #%lld: ",t); memset(tmp,,sizeof(tmp));
memset(vis,,sizeof(vis));
for(i=; i<=n; i++)
{
make_init(a[i]); if(i==)
{
for(j=; j<=; j++)
{
tmp[j]=f[j];
vis[j]=f[j];
}
}
else
{
for(j=; j<=; j++)
{
if(f[j]==)tmp[j]=;
tmp[j]+=f[j];
}
for(j=; j<=; j++)
vis[j]=vis[j]+tmp[j];
}
}
for(i=; i<=; i++)
AND=AND*+vis[i];
printf("%.6lf",(double)(AND*1.0/Sum)); memset(tmp,,sizeof(tmp));
memset(vis,,sizeof(vis));
for(i=; i<=n; i++)
{
make_init(a[i]);
if(i==)
{
for(j=; j<=; j++)
{
tmp[j]=f[j];
vis[j]=f[j];
}
}
else
{
for(j=; j<=; j++)
{
if(f[j]==) tmp[j]=i-;//!!!!!
tmp[j]=tmp[j]+f[j];
}
for(j=; j<=; j++)
vis[j]=vis[j]+tmp[j];
}
}
for(j=; j<=; j++)
OR=OR*+vis[j];
printf(" %.6lf",(double)(OR*1.0/Sum)); memset(tmp,,sizeof(tmp));
memset(vis,,sizeof(vis));
for(i=; i<=n; i++)
{
make_init(a[i]);
if(i==)
{
for(j=; j<=; j++)
{
tmp[j]=f[j];
vis[j]=f[j];
}
}
else
{
for(j=; j<=; j++)
{
if(f[j]==) tmp[j]=i--tmp[j];//!!!!!
tmp[j]=tmp[j]+f[j];
}
for(j=; j<=; j++)
vis[j]=vis[j]+tmp[j];
}
}
for(j=; j<=; j++)
XOR=XOR*+vis[j];
printf(" %.6lf\n",(double)(XOR*1.0/Sum)); } int main()
{
LL T,n,i,j;
scanf("%lld",&T);
for(j=; j<=T; j++)
{
scanf("%lld",&n);
for(i=; i<=n; i++)
{
scanf("%lld",&a[i]);
}
solve(j,n);
}
return ;
}

uestc 1709 Binary Operations 位运算的灵活运用的更多相关文章

  1. [HDU] 3711 Binary Number [位运算]

    Binary Number Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tot ...

  2. Same binary weight (位运算)

    题目描述 The binary weight of a positive  integer is the number of 1's in its binary representation.for ...

  3. 位运算 UEST 84 Binary Operations

    题目传送门 题意:所有连续的子序列的三种位运算计算后的值的和的期望分别是多少 分析:因为所有连续子序列的组数有n * (n + 1) / 2种,所以要将他们分类降低复杂度,以ai为结尾的分成一组,至于 ...

  4. Codeforces 868D Huge Strings - 位运算 - 暴力

    You are given n strings s1, s2, ..., sn consisting of characters 0 and 1. m operations are performed ...

  5. PHP中的位运算与位移运算(其它语言通用)

    /* PHP中的位运算与位移运算 ======================= 二进制Binary:0,1 逢二进1,易于电子信号的传输 原码.反码.补码 二进制最高位是符号位:0为正数,1为负数( ...

  6. CodeForces 282C(位运算)

    C. XOR and OR time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  7. Codeforces 551D GukiZ and Binary Operations(矩阵快速幂)

    Problem D. GukiZ and Binary Operations Solution 一位一位考虑,就是求一个二进制序列有连续的1的种类数和没有连续的1的种类数. 没有连续的1的二进制序列的 ...

  8. NYOJ528 找球号(三)位运算

    找球号(三) 时间限制:2000 ms  |  内存限制:3000 KB 难度:2   描述 xiaod现在正在某个球场负责网球的管理工作.为了方便管理,他把每个球都编了号,且每个编号的球的总个数都是 ...

  9. A Corrupt Mayor's Performance Art(线段树区间更新+位运算,颜色段种类)

    A Corrupt Mayor's Performance Art Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 100000/100 ...

随机推荐

  1. P4145 上帝造题的七分钟2

    题目描述 "第一分钟,X说,要有数列,于是便给定了一个正整数数列. 第二分钟,L说,要能修改,于是便有了对一段数中每个数都开平方(下取整)的操作. 第三分钟,k说,要能查询,于是便有了求一段 ...

  2. Dota2App--第三天

    一.要实现的功能 1.新特性页面 1.1.是否进入新特性界面的两种情况 1)第一次安装此APP,进入新特性界面 2)不是第一次安装,但是有版本更新,进入新特性界面 1.2.具体的代码实现 //0.版本 ...

  3. notepad++常用操作梳理

    在  设置---管理快捷键   可以查询/重置快捷键.如下: 工作or学习中最长用到的操作: Ctrl+ALT-C:列编辑Ctrl+U:转换为小写Ctrl+Shift+U:转换为大写Ctrl+B:跳转 ...

  4. pytest+jenkins安装+allure导出报告

    环境安装: windows7+64位 pytest:4.0.2 allure的安装:allure的python库pytest-allure-adaptor jenkins的安装:2.138.2 JDK ...

  5. Maven自动FTP远程部署

    参照官网文档: https://maven.apache.org/plugins/maven-deploy-plugin/examples/deploy-ftp.html 1.在pom.xml中加入: ...

  6. F6&F7adjust the volume

    Windows Registry Editor Version 5.00[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Keyboard La ...

  7. rbac - 介绍

    找到了上学期末压箱底的rbac组件,这里总结下. 介绍: rbac组件:一个基于角色的权限访问控制系统的组件. 目录:

  8. JS实现值复制

    在JS中对象一般都是传地址,后续修改也会影响原始数据.例如这样. var a={ b:"b" }; var c=a; c.b="c"; console.log( ...

  9. [Xamarin] 透過 IsolatedStorageFile儲存資料(转帖)

    開發手機App通常都會遇到想要儲存資料的,舉個例來說,像是 (圖片來源:http://docs.xamarin.com/guides/android/application_fundamentals/ ...

  10. io流之转换流InputStreamReader、OutputStreamWriter

    例子程序: package io; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.File ...