zoukankan      html  css  js  c++  java
  • HDUBinary Operation

    最后才明白题目的意思可以直接抽象为对于每一位,所有数字的0个数乘以1的个数,再将每一位的数恢复过来。

    自己的做法也是枚举每一个二进制位,对于已经求了 ai,ai+1...aN-1,aN个数的二进制位之后再用ai-1的这一位的情况来判定这一位会增加多少个1。

    代码如下:

    #include <cstring>
    #include <cstdio>
    #include <cstdlib>
    #include <algorithm>
    using namespace std;
    typedef long long int Int64;
    // 1000000 这个数字只有最多19位
    
    int num[1000005], N;
    
    Int64 rec[25];
    
    int main()
    {
        int T;
        Int64 ans, base, c;
        scanf("%d", &T);
        while (T--) {
            ans = 0;
            base = 1;
            memset(rec, 0, sizeof (rec));
            scanf("%d", &N);
            for (int i = 1; i <= N; ++i) {
                scanf("%d", &num[i]);
            }
            if (N == 1) {
                printf("%d\n", num[1]);
                continue;
            }
            for (int i = 19; i >= 0; --i) {  // 枚举每一位
                c = 0;
                for (int j = N; j >= 2; --j) {  // 枚举每一个数
                    if ((1 << i) & num[j]) {
                        ++c;
                    }
                    rec[i] += ((1 << i) & num[j-1]) ? (N-j+1)-c : c;
                }
                ans += rec[i] * (base << i);
            }
            printf("%I64d\n", ans);
        }
        return 0;
    }
  • 相关阅读:
    dos命令连接mysql并且查看编码方式
    node.js 好博客
    node.js 连接数据库
    express的基本配置项
    express 手册
    VPS巴士
    nodejs书籍
    nodejs connect模块
    网站工具,如天气,手机归属地
    NodeJS的异步编程风格
  • 原文地址:https://www.cnblogs.com/Lyush/p/2617728.html
Copyright © 2011-2022 走看看