Note that there are some explanatory texts on larger screens.

plurals
  1. POAlgorithm to add sum of every possible xor-sum sub-array
    primarykey
    data
    text
    <p>I participated in one algorithmic competition. I got stuck in one problem, I am asking the same here.</p> <p><strong>Problem Statement</strong></p> <p>XOR-sum array is to XOR all the numbers of that sub-array. An array is given to you, you have to add all possible such XOR-sub-array.</p> <p>For better understanding, question statement is <a href="http://www.codechef.com/COOK39/problems/PPXOR" rel="nofollow">here</a> also.</p> <p><strong>Example</strong></p> <p><strong>Input</strong></p> <p>Array :- 1 2</p> <p><strong>Output</strong> :- 6</p> <p><strong>Explanation</strong></p> <p><code>F(1, 1) = A[1] = 1</code>, <code>F(2, 2) = A[2] = 2</code> and <code>F(1, 2) = A[1] XOR A[2] = 1 XOR 2 = 3.</code></p> <p>Hence the answer is 1 + 2 + 3 = 6.</p> <p><strong>My code</strong></p> <p>Time Complexity :- O(N^2), (Inefficient one, didn't accpted in the competition)</p> <pre><code>#include&lt;iostream&gt; using namespace std; long long int input[100001]; main() { int T; int N; long long int val; long long int temp = 0; long long int answer = 0; cin &gt;&gt; T; while(T--) { cin &gt;&gt; N; for(int i = 0; i &lt; N; i++) { cin &gt;&gt; val; temp = temp^val; answer += temp; input[i] = temp; } for( int i = 0; i &lt; N; i++ ) { for( int j = i+1; j &lt; N; j++ ) { answer += input[i]^input[j]; } } cout &lt;&lt; answer &lt;&lt; endl; answer = 0; temp = 0; } return 0; } </code></pre> <p><strong>Question:-</strong></p> <p>I saw the best solution of this problem on <a href="http://www.codechef.com/viewplaintext/2928504" rel="nofollow">this link</a></p> <p>But in this code, I didn't understand below module, please help me to understand that.</p> <pre><code>for (int i=0, p=1; i&lt;30; i++, p&lt;&lt;=1) { int c=0; for (int j=0; j&lt;=N; j++) { if (A[j]&amp;p) c++; } ret+=(long long)c*(N-c+1)*p; } </code></pre> <p>Thanks in advance. Looking for your kind reply.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload