uval 6657 GCD XOR
GCD XOR
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 the
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. The
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 the
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
题意:给出n,求出a<=n,b<=n,且gcd(a,b)==a xor b 的对数
思路:找规律得出的解,数学证明暂无,以后如果想出再补(八成补不出来= =)。找规律得出,如果a,b(a<b)满足上述条件,那么a'=a/gcd(a,b),b'=b/gcd(a,b),则a'=2k,b'=2k+1(k为正整数)。那么只要枚举最大公约数t,然后构造(mt)和(m+1)t,然后测试(mt)xor(m+1)t==t就行了。
1 /* 2 * Author: Joshua 3 * Created Time: 2014年10月05日 星期日 20时36分26秒 4 * File Name: h.cpp 5 */ 6 #include<cstdio> 7 #define maxn 30000001 8 int f[maxn]; 9 int n,T; 10 11 void solve() 12 { 13 for (int i=1;i<maxn;++i) 14 for (int j=i+i;j<maxn;j+=i) 15 if ((j^(j-i))==i) f[j]++; 16 for (int i=1;i<maxn;++i) 17 f[i]+=f[i-1]; 18 } 19 20 int main() 21 { 22 solve(); 23 scanf("%d",&T); 24 int x; 25 for (int i=1;i<=T;++i) 26 { 27 scanf("%d",&x); 28 printf("Case %d: %d\n",i,f[x]); 29 } 30 return 0; 31 }