6657 GCD XOR

6657 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 divisorof the numbers A and B. And A xor B is the value of the bitwise xor operationon 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 containan integer N (1<= N<=30000000).

 

Output

For each test case, print the case number firstin the format, ‘Case X:’ (here, X is the serial of the input) followed by aspace 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[30000010]={0};
void init()
{
    int i,j;
    for(i=1;i<30000010;i++)     //gcd的值;
    {
        for(j=i+i;j<30000010;j+=i)    //列举dcd为i的所有情况;
        if((j^(j-i))==i)ans[j]++;    // ^ 是异或运算,gcd(j,j-i)=i;
    }
    for(i=1;i<30000010;i++)ans[i]+=ans[i-1];  //统计;
}
int main()
{
    int t,i,n;
    init();
    scanf("%d",&t);
    for(i=1;i<=t;i++)
    {
        scanf("%d",&n);
        printf("Case %d: %d\n",i,ans[n]);
    }
	return 0;
}

代码借鉴他人.......

这里对于gcd的处理可以学一下!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值