UVA - 12716 GCD XOR(找规律+枚举技巧)

传送门


一开始想的是,先枚举 a a a,对每个 a a a因数分解,可是这样做的世界复杂度为 O ( n ∗ n ) O(n*\sqrt{n}) O(nn ),显然不是LRJ提到的 O ( n l o g n ) O(nlogn) O(nlogn)

然后这题数据太大也不能打表,只能看看题解怎么做了。原来我们不需要枚举 a a a,而是枚举范围内的所有因数 c c c,然后找到这个因数在范围内的若干倍,这样就类似素数筛选那里了,但是这样只是求某个数有多少对,因此还需要累积前缀和,预处理即可

#include <set>
#include <map>
#include <stack>
#include <queue>
#include <math.h>
#include <cstdio>
#include <string>
#include <bitset>
#include <cstring>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <unordered_map>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define ins insert
#define lowbit(x) (x&(-x))
#define mkp(x,y) make_pair(x,y)
#define mem(a,x) memset(a,x,sizeof a);
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int,int> P;
const double eps=1e-8;
const double pi=acos(-1.0);
const int inf=0x3f3f3f3f;
const ll INF=1e18;
const int Mod=1e9+7;
const int maxn=3e7+10;

int ans[maxn];

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

int main(){
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    int t,n,kase=0;
    init();
    cin>>t;
    while(t--){
        cin>>n;
        cout<<"Case "<<++kase<<": "<<ans[n]<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值