GCD XOR uvalive6657

时间:2023-03-09 15:59:57
GCD XOR    uvalive6657

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

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
using namespace std;
int ans[]={};
void init()
{
int i,j;
for(i=;i<;i++)
{
for(j=i+i;j<;j+=i)
if((j^(j-i))==i)ans[j]++;
}
for(i=;i<;i++)ans[i]+=ans[i-];
}
int main()
{
int t,i,n;
init();
scanf("%d",&t);
for(i=;i<=t;i++)
{
scanf("%d",&n);
printf("Case %d: %d\n",i,ans[n]);
}
}