You are given an integer N You need to calculate the Bitwise-OR sum of all ordered pairs (i,j):(1≤i,j≤N) . Print the value of sum modulo 10^9+7 (the remainder when sum is divided by 10^9+7
Example Case:
Input: 2
Output :(1|1 + 1|2 + 2|1 + 2|2 ) modulo (10^9+7) = 9.
Could Someone Suggest an efficient approach
Please Find Below My code.
using namespace std;
const unsigned long long int MOD = 1000000007;
unsigned long long int pairAndSum(vector &vect, unsigned long long int n)
{
unsigned long long int ans = 0;
for (unsigned long long int i = 0; i < 32; i++)
{
unsigned long long int k = 0;
for (unsigned long long int j = 0; j < n; j++)
if ( !(vect[j] & (1 << i)) )
k++;
ans +=(((1<<i)%MOD) ((n(n-1)/2)%MOD - (k*(k-1)/2)%MOD))%MOD;
}
return ans;
}
int main()
{
unsigned long long int n,i; cin>>n;
unsigned long long int arr[n];
vector vec(n);
for(i=0;i<n;i++)
vec[i]=i+1;
cout << (((2%MOD)*(pairAndSum(vec, n)%MOD))+(n*(n+1)/2)%MOD)%MOD << endl;
return 0;
}
This could fetch a partial of 60/100 points , Other test cases gave WA
The Challenge is now over
Thanks
hope if you can simplify that too 