Question
Input integer N (1 <= n <= 3*107). How many pairs of Integers (a, B) meet the following conditions: 1 <= B <= A <= n, and gcd (a, B) = a xor B. For example, when n = 7, there are 4 pairs: (3, 2), (5, 4), (6, 4), (7, 6)
Analysis
The main idea of this question is to find a bridge between gcd (A, B) and a ^ B.
- A ^ B ≥ a-B. Oral proof: If the binary bit is the same, the values are all 0. If the binary bit is added, the former must be 1, and the latter may be 1 (1-0 ), it is also possible that the number of digits may be reduced (0-1)
- A-B ≥ gcd (A, B ). Gcd (a, B) = gcd (a, a-B) = gcd (B, A-B) can be obtained from the 9-Chapter numerics ). Obviously, a-B is ≥gcd (a, a-B ).
We now know that gcd (a, B) = a ^ B. According to the clamping force method, a ^ B = a-B = gcd (a, B) = gcd (A, B, a-B)
In other words, a ^ B is equal to a-B, and A-B is a factor of.
Therefore, we enumerate a-B, obtain all its multiples, and then judge whether a-B = a ^ B is true. Time complexity O (N + n/2 + N/3 + ...... + N/n) = O (n logn)
Make sure to enclose the brackets. Otherwise, wait for a few WAF releases.
Code
- # Include <bits/stdc ++. h>
- Using namespace STD;
- # Define n 30000003
- Int t, n;
- Int ans [N];
- Int main ()
- {
- For (INT I = 1; I <n; I ++)
- {
- For (Int J = I * 2; j <n; j + = I)
- If (J ^ (J-I) = I)
- Ans [J] ++;
- Ans [I] + = ans [I-1];
- }
- Scanf ("% d", & T );
- For (INT I = 1; I <= T; I ++)
- {
- Scanf ("% d", & N );
- Printf ("case % d: % d \ n", I, ANS [N]);
- }
- }
[Uva12716] GCD and XOR