您的位置:首页 > 其它

UVA 12716 GCD XOR

2014-11-10 15:50 344 查看

12716 GCD XOR

Given an integer N, find 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 first 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 first 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 <cstring>
#include <cstdio>
#include <algorithm>

using namespace std;

const int maxn=30000030;

int d[maxn];

void init()
{
for(int c=1;c<=maxn/2;c++)
{
for(int a=c+c;a<maxn;a+=c)
{
int b=a-c;
if((a^b)==c) d[a]++;
}
}
for(int i=2;i<maxn;i++)
d[i]+=d[i-1];
}

int main()
{
init();
int T_T,cas=1;
scanf("%d",&T_T);
while(T_T--)
{
int n;
scanf("%d",&n);
printf("Case %d: %d\n",cas++,d
);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: