zoukankan      html  css  js  c++  java
  • SPOJ:OR(位运算&数学期望)

    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[33],N,a,b,c,i,j;
    int main()
    {
        int T,x; scanf("%d",&T);
        while(T--){
            scanf("%lld",&N);
            F=0; P=(N-1)*N/2;
            c=N*(N-1)/2;
            for(i=0;i<31;i++) num[i]=0;
            for(i=1;i<=N;i++){
                scanf("%d",&x);
                for(j=0;j<31;j++) if(x&(1<<j)) num[j]++;
            }
            for(i=0;i<31;i++){
                F+=(P-(N-num[i])*(N-num[i]-1)/2)*(1LL<<i);
            }
            G=__gcd(F,P);
            F/=G; P/=G;
            printf("%lld/%lld
    ",F,P);
        }
        return 0;
    }
  • 相关阅读:
    设置MySQL的字符编码
    数据库面试题
    java.lang.IncompatibleClassChangeError: Implementing class
    下载SpringJar包
    使用命令wsimport构建WebService客户端
    java读取.properties配置文件的几种方法
    ELK5.X使用X-Pack配置密码
    Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock'
    MySQL常用命令
    XPath常用定位节点元素语句总结
  • 原文地址:https://www.cnblogs.com/hua-dong/p/8948149.html
Copyright © 2011-2022 走看看