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. HTML Strip Char Filter

    The html_strip character filter strips HTML elements from the text and replaces HTML entities with t ...

  2. Java中关键字final用法

    ///首先声明,本文转载自博客园的海子,链接:http://www.cnblogs.com/dolphin0520/p/3736238.html 浅析Java中的final关键字 谈到final关键字 ...

  3. 2019年北航OO第二次博客总结

    一.多线程电梯系列作业设计策略 1. 第一次作业——"FAFS傻瓜电梯" 第一次作业是先来先服务的"傻瓜电梯",我当时觉得这个设计未免太简单了,于是就在傻瓜电梯 ...

  4. 图的最短路径---弗洛伊德(Floyd)算法浅析

    算法介绍 和Dijkstra算法一样,Floyd算法也是为了解决寻找给定的加权图中顶点间最短路径的算法.不同的是,Floyd可以用来解决"多源最短路径"的问题. 算法思路 算法需要 ...

  5. Apache Maven的入门使用之项目的基本构建(1)

    前言 最近在研究java框架struts2的相关漏洞,然后就去看了官方给出的文档.在看文档的过程中发现使用到了Apache Maven这个项目管理工具,我在网上搜索了一下,大多数文章都写得不是很系统, ...

  6. Mac下安装Fiddler抓包工具(别试了,会报错,没办法使用)

    下载: https://www.telerik.com/download/fiddler 离线版本:(链接: https://pan.baidu.com/s/1hr7f8QK 密码: ukg2) 安装 ...

  7. 开发者必备的12个JavaScript库

    现在 web 设计是最有趣的了,做好 web 设计不仅要熟练使用 Javascript,css 和 html 等,还要有自己的创意设计.为了方便大家发挥自己的创意,就产生了很多 JS 框架,Node. ...

  8. Cloudera Manager安装之利用parcels方式(在线或离线)安装3或4节点集群(包含最新稳定版本或指定版本的安装)(添加服务)(Ubuntu14.04)(五)

    前期博客 Cloudera Manager安装之Cloudera Manager 5.6.X安装(tar方式.rpm方式和yum方式) (Ubuntu14.04) (三) 如果大家,在启动的时候,比如 ...

  9. 再谈Hive元数据如hive_metadata与Linux里MySQL的深入区别(图文详解)

    不多说,直接上干货! [bigdata@s201 conf]$ vim hive-site.xml [bigdata@s201 conf]$ pwd /soft/hive/conf [bigdata@ ...

  10. AMQP技术术语

    什么是AMQP AMQP,即Advanced Message Queuing Protocol,一个提供统一消息服务的应用层标准高级消息队列协议,是应用层协议的一个开放标准,为面向消息的中间件设计.基 ...