Problem Statement

 

You are given a positive integer <var>N</var>. Find the number of the pairs of integers <var>u</var> and <var>v</var><var>(0≦u,v≦N)</var> such that there exist two non-negative integers <var>a</var> and <var>b</var> satisfying <var>a</var> <var>xor</var><var>b=u</var> and <var>a+b=v</var>. Here, <var>xor</var> denotes the bitwise exclusive OR. Since it can be extremely large, compute the answer modulo <var>10^9+7</var>.

Constraints

 

  • <var>1≦N≦10^{18}</var>

Input

 

The input is given from Standard Input in the following format:

<var>N</var>

Output

 

Print the number of the possible pairs of integers <var>u</var> and <var>v</var>, modulo <var>10^9+7</var>.

Sample Input 1

 

3

Sample Output 1

 

5

The five possible pairs of <var>u</var> and <var>v</var> are:

  • <var>u=0,v=0</var> (Let <var>a=0,b=0</var>, then <var>0</var> <var>xor</var> <var>0=0</var>, <var>0+0=0</var>.)

  • <var>u=0,v=2</var> (Let <var>a=1,b=1</var>, then <var>1</var> <var>xor</var> <var>1=0</var>, <var>1+1=2</var>.)

  • <var>u=1,v=1</var> (Let <var>a=1,b=0</var>, then <var>1</var> <var>xor</var> <var>0=1</var>, <var>1+0=1</var>.)

  • <var>u=2,v=2</var> (Let <var>a=2,b=0</var>, then <var>2</var> <var>xor</var> <var>0=2</var>, <var>2+0=2</var>.)

  • <var>u=3,v=3</var> (Let <var>a=3,b=0</var>, then <var>3</var> <var>xor</var> <var>0=3</var>, <var>3+0=3</var>.)

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
  <tt>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</tt> (listgraphrefslistenhistorytextinternal format)
  OFFSET

<tt>0,2</tt>

  COMMENTS

<tt>From Gary W. Adamson, Aug 31 2016: (Start)</tt>

<tt>The sequence is the left-shifted vector of the production matrix M, with lim_{k->inf} M^k. M =</tt>

<tt>  1, 0, 0, 0, 0, ...</tt>

<tt>  2, 0, 0, 0, 0, ...</tt>

<tt>  2, 1, 0, 0, 0, ...</tt>

<tt>  1, 2, 0, 0, 0, ...</tt>

<tt>  0, 2, 1, 0, 0, ...</tt>

<tt>  0, 1, 2, 0, 0, ...</tt>

<tt>  0, 0, 2, 1, 0, ...</tt>

<tt>  0, 0, 1, 2, 0, ...</tt>

<tt>  ...</tt>

<tt>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, ...).</tt>

<tt>Term a((2^n) - 1) = A007051: (1, 2, 5, 14, 41, 122, ...). (End)</tt>

<tt>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</tt>

  LINKS

<tt>Alois P. Heinz, Table of n, a(n) for n = 0..10000</tt>

<tt>Michael J. Collins, David Wilson, Equivalence of OEIS A007729 and A174868, arXiv:1812.11174 [math.CO], 2018.</tt>

<tt>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.</tt>

  FORMULA

<tt>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</tt>

<tt>a(2k) = 2*a(k-1) + a(k); a(2k+1) = 2*a(k) + a(k-1). - Michael J. Collins, Dec 25 2018</tt>

  MAPLE

<tt>b:= proc(n) option remember;</tt>

<tt>      `if`(n<2, n, `if`(irem(n, 2)=0, b(n/2), b((n-1)/2) +b((n+1)/2)))</tt>

<tt>    end:</tt>

<tt>a:= proc(n) option remember;</tt>

<tt>      b(n+1) +`if`(n>0, a(n-1), 0)</tt>

<tt>    end:</tt>

<tt>seq(a(n), n=0..70);  # Alois P. Heinz, Jun 21 2012</tt>

  MATHEMATICA

<tt>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 *)</tt>

  CROSSREFS

<tt>A column of A072170.</tt>

<tt>Cf. A002487A007051.</tt>

<tt>Apart from an initial zero, coincides with A174868.</tt>

<tt>Sequence in context: A179509 A157007 A173509 * A174868 A268381 A186349</tt>

<tt>Adjacent sequences:  A007726 A007727 A007728 * A007730 A007731 A007732</tt>

  KEYWORD

<tt>nonn</tt>

  AUTHOR

<tt>N. J. A. Sloane</tt>

  EXTENSIONS

<tt>More terms from Vladeta Jovovic, May 06 2004</tt>

  STATUS

<tt>approved</tt>

细节见代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define rt return
#define dll(x) scanf("%I64d",&x)
#define xll(x) printf("%I64d\n",x)
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define db(x) cout<<"== [ "<<x<<" ] =="<<endl;
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
inline void getInt(int* p);
const int maxn=1000010;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
const ll mod=1e9+7ll;
ll a[100]={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};
map<ll,ll> m;
ll f(ll k)
{
//    cout<<k<<endl;
    if(k<=50)
    {
        return a[k];
    }
    if(m[k])
    {
        return m[k];
    }
    if(k&1)
    {
        return m[k]=(2ll*f(k/2ll)%mod+f(k/2ll-1ll)%mod)%mod;
    }else
    {
        return m[k]=(2ll*f(k/2ll-1ll)%mod+f(k/2ll)%mod)%mod;
    }
}
int main()
{
    //freopen("D:\\common_text\\code_stream\\in.txt","r",stdin);
    //freopen("D:\\common_text\\code_stream\\out.txt","w",stdout);
    ll n;
    cin>>n;
    cout<<f(n)<<endl;

    return 0;
}

inline void getInt(int* p) {
    char ch;
    do {
        ch = getchar();
    } while (ch == ' ' || ch == '\n');
    if (ch == '-') {
        *p = -(getchar() - '0');
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 - ch + '0';
        }
    }
    else {
        *p = ch - '0';
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 + ch - '0';
        }
    }
}