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. Python FLask Web 学习笔记:jinjia2的使用方法1

    # coding:utf-8 from jinja2 import Template x = """ <p>大爷的孙子</p> <ul> ...

  2. 查看npm全局安装位置

    查看npm全局安装位置:npm config get prefix 设置位置:npm config set prefix 填写位置

  3. Linux磁盘及文件系统(二)Linux下磁盘命名和分区

    在为主机添加硬盘之前,首先需要了解Linux系统下对硬盘和分区的命令方法 一.磁盘命名 Linux下对SCSI和SATA设备是以sd命名的,第一个SCSI设备是sda,第二个是sdb....以此类推. ...

  4. 数据结构基础 ---- 数组的理解和实现(Java)

    什么是数组 数组是由类型相同的数据元素构成的有序集合,每个元素称为数组元素,每个元素受n(n>= 1)个线性关系的约束,每个元素在n个线性关系中的序号i1, i2, ....., in称该元素的 ...

  5. 题解 [ZJOI2010]数字计数

    传送门<-洛谷版 电梯<-bzoj版 这份代码是新手友好版,也算是自用版,注释自认为写的很详细. 希望对要学数位dp的人有所帮助 这份题解是记忆化搜索版的数位DP,个人还是比较建议用这种方 ...

  6. 平衡树 替罪羊树(Scapegoat Tree)

    替罪羊树(Scapegoat Tree) 入门模板题 洛谷oj P3369 题目描述 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作: 插入xx数 删除xx数(若有多个相同 ...

  7. 【性能调优】:记录一次数据库sql语句性能调优过程

    一,依旧很简单的一个接口,查询列表接口,发现10并发单交易场景下,数据库表4w铺底数据,每次查询2000条数据进行orderby显示,平均响应时间2秒以上,数据库的cpu使用率高达95%: 二,抓到这 ...

  8. Java集合类中的Iterator和ListIterator的区别

    注意:内容来自网络他人文章! 最近看到集合类,知道凡是实现了Collection接口的集合类,都有一个Iterator方法,用于返回一个实现了Iterator接口的对象,用于遍历集合:(Iterato ...

  9. Git fetch & pull 区别

    1 简单概括 2 git fetch 的用法 3 git pull的用法 文章来源:https://blog.csdn.net/qq_36113598/article/details/78906882

  10. JavaScript设计模式-22.观察者模式

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...