In the first example 1 ,3 ,6 ,2
1 | 3 = 3
1 | 6 = 7
1 | 2 = 3
3 | 6 = 7
3 | 2 = 3
6 | 2 = 6
ans = 3 + 7 + 3 + 7 + 3 + 6
ans = 29
input :24
1 3 6 2
2
3 7
output:Case 1: 29
Case 2: 7
#include <bits/stdc++.h>using namespace std;
long long pairAndSum(long long arr[],long long n)
{
long long ans = 0;
for ( long long i = 0; i < 32; i++)
{
long long k = 0;
for (long long j = 0; j < n; j++)
if ( !(arr[j] & (1 << i)) )
k++;
ans += (1<<i) * (n*(n-1)/2 - k*(k-1)/2);
}
return ans;
}
int main()
{
long long test,ii;
cin>>test;
for(ii=0; ii<test; ii++)
{
long long t,i,n,arr[210000];
cin>>n;
for(i=0; i<n; i++)
cin>>arr[i];
printf("Case %lld: ",ii+1);
cout << pairAndSum(arr, n) << endl;
}
return 0;
}
কোন মন্তব্য নেই:
একটি মন্তব্য পোস্ট করুন