UVALive-6657-GCD XOR, uvalive-6657-gcd
Given an integer N, nd how many pairs (A; B) are there such that: gcd (A, B) = A xor B where
1 <= B <= A <= N.
Here gcd (A, B) means the greatest common divisor of the numbers A and B. And A xor B is
Value of the bitwise xor operation on the binary representation of A and B.
Input
The rst line of the input contains an integer T (T <= 10000) denoting the number of test cases.
Following T lines contain an integer N (1 <= N <= 30000000 ).
Output
For each test case, print the case number rst in the format, 'case X: '(here, X is the serial of
Input) followed by a space and then the answer for that case. There is no new-line between cases.
Explanation
Sample 1: For N = 7, there are four valid pairs: (3, 2), (5, 4), (6, 4) and (7, 6 ).
Sample Input
2
7
20000000
Sample Output
Case 1: 4
Case 2: 34866117
Idea: The difference is that the multiple or value of the two I above two cannot be equal to I. You can push it by yourself. With this conclusion, you can simply enumerate the maximum common approx.
# Include <stdio. h> int d [30000001]; int main () {int T, I, j, n = 30000000; for (I = 1; I <= n; I ++) for (j = I + I; j <= n; j + = I) if (j ^ (j-I) = I) d [j] ++; // The difference is a multiple of two I above two I or cannot be equal to I for (I = 1; I <= n; I ++) d [I] + = d [I-1]; scanf ("% d", & T); for (I = 1; I <= T; I ++) {scanf ("% d", & n); printf ("Case % d: % d \ n", I, d [n]) ;}}