AtCoder Regular Contest 066--D - Xor Sum--DP

D - Xor Sum
Time Limit: 2 sec / Memory Limit: 256 MB
Score :  points
Problem Statement You are given a positive integer . Find the number of the pairs of integers  and   such that there exist two non-negative integers  and satisfying    and . Here,  denotes the bitwise exclusive OR. Since it can be extremely large, compute the answer modulo .
Constraints
Input The input is given from Standard Input in the following format:
 
Output Print the number of the possible pairs of integers  and , modulo .
Sample Input 1

Sample Output 1

The ve possible pairs of  and  are:
 (Let , then   , .)
 (Let , then   , .)
 (Let , then   , .)
 (Let , then   , .)
 (Let , then   , .)
Sample Input 2
1422 
Sample Output 2
52277 
Sample Input 3
1000000000000000000 
Sample Output 3
787014179

考虑到只需要找到a+b<=n的个数即可。

然后推出公式:

记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],
参考:https://blog.csdn.net/just_sort/article/details/54288233 

#include<iostream>
#include<cmath>
#include<cstdio>
#include<algorithm>
#include<string.h>
#include<map>
using namespace std;
#define ll long long
const ll  MOD=1e9+7;
map<ll,ll>dp;
ll ans(ll x)
{
    if(dp[x])return dp[x]%MOD;
    return dp[x]=(ans((x-1)/2)+ans(x/2)+ans((x-2)/2))%MOD;
}
int main()
{
    dp.clear();
    ll n;
    scanf("%lld",&n);
    dp[0]=1;
    dp[1]=2;
    printf("%lld\n",ans(n)%MOD);
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值