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

  • 1≦N≦10^{18}

Input

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

  1. N

Output

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

Sample Input 1

  1. 3

Sample Output 1

  1. 5

The five possible pairs of u and v are:

  • u=0,v=0 (Let a=0,b=0, then 0 xor 0=00+0=0.)

  • u=0,v=2 (Let a=1,b=1, then 1 xor 1=01+1=2.)

  • u=1,v=1 (Let a=1,b=0, then 1 xor 0=11+0=1.)

  • u=2,v=2 (Let a=2,b=0, then 2 xor 0=22+0=2.)

  • u=3,v=3 (Let a=3,b=0, then 3 xor 0=33+0=3.)

Sample Input 2

  1. 1422

Sample Output 2

  1. 52277

Sample Input 3

  1. 1000000000000000000

Sample Output 3

  1. 787014179
  2.  
  3. 思路:
    打出前面一些项的答案:
    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 (listgraphrefslistenhistorytextinternal 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. A002487A007051.

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

细节见代码:

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <algorithm>
  5. #include <cmath>
  6. #include <queue>
  7. #include <stack>
  8. #include <map>
  9. #include <set>
  10. #include <vector>
  11. #include <iomanip>
  12. #define ALL(x) (x).begin(), (x).end()
  13. #define rt return
  14. #define dll(x) scanf("%I64d",&x)
  15. #define xll(x) printf("%I64d\n",x)
  16. #define sz(a) int(a.size())
  17. #define all(a) a.begin(), a.end()
  18. #define rep(i,x,n) for(int i=x;i<n;i++)
  19. #define repd(i,x,n) for(int i=x;i<=n;i++)
  20. #define pii pair<int,int>
  21. #define pll pair<long long ,long long>
  22. #define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
  23. #define MS0(X) memset((X), 0, sizeof((X)))
  24. #define MSC0(X) memset((X), '\0', sizeof((X)))
  25. #define pb push_back
  26. #define mp make_pair
  27. #define fi first
  28. #define se second
  29. #define eps 1e-6
  30. #define gg(x) getInt(&x)
  31. #define db(x) cout<<"== [ "<<x<<" ] =="<<endl;
  32. using namespace std;
  33. typedef long long ll;
  34. ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
  35. ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
  36. ll powmod(ll a,ll b,ll MOD){ll ans=;while(b){if(b%)ans=ans*a%MOD;a=a*a%MOD;b/=;}return ans;}
  37. inline void getInt(int* p);
  38. const int maxn=;
  39. const int inf=0x3f3f3f3f;
  40. /*** TEMPLATE CODE * * STARTS HERE ***/
  41. const ll mod=1e9+7ll;
  42. ll a[]={, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , };
  43. map<ll,ll> m;
  44. ll f(ll k)
  45. {
  46. // cout<<k<<endl;
  47. if(k<=)
  48. {
  49. return a[k];
  50. }
  51. if(m[k])
  52. {
  53. return m[k];
  54. }
  55. if(k&)
  56. {
  57. return m[k]=(2ll*f(k/2ll)%mod+f(k/2ll-1ll)%mod)%mod;
  58. }else
  59. {
  60. return m[k]=(2ll*f(k/2ll-1ll)%mod+f(k/2ll)%mod)%mod;
  61. }
  62. }
  63. int main()
  64. {
  65. //freopen("D:\\common_text\\code_stream\\in.txt","r",stdin);
  66. //freopen("D:\\common_text\\code_stream\\out.txt","w",stdout);
  67. ll n;
  68. cin>>n;
  69. cout<<f(n)<<endl;
  70.  
  71. return ;
  72. }
  73.  
  74. inline void getInt(int* p) {
  75. char ch;
  76. do {
  77. ch = getchar();
  78. } while (ch == ' ' || ch == '\n');
  79. if (ch == '-') {
  80. *p = -(getchar() - '');
  81. while ((ch = getchar()) >= '' && ch <= '') {
  82. *p = *p * - ch + '';
  83. }
  84. }
  85. else {
  86. *p = ch - '';
  87. while ((ch = getchar()) >= '' && ch <= '') {
  88. *p = *p * + ch - '';
  89. }
  90. }
  91. }
  1.  

ARC 066D Xor Sum AtCoder - 2272 (打表找规律)的更多相关文章

  1. 【找规律】ARC 066D Xor Sum AtCoder - 2272

    题目大意 给出一个整数\(n\),已知\(0\le u,v\le n\),求满足\(a\ xor\ b=u\)且\(a+b=v\)的\(a.b\)对数 样例1输入 3 样例1输出 5 /* u=0,v ...

  2. HDU 4861 Couple doubi (数论 or 打表找规律)

    Couple doubi 题目链接: http://acm.hust.edu.cn/vjudge/contest/121334#problem/D Description DouBiXp has a ...

  3. 【ZOJ】3785 What day is that day? ——浅谈KMP在ACM竞赛中的暴力打表找规律中的应用

    转载请声明出处:http://www.cnblogs.com/kevince/p/3887827.html    ——By Kevince 首先声明一下,这里的规律指的是循环,即找到最小循环周期. 这 ...

  4. HDU 3032 (SG打表找规律)

    题意: 有n堆石子,alice先取,每次可以选择拿走一堆石子中的1~x(该堆石子总数) ,也可以选择将这堆石子分成任意的两堆.alice与bob轮流取,取走最后一个石子的人胜利. 思路: 因为数的范围 ...

  5. OpenJ_POJ C16D Extracurricular Sports 打表找规律

    Extracurricular Sports 题目连接: http://acm.hust.edu.cn/vjudge/contest/122701#problem/D Description As w ...

  6. Codeforces Round #493 (Div. 2)D. Roman Digits 第一道打表找规律题目

    D. Roman Digits time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...

  7. 【ZOJ】3785 What day is that day? ——KMP 暴力打表找规律

    转自:http://www.cnblogs.com/kevince/p/3887827.html 首先声明一下,这里的规律指的是循环,即找到最小循环周期. 这么一说大家心里肯定有数了吧,“不就是nex ...

  8. [国家集训队]整数的lqp拆分 数学推导 打表找规律

    题解: 考场上靠打表找规律切的题,不过严谨的数学推导才是本题精妙所在:求:$\sum\prod_{i=1}^{m}F_{a{i}}$ 设 $f(i)$ 为 $N=i$ 时的答案,$F_{i}$ 为斐波 ...

  9. CF R 633 div 1 1338 C. Perfect Triples 打表找规律

    LINK:Perfect Triples 初看这道题 一脸懵逼.. 完全没有思路 最多就只是发现一点小规律 即. a<b<c. 且b的最大的二进制位一定严格大于a b的最大二进制位一定等于 ...

随机推荐

  1. win7 中 sql server2005 卸载简介

    注:卸载前一定要做好备份,一定要清理干净,不然重装会出错(只针对完全卸载,没试过只删除一个版本的) 工具:①Windows Install Clean Up  ②SrvInstw.exe 1.停止所有 ...

  2. CentOS更换源

    这里介绍如何把CentOS默认镜像源更换为阿里云镜像源 1.备份 mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.r ...

  3. maven与jdk版本对应关系

    Maven发布历史 发布日期 版 必需的Java版本 链接 2018年6月21日 3.5.4 Java 7 宣布,发布说明,参考文档 2018年3月8日 3.5.3 宣布,发布说明,参考文档 2017 ...

  4. MySQL注入与防御

    1.简介 1.1.含义 在一个应用中,数据的安全无疑是最重要的.数据的最终归宿都是数据库,因此如何保证数据库不被恶意攻击者入侵是一项重要且严肃的问题! SQL注入作为一种很流行的攻击手段,一直以来都受 ...

  5. LeetCode算法题-Plus One(Java实现)

    这是悦乐书的第156次更新,第158篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第15题(顺位题号是66).给定一个非空数字数组来表示一个非负整数,并给其加1.该数组已 ...

  6. 【微信小游戏】【提审的坑】!#¥%&……&&……%¥#@@*()()&%%¥

    一.开通了虚拟支付后审核变慢 前两个版本是没有开通虚拟支付的,最快一个半小时就过审了.当时还在暗自嘲笑WX,条款很多,审核却那么松,甚至一度怀疑是不是没有审核直接放.然而第三版提审了之后,一个小时.两 ...

  7. 《生命》第三集:Mammals (哺乳动物)

    南极零下四十度的情况下,威德尔海豹能深潜到冰下捕食,并且教自己的小宝宝如何下水,看了这个才知道,海豹居然是哺乳动物,小海豹看着挺萌的. 长鼻鼩是一种很活跃的生物,而且会自己设计路线,建立迷宫,帮助自己 ...

  8. 【Python语言】Python介绍

    目前在大数据的行业中有3种语言:1. Java ---> 用于大数据工程2. Scala ---> 用于大数据工程和数据科学3.Python ---> 用于数据科学 Python是一 ...

  9. SQL Alias(别名)

    通过使用 SQL,可以为列名称和表名称指定别名(Alias). SQL Alias 表的 SQL Alias 语法 SELECT column_name(s) FROM table_name AS a ...

  10. 并发控制--Concurrency control--乐观、悲观及方法

    In information technology and computer science, especially in the fields of computer programming, op ...