zoukankan      html  css  js  c++  java
  • TZOJ--4986: Team Formation(二进制枚举)

    4986: Team Formation 

    时间限制(普通/Java):1000MS/3000MS     内存限制:65536KByte

    描述

     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.

    输入

     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. The ithinteger denotes the skill level of ith student. Every integer will not exceed 109.

    输出

    For each case, print the answer in one line.

    样例输入

    2
    3
    1 2 3
    5
    1 2 3 4 5

    样例输出

    1

    6

    题目来源

    ZJCPC 2015

     

    题目链接:tzcoder.cn/acmhome/problemdetail.do?&method=showdetail&id=4986

     

    题目大意:给你n个数字,问 满足(a^b)>max(a,b)的有多少对?

    异或:相同为0不同为1,所以只需要枚举二进制中最高位的1所在位置,然后枚举这个数二进制中0所在的位置,计算出这个位置其他数字中是最高位的数字有几个即可。

     

    #include<stdio.h>
    #include<string.h>
    int main()
    {
    	int t,n,a[100010],b[33];
    	scanf("%d",&t);
    	while(t--){
    		memset(a,0,sizeof(a));
    		memset(b,0,sizeof(b));
    		int x,i,j;
    		scanf("%d",&n);
    		for(i=0;i<n;i++){
    			scanf("%d",&a[i]);
    			for(j=31;j>=0;j--){
    				if(a[i]&(1<<j)){
    					b[j]++;
    					break;
    				}
    			}
    		}
    		int s=0;
    		for(i=0;i<n;i++){
    			for(j=31;j>=0;j--){
    				if(a[i]&(1<<j))break;
    			}
    			for(;j>=0;j--){
    				if(!(a[i]&(1<<j))){
    					s+=b[j];
    				}
    			}
    		}
    		printf("%d
    ",s);
    	}
    	return 0;
    }
    

      

  • 相关阅读:
    linux 相关( 随时更新)
    django admin 修改批量操作内容
    区块链
    关于读取excel 和 写excel
    git基本用法
    服务器与本地的控制工具unison
    爬虫框架 Scrapy
    爬虫高性能 asyncio库 twisted库 tornado库
    爬虫请求库之selenium模块
    爬虫请求库
  • 原文地址:https://www.cnblogs.com/Anidlebrain/p/10029071.html
Copyright © 2011-2022 走看看