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. docker镜像mac下保存路径

    mac下docker的镜像保存位置: /Users/{YourUserName}/Library/Containers/com.docker.docker/Data/com.docker.driver ...

  2. 487. Max Consecutive Ones II

    Given a binary array, find the maximum number of consecutive 1s in this array if you can flip at mos ...

  3. Codeforces 713C Sonya and Problem Wihtout a Legend(DP)

    题目链接   Sonya and Problem Wihtout a Legend 题意  给定一个长度为n的序列,你可以对每个元素进行$+1$或$-1$的操作,每次操作代价为$1$. 求把原序列变成 ...

  4. java队列--queue详细分析

    ---恢复内容开始--- Queue:基本上一个队列就是一个先入先出(FIFO)的数据结构 Queue接口与List.Set同一级别,都是继承了Collection接口,LinkedList实现了Li ...

  5. spring工具类获取bean

    import org.springframework.web.context.ContextLoader; import org.springframework.web.context.WebAppl ...

  6. 解决asp.net core 日期格式 datetime Json返回 带T的问题

    原文:解决asp.net core 日期格式 datetime Json返回 带T的问题 记录一下: Startup中,将 services.AddMvc(); 改为: services.AddMvc ...

  7. OD调试器调试Delphi程序按钮事件断点方法

    这几天我调试一个Delphi程序,总是无法断点按钮事件,像我这样的菜鸟断点按钮事件真心累啊.所以我分享一下我下断点的经验!希望新手们少走弯路!!   工具/原料   OD调试器 Delphi程序 方法 ...

  8. Android 平板中 自己定义键盘(popuwindow) 居于屏幕左下方 仿微信的password输入界面

          之前博客中,介绍过使用谷歌提供的键盘的一些api,能够非常好地自己定义键盘,參考我之前的博客链接:android 自己定义键盘 ,这个有一个局限性,仅仅能占满屏幕,无法做到仅仅能占一部分的 ...

  9. saltstack安装配置(syndic)

    syndic是saltstack用来做集群部署的,一般结构如图: syndic是一个特殊的minion,syndic类继承于minion类,syndic可以看作一个代理,只做数据传递. CentOS上 ...

  10. [LeetCode] 038. Count and Say (Easy) (C++/Python)

    索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 038. Cou ...