Description
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
7
20000000
Sample Output
Case 2: 34866117
xor有个性质:a xor b = c ,那么 a xor c =b;因此,可以枚举a和c,然后算出b=a xor c ,再验证是否有gcd(a,b)=c;
而通过列举几组三元组,我们可以发现c=a-b ,因此,我们只需枚举a和c 取b=a-c ,看是否有c= a xor b 即可。
#include <iostream>
#include <cstdio>
#include <map>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <string>
#define LL long long
using namespace std;
#define maxn 30000005
int s[maxn];
void p()
{
int n=maxn/2;
memset(s,0,sizeof(s));
for(int c=1;c<=n;c++){
for(int a=c+c;a<=maxn;a+=c){
int b=a-c;
if((a^b)==c){
s[a]++;
}
}
}
for(int i=2;i<maxn;i++){
s[i]+=s[i-1];
}
}
int main()
{
int t,n,ca=1;
p();
scanf("%d",&t);
while(t--){
scanf("%d",&n);
printf("Case %d: %d\n",ca++,s[n]);
}
return 0;
}