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. GridView.SelectedIndex

    获取或设置 GridView 控件中的选中行的索引. C#复制 [System.ComponentModel.Bindable(true)] public virtual int SelectedIn ...

  2. 转换嵌套JSON数据为TABLE

    先准备一些数据: 创建一张临时表来存储: DECLARE @json_table AS TABLE ( [type] NVARCHAR(MAX), [desc] NVARCHAR(MAX) ) Sou ...

  3. Automake使用(初级)

    工程地址 https://github.com/silvermagic/automakeDev.git 最初工程目录结构 $ vim main.cpp $ vim src/main.cpp ls -l ...

  4. [poj] Catch That Cow--bfs

    Description Farmer John has been informed of the location of a fugitive cow and wants to catch her i ...

  5. 通过node.js读取html页面及其页面中引入的css样式

    Node 是什么? Node 是一个基于Chrome V8 引擎的JavaScript 运行环境. Node 不是一种独立的语言.Node不是 JavaScript 框架, Node是一个除了浏览器之 ...

  6. Solidity 最新 0.5.8 中文文档发布

    本文首发于深入浅出区块链社区 热烈祝贺 Solidity 最新 0.5.8 中文文档发布, 这不单是一份 Solidity 速查手册,更是一份深入以太坊智能合约开发宝典. 翻译说明 Solidity ...

  7. Maven项目已启动但是报异常访问webapp下所有资源都404

  8. Java与Javac版本不一致问题解决方案

    问题:在自己电脑上运行java -version和javac -version,发现java版本不一致,然后查看了环境变量,JAVA_HOME 是 C:\Program Files(x86)\Java ...

  9. HDU-1179-Ollivanders(二分图最大匹配)

    链接:https://vjudge.net/problem/HDU-1179 题意: 有n个法师和m个魔棒,每个法师喜欢多种魔棒,但每个法师只能在喜欢的魔棒中选一个. 求最多有几个法师能选到魔棒. 思 ...

  10. Codeforces #564div2 C(模拟)

    要点 没想到的一点是,对于堆里的某牌,最好情况是它出来时后边都准备就绪了,答案是\(p[i] + (n - i + 1)\),所有的这个取最大的即可 一发结束的情况先特判一下 const int ma ...