• ZOJ3870 Team Formation【位运算+数学】



    Team Formation

    Time Limit: 2 Seconds      Memory Limit: 131072 KB

    For an upcoming programming contest, Edward, the headmaster of Marjar University, is forming a two-man team from N students of his university.

    Edward knows the skill level of each student. He has found that if two students with skill level A and B form a team, the skill level of the team will be A  B, where ⊕ means bitwise exclusive or. A team will play well if and only if the skill level of the team is greater than the skill level of each team member (i.e. A  B > max{A, B}).

    Edward wants to form a team that will play well in the contest. Please tell him the possible number of such teams. Two teams are considered different if there is at least one different team member.

    Input

    There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:

    The first line contains an integer N (2 <= N <= 100000), which indicates the number of student. The next line contains N positive integers separated by spaces. Theith integer denotes the skill level of ith student. Every integer will not exceed 109.

    Output

    For each case, print the answer in one line.

    Sample Input

    2
    3
    1 2 3
    5
    1 2 3 4 5
    

    Sample Output

    1
    6
    

    Author: LIN, Xi
    Source: The 12th Zhejiang Provincial Collegiate Programming Contest


    问题链接ZOJ3870 Team Formation

    问题简述输入n个正整数,求任意两个数异或运算结果大于两个数中的最大值的组数。

    问题分析

    这里给出异或运算的规则,1^1=0,1^0=1,0^1=1,0^0=0。

    对于y而言,若y的第i位为1(从右到左分别为0-31位),y的第j位为0(j<i),

    对于第j位为1并且是最高位的1的x,

    那么y^x>y(按照假定满足x<y)。

    程序说明

    数组bit[],bit[i]表示各个数(所有的a)中第i位是最高位1的数的个数。

    数组left[],left[i]保存a[i]的为1的最高位。

    题记:(略)

    AC的C++语言程序如下:

    /* ZOJ3870 Team Formation */
    
    #include <iostream>
    #include <stdio.h>
    #include <string.h>
    
    const int N = 100000;
    const int N2 = 32;
    
    int a[N], left[N], bit[N2];
    
    void left1count(int x, int i) {
        int l = N2 - 1;
        left[i] = -1;
        while(l >= 0) {
            if(x & (1 << l)) {
                bit[l]++;
                left[i] = l;
                break;
            }
            l--;
        }
        return;
    }
    
    int main()
    {
        int t, n;
        scanf("%d", &t);
        while(t--) {
            memset(bit, 0, sizeof(bit));
            scanf("%d",&n);
            for(int i=0; i<n; i++) {
                scanf("%d", &a[i]);
                left1count(a[i], i);
            }
    
            long long ans = 0;
            for(int i=0; i<n; i++) {
                if(a[i]) {
                    while(left[i] >= 0) {
                        if(!(a[i] & (1 << left[i])))
                            ans += bit[left[i]];
                        left[i]--;
                    }
                }
            }
            printf("%lld
    ", ans);
        }
    
        return 0;
    }


  • 相关阅读:
    Asp.NET Core+ABP框架+IdentityServer4+MySQL+Ext JS之验证码
    ABP前端使用阿里云angular2 UI框架NG-ZORRO分享
    ASP.NET Core之跨平台的实时性能监控(2.健康检查)
    应用程序的8个关键性能指标以及测量方法
    ASP.NET Core之跨平台的实时性能监控
    浅析Entity Framework Core2.0的日志记录与动态查询条件
    开发短网址平台的思路
    Nginx解决错误413 Request Entity Too Large
    配置Nginx实现负载均衡
    Windows下Nginx的安装及使用方法入门
  • 原文地址:https://www.cnblogs.com/tigerisland/p/7563616.html
Copyright © 2020-2023  润新知