zoukankan      html  css  js  c++  java
  • [uestc][1709][Binary Operations]

    题目:http://acm.uestc.edu.cn/problem.php?pid=1709

    View Code
    #include <stdio.h>
    #include <string.h>
    #include <iostream>
    
    #define ll long long
    using namespace std;
    
    const int N = 50000+10;
    
    int M;
    ll a[N], n, dp[N], m;
    
    ll get_and(int y){
        ll ans = 0; dp[0]=0;
        for (int i=1; i<=n; i++){
            if ((a[i]&(1<<y)))dp[i]=dp[i-1]+1;
            else dp[i]=0;
            ans += dp[i];
        }
        return ans << y;
    }
    
    ll get_or(int y){
        ll ans = 0; dp[0]=0;
        for (int i=1; i<=n; i++){
            if ((a[i]&(1<<y)))dp[i]=i;
            else dp[i]=dp[i-1];
            ans += dp[i];
        }
        return ans << y;
    }
    
    ll get_xor(int y){
        ll ans=0; dp[0]=0;
        for (int i=1; i<=n; i++){
            if ((a[i]&(1<<y)))dp[i]=i-dp[i-1];
            else dp[i]=dp[i-1];
            ans += dp[i];
        }
        return ans << y;
    }
    
    void solve(int k){
        ll ans = 0;
        if (k==1){
            for (int i=0; i<=M; i++){
                ans += get_and(i);
            }
        }
        else if (k==2){
            for (int i=0; i<=M; i++){
                ans += get_or(i);
            }
        }
        else {
            for (int i=0; i<=M; i++){
                ans += get_xor(i);
            }
        }
        double t = ans*2.0 / (n*(n+1));
        printf(" %.6f", t);
    }
    
    int main()
    {
        //freopen("D:/a.txt", "r", stdin);
        int T, cas=1;
        scanf("%lld", &T);
        while (T--){
            m = -1;
            scanf("%lld", &n);
            for (int i=1; i<=n; i++)
                scanf("%lld", &a[i]), m=max(m, a[i]);
            for (int i=0; i<31; i++)
                if(m&(1<<i)) M=i;
            printf("Case #%d:", cas++);
            for (int i=1; i<=3; i++) solve(i);
            puts("");
        }
        return 0;
    }
  • 相关阅读:
    002: Opencv 4.0代码执行
    Opencv4.0+cmake3.13.3+vs2017源码编译
    ubuntu 18.0Lts +YouCompleteMe插件安装
    pip 安装keras
    pip 安装paddle
    pip 安装 tensorboardX
    pip 安装pytorch 命令
    TT信息-4-Spring Boot/Spring Cloud
    TT信息-3-Spring/Spring MVC
    TT信息-2设计模式
  • 原文地址:https://www.cnblogs.com/nigel0913/p/2586797.html
Copyright © 2011-2022 走看看