Блог пользователя garg_saurav

Автор garg_saurav, история, 4 года назад, По-английски

Hi everyone, this is a question that was asked in a contest which is over now. Here is the problem statement:

Description
You are given an array A of length N. Consider that the array is one indexed.
You need to find the sum of (A[i] & A[j] & (A[i]+A[j])) for all pairs (i,j) such that 1<=i<j<=N.

Constraints
2<=N<=2x10^5
1<=A[i]<=10^9

Output Format
Since the answer can be large, return it modulo 10^9 + 7

I know the solution in the case when there is only (A[i] & A[j]). In this case, we can count the number of set bits at i'th position (= k). Then it will contribute to kC2 pairs in summation which can directly be written as kC2 x 2^i But I can't extend the argument to the above problem. Any links/references would be highly appreciated.

  • Проголосовать: нравится
  • -3
  • Проголосовать: не нравится

»
4 года назад, # |
Rev. 3   Проголосовать: нравится +1 Проголосовать: не нравится

The idea is we will compute bit by bit contribution. For $$$i^{th}$$$ bit let $$$A=a_{i_1},a_{i_2},...,a_{i_k}$$$ be the sequence of number from the array whose $$$i^{th}$$$ bit is set. Let $$$X=2^i-1$$$. Now in this sequence for any two elements $$$(A[i]+A[j])$$$ has $$$i^{th}$$$ bit set $$$iff$$$ $$$XandA[i]+XandA[j]\,>=2^i$$$. This can be efficiently computed using two pointer method if A is sorted after anding with $$$X$$$.

My accepted Java Code
»
4 года назад, # |
  Проголосовать: нравится +13 Проголосовать: не нравится

if a contest is over why dont you give a link?