Problem Statement
You are given a positive integer N. 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 xorb=u and a+b=v. Here, xor denotes the bitwise exclusive OR. Since it can be extremely large, compute the answer modulo 10^9+7.
Constraints
Input
The input is given from Standard Input in the following format:
N
Output
Print the number of the possible pairs of integers u and v, modulo 10^9+7.
Sample Input 1
3
Sample Output 1
5
The five possible pairs of u and v are:
-
u=0,v=0 (Let a=0,b=0, then 0 xor 0=0, 0+0=0.)
-
u=0,v=2 (Let a=1,b=1, then 1 xor 1=0, 1+1=2.)
-
u=1,v=1 (Let a=1,b=0, then 1 xor 0=1, 1+0=1.)
-
u=2,v=2 (Let a=2,b=0, then 2 xor 0=2, 2+0=2.)
-
u=3,v=3 (Let a=3,b=0, then 3 xor 0=3, 3+0=3.)
Sample Input 2
1422
Sample Output 2
52277
Sample Input 3
1000000000000000000
Sample Output 3
787014179
思路:
打出前面一些项的答案:
1, 2, 4, 5, 8, 10, 13, 14, 18, 21, 26, 28, 33, 36, 40, 41, 46, 50, 57, 60, 68, 73, 80, 82, 89, 94, 102, 105, 112, 116, 121, 122, 128, 133, 142, 146, 157, 164, 174, 177, 188, 196, 209, 214, 226, 233, 242, 244, 253, 260, 272, 277, 290, 298, 309, 312, 322, 329,
可以发现这样的规律:
d
a(2k) = 2*a(k-1) + a(k);
a(2k+1) = 2*a(k) + a(k-1).
然后用数组记录前面一些项目,然后开一个map记录中间递归过程访问过的变量(即,记忆话搜索)
然后直接递归处理上面发现的递归式即可了。
这也是OEIS中的一个数列。
http://oeis.org/A007729
A007729 |
|
6th binary partition function. |
|
4 |
|
|
1, 2, 4, 5, 8, 10, 13, 14, 18, 21, 26, 28, 33, 36, 40, 41, 46, 50, 57, 60, 68, 73, 80, 82, 89, 94, 102, 105, 112, 116, 121, 122, 128, 133, 142, 146, 157, 164, 174, 177, 188, 196, 209, 214, 226, 233, 242, 244, 253, 260, 272, 277, 290, 298, 309, 312, 322, 329, 340, 344 (list; graph; refs; listen; history; text; internal format) |
|
|
OFFSET |
0,2 |
|
COMMENTS |
From Gary W. Adamson, Aug 31 2016: (Start) The sequence is the left-shifted vector of the production matrix M, with lim_{k->inf} M^k. M = 1, 0, 0, 0, 0, ... 2, 0, 0, 0, 0, ... 2, 1, 0, 0, 0, ... 1, 2, 0, 0, 0, ... 0, 2, 1, 0, 0, ... 0, 1, 2, 0, 0, ... 0, 0, 2, 1, 0, ... 0, 0, 1, 2, 0, ... ... The sequence is equal to the product of its aerated variant by (1,2,2,1): (1, 2, 2, 1) * (1, 0, 2, 0, 4, 0, 5, 0, 8, ...) = (1, 2, 4, 5, 8, 10, ...). Term a((2^n) - 1) = A007051: (1, 2, 5, 14, 41, 122, ...). (End) a(n) is the number of ways to represent 2n (or 2n+1) as a sum e_0 + 2*e_1 + ... + (2^k)*e_k with each e_i in {0,1,2,3,4,5}. - Michael J. Collins, Dec 25 2018 |
|
LINKS |
Alois P. Heinz, Table of n, a(n) for n = 0..10000 Michael J. Collins, David Wilson, Equivalence of OEIS A007729 and A174868, arXiv:1812.11174 [math.CO], 2018. B. Reznick, Some binary partition functions, in "Analytic number theory" (Conf. in honor P. T. Bateman, Allerton Park, IL, 1989), 451-477, Progr. Math., 85, Birkhäuser Boston, Boston, MA, 1990. |
|
FORMULA |
G.f.: (r(x) * r(x^2) * r(x^4) * r(x^8) * ...) where r(x) = (1 + 2x + 2x^2 + x^3 + 0 + 0 + 0 + ...). - Gary W. Adamson, Sep 01 2016 a(2k) = 2*a(k-1) + a(k); a(2k+1) = 2*a(k) + a(k-1). - Michael J. Collins, Dec 25 2018 |
|
MAPLE |
b:= proc(n) option remember; `if`(n<2, n, `if`(irem(n, 2)=0, b(n/2), b((n-1)/2) +b((n+1)/2))) end: a:= proc(n) option remember; b(n+1) +`if`(n>0, a(n-1), 0) end: seq(a(n), n=0..70); # Alois P. Heinz, Jun 21 2012 |
|
MATHEMATICA |
b[n_] := b[n] = If[n<2, n, If[Mod[n, 2] == 0, b[n/2], b[(n-1)/2]+b[(n+1)/2]]]; a[n_] := a[n] = b[n+1] + If[n>0, a[n-1], 0]; Table[a[n], {n, 0, 70}] (* Jean-François Alcover, Mar 17 2014, after Alois P. Heinz *) |
|
CROSSREFS |
A column of A072170. Cf. A002487, A007051. Apart from an initial zero, coincides with A174868. Sequence in context: A179509 A157007 A173509 * A174868 A268381 A186349 Adjacent sequences: A007726 A007727 A007728 * A007730 A007731 A007732 |
|
KEYWORD |
nonn |
|
AUTHOR |
N. J. A. Sloane |
|
EXTENSIONS |
More terms from Vladeta Jovovic, May 06 2004 |
|
STATUS |
approved |
|
细节见代码:
#include
#include
#include
#include
#include
#include
#include
#include