找规律
题目
You are given a positive integer N(1≦N≦1e18). 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 1e9+7.
Input
The input is given from Standard Input in the following format:N
Output
u,v 对的数量 模1e9+7
Sample Input 1
3
Sample Output 1
5
Sample Input 2
1422
Sample Output 2
52277
Sample Input 3
1000000000000000000
Sample Output 3
787014179
题意
给出正整数N,求出整数对u和v(0≤u,v≤N)的数目,使得存在两个非负整数a和b满足a xo rb=u 和a+b=v。这里,xor表示按位异或。对答案取模1e9+7
分析
这个题就是找规律,然后递归找答案,当然,递归是需要记忆化搜索的,不然可能会爆掉。如果写递推也是可以的(当然我写的递归);
代码
#include<bits/stdc++.h> using namespace std; #define ll long long const int Mod = 1e9+7; map<ll, ll>ans; ll f(ll x){ if (ans[x])return ans[x] % Mod; return ans[x]=(f(x/2)+f((x-1)/2)+f((x-2)/2))%Mod; } int main(){ ll n; scanf("%lld",&n); ans[0] = 1; ans[1] = 2; printf("%lld\n", f(n) % Mod); }
PS:感谢zhl大佬提供的查找公式的网站。
$Never\ Give\ Up$