We can solve the problem by breaking the numbers (x, y, z) into bits. Like x, y, z <= 10 18, so the number is less than 50 bits .
So, from the first bit to the 50th bit, we need to know four things
- If
xless than A y , Bz C(x ^ y) | z D
, :
long totalNumber(int bit, boolean lessThanA, boolean lessThanB, boolean lessThanC, boolean lessThanD)
0 1 x, y, z
long result = 0;
for(int x = 0; x < 2; x++){
for(int y = 0; y < 2; y++){
for(int z = 0; z < 2; z++){
result += totalNumber(bit + 1, lessThanA, lessThanB, lessThanC, lessThanD);
result %= MOD;
}
}
}
1, 50th
if(bit == 50)
return 1;
, ,
dp[bit][lessThanA][lessThanB][lessThanC][lessThanD]
O(bit * lessThanA * lessThanB *lessThanC * lessThanD), ~ O(50*2^4) = O(800)
: . B round 1B Google Code Jam 2014 .