UVA 12716 GCDXOR 数论

UVA-12716解题报告
本文提供了一种解决UVA-12716问题的有效方法,该问题要求找出1到N范围内所有满足GCD(A,B)==A^B条件的数对(A,B)的数量。通过巧妙地利用异或的性质和素数筛法,文章详细介绍了如何将时间复杂度从O(nlognlogn)降低到更优的O(nlogn)。

  题目链接: https://vjudge.net/problem/UVA-12716

  题目描述: 给你一个N, 让你求1~N内所有的GCD(A,B) == A XOR B的个数, 其中1 <= B <= A <= N ,      N <= 3e7

  解题思路: gcd(a, b) = a ^ b = c,   异或的性质是a ^ b = c 则 a ^ c = b 所以我们就枚举a, c得出b , 此时如果gcd(a, b) == c则++ans, 枚举a是O(n), 枚举因数模仿素数筛法枚举a, c是O(nlogn),  判断gcd是logn所以是O(nlognlogn), 这里N是3e7照理说是不会T了, 但是确实是T了, 可能是这个方法卡常, 这里我们还有O(nlogn)的做法, 这样是不会T的, 由于异或是不进位的加法, 所以a - b <= a ^ b 相等的时候是a == b , 同时a - b >= c 由于a ^ b == c 所以说a-b>=c && a-b<=所以a-b==c , 所以这里我们就能O(1)得出b, 再O(1)判断a^b是否==c这样复杂度就能降下来了, 题目就可以A了

  代码: 

#include <iostream>
#include <cstdio>
#include <string>
#include <vector>
#include <cstring>
#include <iterator>
#include <cmath>
#include <algorithm>
#include <stack>
#include <deque>
#include <map>
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
#define mem0(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,0x3f,sizeof(a))
#define fi(n) for(i=0;i<n;i++)
#define fj(m) for(j=0;j<m;j++)
#define sca(x) scanf("%d",&x)
#define scalld(x) scanf("%I64d",&x)
#define print(x) printf("%d\n",x)
#define printlld(x) printf("%I64d\n",x)
#define d printf("=======\n")

typedef long long ll;
using namespace std;
const int maxn = 3e7;

ll ans[maxn+10];

ll gcd( ll a, ll b ) {
    return( b == 0 ? a : gcd( b, a%b ) );
}
void build() {
    mem0(ans);
    int n = maxn >> 1;
    for( int c = 1; c <= n; c++ ) {
        for( int a = 2*c; a <= maxn; a+=c ) {
            ll b = a ^ c;            // TLE!!!!!!!!!!!!!!!!
            if( a >= b && gcd(a, b) == c ) ++ans[a];
//            ll b = a-c;
//            if( (a ^ b) == c ) ++ans[a];
        }
    }
//    for( int i = 1; i <= 10; i++ ) {
//        cout << ans[i] << " ";
//    }
//    cout << endl;
    for( int i = 2; i <= maxn; i++ ) {
        ans[i] += ans[i-1];
    }
}
int main() {
    int t;
//    cout << gcd(9,15) << endl;
    build();
    sca(t);
    int cases = 1;
    while( t-- ) {
        ll res = 0;
        int n;
        sca(n);
        res = ans[n];
        printf( "Case %d: %lld\n", cases++, res );
    }
    return 0;
}
View Code

  思考 : 数论真的是好神奇啊, 对数字的敏感性要求是真的高, 而且这个东西得自己动脑筋去想才能够解题, 同时还要掌握更多的定理来支持自己, 一会儿hihocoder啦, 激动ing......

转载于:https://www.cnblogs.com/FriskyPuppy/p/7400160.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值