cdoj第13th校赛初赛H - Hug the princess

时间:2023-03-09 03:28:56
cdoj第13th校赛初赛H - Hug the princess

http://acm.uestc.edu.cn/#/contest/show/54

H - Hug the princess

Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others)
Submit Status

There is a sequence with n elements. Assuming they are a1,a2,⋯,an.
Please calculate the following expession.
cdoj第13th校赛初赛H - Hug the princess
In the expression above, ^ | & is bit operation.

Input

The first line contains a single integer n, which is the size of the sequence.

The second line contains n integers, the ith integer ai is the ith element of the sequence.

1≤n≤100000,0≤ai≤100000000

Output

Print the answer in one line.

Sample input and output

Sample Input Sample Output
2
1 2
6

思路:首先,自己可以通过举几个例子来验证,异或运算与与运算之和刚好等价于或运算,或者可以这样想,异或是(1,0)、(0,1),与是(1,1),合起来刚好是或。然后题目就是求两倍的或运算了。然后,每一个ai都与aj或运算(i<j),每次ai与aj或的时候,aj二进制位上是1的数位在或运算后总还是1,所以前面有多个ai与aj或,最后结果里就有多少个aj的和;然后考虑aj上是0的数位(比如第4位),记录前面所有的ai中哪些数在这个第4位上是1,记为sum[4],最后的结果里就有sum[4]*(1<<4),因为有权值的嘛。这样O(n)地扫一遍,就行了。如果就像我第一次那样直接O(n*n)地把所有数进行或运算,呵呵,tle了。

这是官方题解:

cdoj第13th校赛初赛H - Hug the princess

代码:

 #include <fstream>
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <vector> using namespace std; #define PI acos(-1.0)
#define EPS 1e-10
#define lll __int64
#define ll long long
#define INF 0x7fffffff ll a[];
vector<ll> cnt; int main(){
//freopen("D:\\input.in","r",stdin);
//freopen("D:\\output.out","w",stdout);
int n;
ll tn=;
scanf("%d",&n);
for(int i=;i<n;i++){
scanf("%lld",&a[i]);
tn=max(tn,a[i]);
}
ll ans=,t=;
while(tn){
t++;
tn>>=;
cnt.push_back();
}
for(int i=;i<n;i++){
ans+=a[i]*i;
for(int j=;j<t;j++){
if(!((a[i]>>j)&)){
ans+=(<<j)*cnt[j];
}else{
cnt[j]++;
}
}
}
printf("%lld\n",ans*);
return ;
}