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. 使用X-Pack插件来进行权限控制

    1.为elasticsearch安装X-Pack插件.进入 elasticsearch根目录,执行: bin/elasticsearch-plugin install x-pack杀掉进程,重启es, ...

  2. 无比迅速敏捷地开发iOS超精美控件

    目录 前言 设计 编码 PaintCode 前言 自从人生第一篇博客<iOS中的预编译指令的初步探究>问世以来 浏览量竟然达到了360多,(路过的大神勿笑!)这些浏览量使我兴奋异常但又令我 ...

  3. opencv 将视频分解成图片和使用本地图片合成视频

    代码如下: // cvTest.cpp : Defines the entry point for the console application. #include "stdafx.h&q ...

  4. Java NIO学习与记录(七): Reactor单线程模型的实现

    Reactor单线程模型的实现 一.Selector&Channel 写这个模型需要提前了解Selector以及Channel,之前记录过FileChannel,除此之外还有以下几种Chann ...

  5. 【算法笔记】B1053 住房空置率

    看了半天发现是题目理解错了,可能空置的里面观察期超过D则判定空置,而不是用电量低于e的天数超过D. code #include <bits/stdc++.h> using namespac ...

  6. np.array()和np.mat()区别

    1. 生成数组所需格式不同 mat可以从字符串或列表中生成:array只能从列表中生成 2. 生成的数组计算方式不同 array生成数组,用np.dot()表示矩阵乘积,(*)号或np.multipl ...

  7. 删除数据库的数据后让id从1开始算

    delete from t_AttendanceRecorddbcc checkident('t_AttendanceRecord',reseed,0) truncate table  表名称

  8. winform FormBordStyle=none 及 wpf FormBordStyle=none 的鼠标点击移动问题

    winform: //bool formMove = false;//窗体是否移动 //Point formPoint;//记录窗体的位置 private void Login_MouseDown(o ...

  9. ADO.NET中DbConnection.GetSchema方法的使用总结

    此方法获取数据库的结构,所以可以用它获取数据库中所有的表 先上代码 string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;U ...

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

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