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;
}