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
3
Sample Output 1
5
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);
}