6498: Xor Sum

时间限制: 1 Sec  内存限制: 128 MB
提交: 27  解决: 13
[提交][状态][讨论版][命题人:admin]

题目描述

You are given a positive integer N. 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 109+7.

Constraints
1≤N≤1018

输入

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

输出

Print the number of the possible pairs of integers u and v, modulo 109+7.

样例输入

3

样例输出

5

提示

The five possible pairs of u and v are:
u=0,v=0 (Let a=0,b=0, then 0 xor 0=0, 0+0=0.)
u=0,v=2 (Let a=1,b=1, then 1 xor 1=0, 1+1=2.)
u=1,v=1 (Let a=1,b=0, then 1 xor 0=1, 1+0=1.)
u=2,v=2 (Let a=2,b=0, then 2 xor 0=2, 2+0=2.)
u=3,v=3 (Let a=3,b=0, then 3 xor 0=3, 3+0=3.)

按位考虑,因为a xor b = <=a+b,实际上只需要考虑a+b<=n,
但是要求a的每一位不大于b的每一位(关键点,否则u,v会有重复),那么对于两组不同的(a1,b1)和(a2,b2),
如果a1 xor b1等于a2 xor b2,则异或值均为零,
这表明每种(a,b)的取法都会导致不同的(a xor b,a+b)。
 
解法:记dp[i]为满足a+b<=i的(a,b)对的个数,枚举a和b的最低位,记a=2a1+a2,b=2b1+b2,其中a2,b2=0,1且a2<=b2,
那么有a1+b1<=(n-a2-b2)/2,因为a2+b2只能是0,1,2,则有dp[i]=dp[i/2]+dp[(i-1)/2]+dp[(i-2)/2],
那么对于dp[n],可以分析出需要计算的状态数是O((logn)^2)的。
此处对map的用法要熟悉,否则在时间和空间上都会爆!
AC代码:

#include<bits/stdc++.h>
using namespace std;
const long long mod=1e9+7;
map<long long ,long long>dp;
long long solve(long long x)
{
if(dp[x])
{
return dp[x];
}
else
{
return dp[x]=((solve(x/2)+solve((x-1)/2)+solve((x-2)/2)))%mod;
}
}
int main()
{
dp[0]=1;
dp[1]=2;
long long n;
cin>>n;
cout<<solve(n)<<endl;
return 0;
}

Xor Sum的更多相关文章

  1. HDU 4825 Xor Sum(经典01字典树+贪心)

    Xor Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others) Total ...

  2. 字典树-百度之星-Xor Sum

    Xor Sum Problem Description Zeus 和 Prometheus 做了一个游戏,Prometheus 给 Zeus 一个集合,集合中包括了N个正整数,随后 Prometheu ...

  3. HDU 4825 Xor Sum 字典树+位运算

    点击打开链接 Xor Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others) ...

  4. 2014百度之星第三题Xor Sum(字典树+异或运算)

    Xor Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others) Total ...

  5. Xor Sum 01字典树 hdu4825

    Xor Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others)Total S ...

  6. hdu 4825 Xor Sum (01 Trie)

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=4825 题面: Xor Sum Time Limit: 2000/1000 MS (Java/Others) ...

  7. HDU--4825 Xor Sum (字典树)

    题目链接:HDU--4825 Xor Sum mmp sb字典树因为数组开的不够大一直wa 不是报的 re!!! 找了一下午bug 草 把每个数转化成二进制存字典树里面 然后尽量取与x这个位置上不相同 ...

  8. hdu 4825 Xor Sum trie树

    Xor Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others) Proble ...

  9. hdu 4825 Xor Sum(trie+贪心)

    hdu 4825 Xor Sum(trie+贪心) 刚刚补了前天的CF的D题再做这题感觉轻松了许多.简直一个模子啊...跑树上异或x最大值.贪心地让某位的值与x对应位的值不同即可. #include ...

  10. UVALive4682 XOR Sum

    UVALive4682 XOR Sum 题意 给定一个数组, 求连续子序列中异或值最大的值. 题解 假设答案区间为 [L, R], 则答案为 XOR[L, R], 可以将区间分解为 XOR[L,R] ...

随机推荐

  1. C++ - main()函数参数

    main()函数及其参数说明 main()函数主要形式: int main(void) int main(int argc, char *argv[]) = int main(int argc, ch ...

  2. 数据库路由中间件MyCat - 源代码篇(9)

    此文已由作者张镐薪授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 3. 连接模块 3.5 后端连接 3.5.1 后端连接获取与负载均衡 上一节我们讲了后端连接的基本建立和响应 ...

  3. 求斐波那契数列第n位的几种实现方式及性能对比(c#语言)

    在每一种编程语言里,斐波那契数列的计算方式都是一个经典的话题.它可能有很多种计算方式,例如:递归.迭代.数学公式.哪种算法最容易理解,哪种算法是性能最好的呢? 这里给大家分享一下我对它的研究和总结:下 ...

  4. yii2之使用ueditor

    代码效果: 1.去github下载yii2高级版ueditor扩展 2.将下载的扩展放入  /common/widgets 中(目录如图所示) 3.在视图中的代码 <?=common\widge ...

  5. linux 删除软链接

    Linux下取消软连接,做个案例来说明: 1.先建立一个软连接 ln -s /soft/hexo/nodejs/bin/node /usr/local/bin/node ln -s /soft/hex ...

  6. cf706C(dp)

    题目链接:http://codeforces.com/problemset/problem/706/C 题意:给出n个字符串,反转第 i 个字符串需要花费 ai,问通过反转操作将n个字符串变成升序排列 ...

  7. [Xcode 实际操作]七、文件与数据-(11)数据持久化存储框架CoreData的使用:创建CoreData实体并插入数据

    目录:[Swift]Xcode实际操作 本文将演示[CoreData]数据持久化存储框架的使用. 点击[Create a new Xcode project]创建一个新的项目 ->[Single ...

  8. phpstorm使用

    生成注释快捷键/**+enter 注释快捷键ctrl+/

  9. ADO.NET 学习链接

    在博客园上,这个系列的文章对ADO.NET 总结的很好. ADO.NET 系列文章

  10. CC07:清除行列

    题目 请编写一个算法,若N阶方阵中某个元素为0,则将其所在的行与列清零. 给定一个N阶方阵int[][](C++中为vector>)mat和矩阵的阶数n,请返回完成操作后的int[][]方阵(C ...