UVa12716GCD XOR

/**
    首先可以打表找规律,找到规律我们可以发现:
    1.如果gcd(a,b) = a ^ b = c,那么 b = a - c;
    既然这样我们可以枚举a,c,求出b之后判断 c 是否等于 a ^ b,那么如何枚举c呢?
    2.利用类似筛选素数的方法去枚举a,c
    首先c是a的约数,所以这道题我们需要枚举的其实是a的约数,但是约数也不好枚举,我们可以通过c去枚举a,我们通过枚举c,找到约数是c的所有a.
    这题由于数据过大,需要打表,否则超时.
*/
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn = 3e7+10;
int ans[maxn];
void init()
{
    memset(ans,0,sizeof(ans));
    for(int c=1;c< (maxn >> 1) ; c++)
    {
        for(int a=c+c;a<maxn;a+=c)
        {
            int b = a - c;
            if((a^b) == c) ans[a]++;
        }
    }
    for(int i=2;i<=maxn;i++) ans[i] = ans[i-1] + ans[i];
}

int main()
{
    init();
    int caset,n,t = 1;
    scanf("%d",&caset);
    while(caset--)
    {
        scanf("%d",&n);
        printf("Case %d: %d\n",t++,ans[n]);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值