x3

【问题描述】
X3 星球的外星人名字都是一个正整数,而且他们都互相认识。任意两个外星人的友谊度
都是他们名字的异或值。整个星球的友好值等于所有友谊度之和。这个星球的友好值是多少。
【输入格式】
第一行输入两个整数 n 表示 x3 星球有 n 个外星人(1<=n<=1000000)

接下来 n 行,第 i 行为外星人的名字 a[i],a[i]<=1000000。
【输出格式】
一个整数代表 x3 星球的友好值。

input
2
19
10
output
25

input
3
7
3
5
output
12

input
5
9
13
1
9
6
output
84

#include <cstdio>
#include <iostream>
using namespace std;
#define GC getchar()
#define R register
template <typename T>
inline void read(T &x)
{
    x=0;char a=GC;int f=1;
    for(;a>'9'||a<'0';a=GC) if(a=='-') f=-1;
    for(;a>='0'&&a<='9';a=GC) x=(x<<1)+(x<<3)+(a^48);
    x*=f;
}
int a;
int c[30];
int main()
{
    freopen("x3.in","r",stdin);
    freopen("x3.out","w",stdout);
    int n;read(n);long long sum=0;
    for(R int i=1;i<=n;i++) 
    {
        read(a);
        R int num=0;
        while(a)
        {
            if(a&1) c[num]++;
            a/=2;
            num++;
        }
    }
    for(R int i=0;i<30;i++)
    sum+=(long long)c[i]*(n-c[i])*(1<<i);
    printf("%lld\n",sum);
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/000226wrp/p/11352809.html
x3