Count all pairs of x,y such as x + y = c && x XOR y = d Given two decimals $c$ and $d$;  $1 \le c,d \le 10^{12}$. Count all pairs of $x$ and $y$ that satisfy the following statements:
  $$
x+y = c\\
x \text{ XOR } y = d
$$


Killed already a day still can't solve that task in 2s. Even with optimizations brute-force algorithm doesn't terminate when $c$ and $d$ closer to upper bound. 

UPDATE
Took a while to figure, here is solution:

void solve( Scanner in, PrintWriter out ) {
    long sum = in.nextLong();
    long xor = in.nextLong();

    long andMask = andMask( sum, xor );
    if( andMask == -1 ) {
        out.print( 0 );
        return;
    }

    long sample = sample( sum, andMask );
    if( sample == -1 ) {
        out.print( 0 );
        return;
    }

    out.print( combinations( sample, andMask ) );

}

long andMask( long sum, long xor ) {
    if( ( sum - xor ) % 2 != 0 ) return -1;
    return ( sum - xor ) / 2;
}

long sample( long sum, long andMask ) {
    if( ( ( sum - andMask ) &amp; andMask ) != andMask ) return -1;
    return sum - andMask;
}

long combinations( long sample, long andMask ) {
    long combinations = 1;
    int major = major( sample );
    for( int i = 0; i &lt; major; i++ ) {
        if( bitValue( sample, i ) == 1 &amp;&amp; bitValue( andMask, i ) != 1 ) {
            combinations *= 2;
        }
    }

    if( andMask == 0 ) {
        combinations -= 2;
    }
    return combinations;
}

int major( long sum ) {
    int major = 0;
    while( sum &gt; 0 ) {
        major++;
        sum = sum &gt;&gt; 1;
    }
    return major;
}

int bitValue( long value, int bit ) {
    return ( int ) ( value &gt;&gt; bit &amp; 1 );
}
Hint: $x+y=(x \text{ XOR } y)+2(x \text{ AND } y)$.