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. NEST - How can i do multiple nested aggregation?

    question: How can I do multiple nested aggregation? I have tried something like this: Aggregations(x ...

  2. [ActionScript 3.0] 通过as3操作web内容

    package com.controls.utils { import flash.net.*; import flash.utils.ByteArray; import flash.external ...

  3. AngularJS入门讲解4:多视图,事件绑定,$resource服务讲解

    上一课,大家知道,手机详细模板我们没有写出来,使用的是一个占位模板. 这一课,我们先实现手机详细信息视图,这个视图会在用户点击手机列表中的一部手机时被显示出来. 为了实现手机详细信息视图,我们将会使用 ...

  4. [CISCO] Telete/SSH 之 Port 绑定/端口安全

    [网络] Telete/SSH 之 Port 绑定/端口安全 一.前言 之前写完了网络] DHCP 之 Mac 绑定,CiSCO 交换机配置 SSH 登陆.这次我们再试试能不能挖的在深入些. (1) ...

  5. XMPPManager 解析

    一.用户登录流程 用户登录流程.png 注意:XMPP核心文件,基于TCP的XML流的传输,XMPPFrame框架是通过代理的方式实现消息传递的 实现用户登录的步骤如下: 1.实例化XMPPStrea ...

  6. QuantLib 金融计算——基本组件之 Schedule 类

    目录 QuantLib 金融计算--基本组件之 Schedule 类 Schedule 对象的构造 作为"容器"的 Schedule 对象 一些常用的成员函数 如果未做特别说明,文 ...

  7. HLS:OpenCV和RTL代码转换关系

    OpenCV 图像处理是基于存储器帧缓存而构建的, 它总是假设视频帧数据存放在外部 DDR 存储器中. 由于处理器的小容量高速缓存性能的限制, 因此, OpenCV 访问局部图像性能较差. 并且, 从 ...

  8. 搭建django环境

    一.安装django(两种方式) 1.pip install django 2.python setup.py install(下载gjango包:https://www.djangoproject. ...

  9. 使用NHibernate(5)-- Linq To NHibernate

    Linq是NHibernate所支持的查询语言之一,对于Linq的实现在源码的src/Linq目录下.以下是一个使用Linq进行查询数据的示例: var users = session.Query&l ...

  10. GANS--理解

    GAN所建立的一个学习框架,实际上就是生成模型和判别模型之间的一个模仿游戏.生成模型的目的,就是要尽量去模仿.建模和学习真实数据的分布规律:而判别模型则是要判别自己所得到的一个输入数据,究竟是来自于真 ...