zoukankan      html  css  js  c++  java
  • GCD XOR UVA 12716 找规律 给定一个n,找多少对(a,b)满足1<=b<=a<=n,gcd(a,b)=a^b;

    /**
    题目:GCD XOR UVA 12716
    链接:https://vjudge.net/problem/UVA-12716
    题意:给定一个n,找多少对(a,b)满足1<=b<=a<=n,gcd(a,b)=a^b;
    思路:
    打表找规律发现:满足条件的结果一定满足,gcd(a,b) = a-b;
    
    即:先确定每一个a以及它的约数c可以获得,b = a-c; 如果a^b=c 那么满足。
    
    时间复杂度nlg(n)
    */
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <set>
    #include <vector>
    #include <cmath>
    using namespace std;
    typedef long long ll;
    typedef unsigned long long ull;
    const int inf = 0x3f3f3f3f;
    const int maxn = 3e7+10;
    const double eps = 1e-6;
    ll f[maxn];
    void init()
    {
        memset(f, 0, sizeof f);
        for(int i = 1; i <= maxn/2; i++){
            for(int j = i*2; j < maxn; j+=i){
                int b = j-i;
                if((j^b)==i){
                    f[j]++;
                }
            }
        }
        for(int i = 1; i < maxn; i++) f[i] += f[i-1];
    }
    int main()
    {
        int n;
        int T, cas=1;
        init();
        cin>>T;
        while(T--){
            scanf("%d",&n);
            printf("Case %d: %lld
    ",cas++,f[n]);
        }
        return 0;
    }
  • 相关阅读:
    Django的中间件
    电脑端调手机端
    计算两个时间戳之间相差的时间
    去除html标签 php
    Tp5 一次修改多个数据update
    ThinkPhp5.0 引入全局自定义函数global
    layer 使用教程
    phpstrom 汉化
    phpcms 电脑手机合并
    vue 中 v-model
  • 原文地址:https://www.cnblogs.com/xiaochaoqun/p/6738218.html
Copyright © 2011-2022 走看看