XOR
(xor.cpp / .c)
[Title] Description
Given a positive integer n, in [1, n] range, is obtained on the number of random numbers (a, b) satisfying gcd (a, b ) = a xor b. [Input format input common line, a positive integer n. [] Output format output common line, a positive integer answers. Sample [O] [input] Sample 3 [output] Sample 1 [explain] sample only (2, 3) meet the requirements. [Data] range of 30% of the data, n≤1000. For 60% of the data, n≤10 ^ 5. To 100% of the data, n≤10 ^ 7.
Title effect: Given n, find [1, n] in the number of gcd (a, b) == a ^ b; (unordered);
Assume that a> b, we know that gcd (a, b) = gcd (ab, b), while two positive integers gcd must not exceed two numbers, so gcd (a, b) ≤ab
Let a i-th bit of x, the i-th bit b to y.
When x = 1 or x = y = 0, there xy = x xor y.
When x = 0 and y = 1, there are x xor y = 1, xy = -1, i.e. x xor y> xy.
So, we have come a xor b≥ab.
Therefore, gcd (a, b) = a ^ b = ab;
Setting c = ab, then there gcd (a, ac) = c, i.e., we need to satisfy a multiple of a and c a ≠ c
Complexity: in [1, n] are multiples of the i [n / i] a, so complexity: nlogn;
#include <bits/stdc++.h> using namespace std; int f[10000001]; int main() { int n; cin>>n; for(int i=1;i<=5000000;i++){ for(int j=i*2;j<10000000;j+=i){ int b=j-i; if((j^b)==i) f[j]++; } } for(int i=1;i<=n;i++) f[i]+=f[i-1]; cout<<f[n]; }