D - Xor Sum(dp)

Problem Statement

You are given a positive integer NN. Find the number of the pairs of integers uu and vv (0≦u,v≦N)(0≦u,v≦N) such that there exist two non-negative integers aa and bbsatisfying aa xorxor b=ub=u and a+b=va+b=v. Here, xorxor denotes the bitwise exclusive OR. Since it can be extremely large, compute the answer modulo 109+7109+7.

Constraints

  • 1≦N≦10181≦N≦1018

Input

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

NN

Output

Print the number of the possible pairs of integers uu and vv, modulo 109+7109+7.


Sample Input 1 Copy

Copy

3

Sample Output 1 Copy

Copy

5

The five possible pairs of uu and vv are:

  • u=0,v=0u=0,v=0 (Let a=0,b=0a=0,b=0, then 00 xorxor 0=00=0, 0+0=00+0=0.)

  • u=0,v=2u=0,v=2 (Let a=1,b=1a=1,b=1, then 11 xorxor 1=01=0, 1+1=21+1=2.)

  • u=1,v=1u=1,v=1 (Let a=1,b=0a=1,b=0, then 11 xorxor 0=10=1, 1+0=11+0=1.)

  • u=2,v=2u=2,v=2 (Let a=2,b=0a=2,b=0, then 22 xorxor 0=20=2, 2+0=22+0=2.)

  • u=3,v=3u=3,v=3 (Let a=3,b=0a=3,b=0, then 33 xorxor 0=30=3, 3+0=33+0=3.)


Sample Input 2 Copy

Copy

1422

Sample Output 2 Copy

Copy

52277

Sample Input 3 Copy

Copy

1000000000000000000

Sample Output 3 Copy

Copy

787014179

题意:
1<=u,v<=n,存在多少对u,v使得存在(a+b=u,a^v=v);

题意:
自己先感觉有规律打了个表发现找不到规律,看了大佬的博客发现f(n)=f(n/2)+f((n-1)/2)+f((n-2)/2);

详解请看大佬博客:https://blog.csdn.net/axuhongbo/article/details/79169565?utm_source=blogxgwz9

代码:
 

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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值