Given an array of N integers A1, A2, A3…AN. If you randomly choose two indexes i ,j such that 1 ≤ i < j ≤ N, what is the expected value of Ai | Aj?

Input

First line contains an integer T, the number of test cases. Each test case consists of two lines. First line denotes the size of array, N and second line contains N integers forming the array.
1 ≤ T ≤ 10 
2 ≤ N ≤ 100,000 
0 ≤ Ai < 231

Output

For each test case, print the answer as an irreducible fraction. Follow the format of the sample output. 
The fraction p/q (p and q are integers, and both p ≥ 0 and q > 0 holds) is called irreducible, if there is no such integer d > 1 that divides both p and q separately.

Example

Input:
2
2
0 0
3
1 2 3 Output:
0/1
3/1

题意:给定一个数列a[],求任意两个数a[i]和a[j]的或运算的期望(i!=j)。

思路:此类题已经是套路了,就是每一位分别看,统计为一位为1和为0的个数。然后根据XOR,或者OR的性质采取相应的措施。

OR的话,就是第i位的贡献是:(C(n,2)-C(num[i],2))/C(n,2)*(1<<i) 。num[i]是第i位为0的个数。

(注意,用unsigned long long)

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define ll unsigned long long
using namespace std;
ll F,P,G,num[],N,a,b,c,i,j;
int main()
{
int T,x; scanf("%d",&T);
while(T--){
scanf("%lld",&N);
F=; P=(N-)*N/;
c=N*(N-)/;
for(i=;i<;i++) num[i]=;
for(i=;i<=N;i++){
scanf("%d",&x);
for(j=;j<;j++) if(x&(<<j)) num[j]++;
}
for(i=;i<;i++){
F+=(P-(N-num[i])*(N-num[i]-)/)*(1LL<<i);
}
G=__gcd(F,P);
F/=G; P/=G;
printf("%lld/%lld\n",F,P);
}
return ;
}

SPOJ:OR(位运算&数学期望)的更多相关文章

  1. hdu 4336 Card Collector (概率dp+位运算 求期望)

    题目链接 Card Collector Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  2. HDU - 4810 - Wall Painting (位运算 + 数学)

    题意: 从给出的颜料中选出天数个,第一天选一个,第二天选二个... 例如:第二天从4个中选出两个,把这两个进行异或运算(xor)计入结果 对于每一天输出所有异或的和 $\sum_{i=1}^nC_{n ...

  3. SPOJ Favorite Dice(数学期望)

    BuggyD loves to carry his favorite die around. Perhaps you wonder why it's his favorite? Well, his d ...

  4. SPOJ:Robot(数学期望)

    There is a robot on the 2D plane. Robot initially standing on the position (0, 0). Robot can make a ...

  5. 【整理】简单的数学期望和概率DP

    数学期望 P=Σ每一种状态*对应的概率. 因为不可能枚举完所有的状态,有时也不可能枚举完,比如抛硬币,有可能一直是正面,etc.在没有接触数学期望时看到数学期望的题可能会觉得很阔怕(因为我高中就是这么 ...

  6. Rightmost Digit(快速幂+数学知识OR位运算) 分类: 数学 2015-07-03 14:56 4人阅读 评论(0) 收藏

    C - Rightmost Digit Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit ...

  7. [CSP-S模拟测试]:位运算(数学)

    题目传送门(内部题72) 输入格式 输入文件$bit.in$ 每个输入文件包含$T$组测试数据.输入文件的第一行为一个整数$T$,表示数据组数.接下来$T$行,每行表示一组测试数据每组测试数据包括三个 ...

  8. $CH$3801 $Rainbow$的信号 期望+位运算

    正解:位运算 解题报告: 传送门! 其实就是个位运算,,,只是顺便加了个期望的知识点$so$期望的帕并不难来着$QwQ$ 先把期望的皮扒了,就直接分类讨论下,不难发现,答案分为两个部分 $\left\ ...

  9. 我眼中的 Nginx(一):Nginx 和位运算

    作者张超:又拍云系统开发高级工程师,负责又拍云 CDN 平台相关组件的更新及维护.Github ID: tokers,活跃于 OpenResty 社区和 Nginx 邮件列表等开源社区,专注于服务端技 ...

随机推荐

  1. pandaboard安装ubuntu14.04系统遇到的问题

    按照该网址步骤安装https://www.eewiki.net/display/linuxonarm/PandaBoard 在linux kernel的./build_kernel.sh时,自动安装交 ...

  2. ganglia-monitoring-centos-linux

    http://www.tecmint.com/install-configure-ganglia-monitoring-centos-linux/ http://monitor.gitlab.net/ ...

  3. 深入GCD(四):使用串行队列实现简单的预加载

    其主要思路是使用gcd创建串行队列,然后在此队列中先后执行两个任务:1.预加载一个viewController 2.将这个viewController推入代码如下:@implementation DW ...

  4. send to instance already dealloc nil error

    这个是因为发送消息的对象已经被dealloc了,然后再次发送[release]请求就不行了.所以可以retain或者alloc对象 if (self.buttonsList) {            ...

  5. fedora关闭防火墙

    sudo systemctl stop iptables sudo sytemctl stop firewalld

  6. ubutu强制结束进程 kill -9 ProcessID

    强制终止进程 kill -9 2128 表示强制结束进程号 2128 对应的进程.

  7. windows10 開機失敗,且按F8無法進入安全模式

    windows10 開機失敗,且按F8無法進入安全模式: 在cmd視窗下: bcdedit set {default} bootmenupolicy legacy 重啟,再按F8試一試吧! To En ...

  8. PS 如何使用液化工具给人物减肥

    进入"液化", 有个收缩按钮, 可以选择范围大小, 想瘦哪里, 瘦多少都OK   最终效果图     1.打开原图,进入通道面板,选择菜单图像计算,计算红色通道,保留人物见图.   ...

  9. css 滤镜之AlphaImageLoader

      CreateTime--2017年12月25日17:05:37 Author:Marydon ie滤镜特效之AlphaImageLoader 作用: 用于设置背景图片特效样式 使用条件: IE8及 ...

  10. 用JAVA编写浏览器内核之实现javascript的document对象与内置方法

    原创文章.转载请注明. 阅读本文之前,您须要对浏览器怎样载入javascript有一定了解. 当然,对java与javascript本身也须要了解. 本文首先介绍浏览器载入并执行javascript的 ...