ZOJ 3870 Team Formation(异或运算)

来源:互联网 发布:宽带网络那家好 编辑:程序博客网 时间:2024/09/21 08:51
题意:给出n个数,其中两个数的异或能大于他们本身。。
好气啊aaa,思路都对了,可惜没弄出来,,不难发现换成二进制后,某个数的最高位在其他数对应的数位为0的话,肯定比这两个数要大。。只要先储存下所有数的位数QAQ~~,然后再循环判断后面位数为0的情况。。
#include "string"#include "iostream"#include "cstdio"#include "cmath"#include "set"#include "queue"#include "vector"#include "cctype"#include "sstream"#include "cstdlib"#include "cstring"#include "stack"#include "ctime"#include "algorithm"#define pa pair<int,int>#define Pi M_PI#define INF 0x3f3f3f3f#define INFL 0x3f3f3f3f3f3f3f3fLLusing namespace std;typedef long long LL;const int M=30005;int a[M], bit[50]; // bit[i]表示有多少个数的最高位的1在第i位上void solve(int x)  //哭晕  存储最高位{    int l = 31;    while(l >= 0)    {        if(x & (1<<l))        {            bit[l]++;            return ;        }        l--;    }    return ;}int main(){    int T, n;    scanf("%d", &T);    while(T--)    {        scanf("%d", &n);        memset(bit, 0, sizeof(bit));        for(int i = 0; i < n; i++)        {            scanf("%d", &a[i]);            solve(a[i]);        }        int ans = 0;        for(int i = 0; i < n; i++)        {            int l = 31;            while(l >= 0)            {                if(a[i] & (1<<l))                          break;                l--;            }            while(l >= 0)            {                if(!(a[i] & (1<<l)))                       ans += bit[l];                l--;            }        }        printf("%d\n", ans);    }        return 0;    }

0 0
原创粉丝点击