zoukankan      html  css  js  c++  java
  • Xor_Sum 题解

    题目

    You are given a positive integer (N(1≦N≦10^{18})). Find the number of the pairs of integers (u) and (v (0≦u,v≦N)) such that there exist two non-negative integers (a) and (b) satisfying (a xor b=u) and (a+b=v). Here, (xor) denotes the bitwise exclusive OR. Since it can be extremely large, compute the answer modulo (10^9+7).

    给出正整数(N),求出整数对(u)(v (0≤u,v≤N))的数目,使得存在两个非负整数(a)(b)满足(a xor b = u)(a + b= v)。这里,(xor)表示按位异或。对答案取模(10^9 + 7)

    输入格式

    The input is given from Standard Input in the following format: (N)

    一个整数(N)

    输出格式

    Print the number of the possible pairs of integers (u) and (v) ,modulo (10^9+7).

    (u,v)对的数量模(10^9+7)

    输入样例

    3
    

    输出样例

    5
    

    题解

    (n=1,2,3,4,5...)的答案手算出来, 是1, 2, 4, 5, 8, 10, 13, 14, 18, 21, 26, 28, 33, 36, 40, 41, 46, 50, 57, 60...然后找规律, 如果不好找, 可以在这个网站搜索.

    用记忆化搜索优化效率, 如果开数组开不下, 用map即可

    我怀疑这个不是正解

    代码

    #include <cstdio>
    #include <map>
    const long long MOD = 1e9 + 7;
    std::map<long long, long long> dp;
    long long f(long long x) {
        if (dp[x]) return dp[x] % MOD;
        return dp[x] = (f((x - 1) / 2) + f(x / 2) + f((x - 2) / 2)) % MOD;
    }
    int main() {
        long long n;
        scanf("%lld", &n);
        dp[0] = 1;
        dp[1] = 2;
        printf("%lld
    ", f(n) % MOD);
    }
    
  • 相关阅读:
    topcoder srm 495 div1
    topcoder srm 500 div1
    topcoder srm 485 div1
    topcoder srm 490 div1
    IDEWorkspaceChecks.plist文件是干什么用的?
    博客推荐
    如何使用U盘安装macOS high Sierra?
    小程序--模板消息调研
    小程序--剖析小程序上传文件
    小程序--小程序开发过程中遇到的问题以及解决方案
  • 原文地址:https://www.cnblogs.com/youxam/p/xor-sum.html
Copyright © 2011-2022 走看看