标签:style inpu fine for ret exclusive 存在 standard 递推
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.
The input is given from Standard Input in the following format:N
u,v 对的数量 模1e9+7
3
5
1422
52277
1000000000000000000
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大佬提供的查找公式的网站。
标签:style inpu fine for ret exclusive 存在 standard 递推
原文地址:https://www.cnblogs.com/Vocanda/p/12868176.html