Xor Sum(AtCoder-2272)
Problem Description
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≦10^18
Input
The input is given from Standard Input in the following format:
N
Output
Print the number of the possible pairs of integers u and v, modulo 109+7.
Example
Sample Input 1
3
Sample Output 1
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.)
Sample Input 2
1422
Sample Output 2
52277
Sample Input 3
1000000000000000000
Sample Output 3
787014179
题意:给出一个整数 n,已知 0<=u,v<=n,求满足 a xor b=u 且 a+b=v 的 a、b 对数
思路:
首先打表,可以发现前几项有:1、2、4、5、8、10、13、14、18、21、26、28、33、36、40、41、46、50、57、60、68
可以发现:
因此直接用数组记录前面已有的项目,再开一个 map 用于记忆化搜索,然后递归即可
Source Program
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#define EPS 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
const int MOD = 1E9+7;
const int N = 1000000+5;
const int dx[] = {0,0,-1,1,-1,-1,1,1};
const int dy[] = {-1,1,0,0,-1,1,-1,1};
using namespace std;LL a[25]={1,2,4,5,8,10,13,14,18,21,26,28,33,36,40,41,46,50,57,60,68};
map<LL,LL> mp;
LL cal(LL x) {if(x<=20)return a[x];if(mp[x])return mp[x];if(x%2)return mp[x]=(2*cal(x/2)%MOD+cal(x/2-1)%MOD)%MOD;elsereturn mp[x]=(2*cal(x/2-1)%MOD+cal(x/2)%MOD)%MOD;
}
int main() {LL n;scanf("%lld",&n);printf("%lld\n",cal(n));return 0;
}
Xor Sum(AtCoder-2272)相关推荐
- MySql sum(A+B)和sum(A)+sum(B)的区别
1.如果字段A和字段B的值都不为NULL,则以上两种情况一样 2.如果其中有一个字段的值为NULL则sum(A+B)的值为NULL 3.如果不想想这么多,使用sum(A)+sum(B)可一劳永逸
- 2014百度之星资格赛—— Xor Sum(01字典树)
Xor Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others) Total ...
- 【HDOJ6955】Xor sum(异或前缀和,01字典树)
1006 Xor sum 题意: 给出一个长度为n的序列,求一个最短连续子序列满足异或和大于等于k.n<1e5. 思路: 参考CF665E,求序列a中有多少个异或和大于等于k的子序列,枚举所有的 ...
- 牛客 XOR和(找规律)
文章目录 1. 题目 2. 解题 1. 题目 链接:https://ac.nowcoder.com/acm/contest/10166/C 来源:牛客网 牛牛最近学会了异或操作,于是他发现了一个函数 ...
- Tinkoff Internship Warmup Round 2018 and Codeforces Round #475 (Div. 2) C.Alternating Sum(等比数列求和)
题目链接;点击打开链接 题目大意:给出一个数列s,其中的元素为s1,s2,s3......sn.元素要么是1(用'+'表示),要么是-1(用'-'表示) 这个数列以k个数为一个周期,且n能被k整除. ...
- 2018.08.10 atcoder Median Sum(01背包)
传送门 题意简述:输入一个数组anan. 对于所有2n−12n−1个非空子集,每个子集的权值是包含的所有元素之和. 求这2n−12n−1个非空子集权值的中位数. 对于每个权值vv都有一个对应的&quo ...
- 牛客网暑期ACM多校训练营(第九场)H. Prefix Sum(CDQ分治)
题目描述 Niuniu has learned prefix sum and he found an interesting about prefix sum. Let's consider (k+1 ...
- 数学--数论--HDU 6128 Inverse of sum (公式推导论)
Description 给nn个小于pp的非负整数a1,-,na1,-,n,问有多少对(i,j)(1≤i<j≤n)(i,j)(1≤i<j≤n)模pp在意义下满足1ai+aj≡1ai+1aj ...
- Codeforces 766E Mahmoud and a xor trip(树形DP)
题目链接 Mahmoud and a xor trip 树形DP.先考虑每个点到他本身的距离和,再算所有点两两距离和. 做的时候考虑二进制拆位即可. #include <bits/stdc++. ...
最新文章
- 线段树、二叉堆以及离散化入门
- 学习JS的心路历程-参数传递方式(上)
- Phantomjs代理设置
- [hackinglab][CTF][解密关][2020] hackinglab 解密关 writeup
- 一起学习C语言:C语言循环结构(二)
- 95-38-025-Buffer-Buffer1
- linux禁止客户端上传文件_linux 文件服务
- prd移动端通用产品需求文档+Axure高保真app社交餐饮通用prd文档+产品业务说明+通用prd文档+通用prd模板+全局说明+需求分析+竞品分析+产品结构图+产品业务流程图+产品信息图+餐饮系统
- Mac 安装JDK 8
- w10恢复出厂设置_笔记本电脑w10怎么恢复出厂设置
- [window] win7系统下电脑声音正常,右下角的小喇叭图标不见了
- js 让鼠标右下角有一排小字_JavaScript+css代码实现漂亮的右下角弹窗提示
- 手机OTG是什么功能,怎样使用?
- linux apache 503,Apache ProxyPass出现503 Service Temporarily Unavailable错误
- 关于搞国外广告联盟的一些思路
- OtterCTF 内存取证
- 十年测试老司机带你应对68道面试题---文章转载自 TesterHome
- 五、传输层(三)TCP
- Linux下串口的配置
- 如何统计PHP程序的运行时间、耗费内存