ARC 066D Xor Sum AtCoder - 2272 (打表找规律)
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:
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
|
|||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||
|
细节见代码:
#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=;while(b){if(b%)ans=ans*a%MOD;a=a*a%MOD;b/=;}return ans;}
inline void getInt(int* p);
const int maxn=;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
const ll mod=1e9+7ll;
ll a[]={, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , };
map<ll,ll> m;
ll f(ll k)
{
// cout<<k<<endl;
if(k<=)
{
return a[k];
}
if(m[k])
{
return m[k];
}
if(k&)
{
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 ;
} inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '');
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * - ch + '';
}
}
else {
*p = ch - '';
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * + ch - '';
}
}
}
ARC 066D Xor Sum AtCoder - 2272 (打表找规律)的更多相关文章
- 【找规律】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 ...
- HDU 4861 Couple doubi (数论 or 打表找规律)
Couple doubi 题目链接: http://acm.hust.edu.cn/vjudge/contest/121334#problem/D Description DouBiXp has a ...
- 【ZOJ】3785 What day is that day? ——浅谈KMP在ACM竞赛中的暴力打表找规律中的应用
转载请声明出处:http://www.cnblogs.com/kevince/p/3887827.html ——By Kevince 首先声明一下,这里的规律指的是循环,即找到最小循环周期. 这 ...
- HDU 3032 (SG打表找规律)
题意: 有n堆石子,alice先取,每次可以选择拿走一堆石子中的1~x(该堆石子总数) ,也可以选择将这堆石子分成任意的两堆.alice与bob轮流取,取走最后一个石子的人胜利. 思路: 因为数的范围 ...
- OpenJ_POJ C16D Extracurricular Sports 打表找规律
Extracurricular Sports 题目连接: http://acm.hust.edu.cn/vjudge/contest/122701#problem/D Description As w ...
- 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 ...
- 【ZOJ】3785 What day is that day? ——KMP 暴力打表找规律
转自:http://www.cnblogs.com/kevince/p/3887827.html 首先声明一下,这里的规律指的是循环,即找到最小循环周期. 这么一说大家心里肯定有数了吧,“不就是nex ...
- [国家集训队]整数的lqp拆分 数学推导 打表找规律
题解: 考场上靠打表找规律切的题,不过严谨的数学推导才是本题精妙所在:求:$\sum\prod_{i=1}^{m}F_{a{i}}$ 设 $f(i)$ 为 $N=i$ 时的答案,$F_{i}$ 为斐波 ...
- CF R 633 div 1 1338 C. Perfect Triples 打表找规律
LINK:Perfect Triples 初看这道题 一脸懵逼.. 完全没有思路 最多就只是发现一点小规律 即. a<b<c. 且b的最大的二进制位一定严格大于a b的最大二进制位一定等于 ...
随机推荐
- Sql Server XML
实验数据: Create table xmldata (name NVARCHAR(20), age int, sex NVARCHAR(5) ) INSERT INTO xmldata VALUES ...
- 修改Windows默认远程端口号
1.定位注册表,[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal Server\Wds\rdpwd\Tds\tcp],右侧修改 ...
- MySql基本使用方法
一,基本概念 1, 常用的两种引擎: (1) InnoDB a,支持ACID,简单地说就是支持事务完整性.一致性: b,支持行锁,以及类似ORACLE的一 ...
- python3编写网络爬虫17-验证码识别
一.验证码识别 1.图形验证码的识别 识别图形验证码需要 tesserocr 库 OCR技术识别(光学字符识别,是指通过扫描字符,然后通过其形状将其翻译成电子文本的过程.)例如 中国知网注册页面 ht ...
- Python虚拟环境笔记
虚拟环境 为什么需要虚拟环境: 到目前位置,我们所有的第三方包安装都是直接通过pip install xx的方式进行安装的,这样安装会将那个包安装到你的系统级的Python环境中.但是这样有一个问题, ...
- C. Meaningless Operations Codeforces Global Round 1 异或与运算,思维题
C. Meaningless Operations time limit per test 1 second memory limit per test 256 megabytes input sta ...
- Java面试知识点之计算机网络篇(一)
前言:在Java面试中,计算机网络的知识也是一项重点,因此笔者在此对计算机网络的相关知识进行总结. 1.OSI参考模型 自下而上:物理层(物理介质,比特流).数据链路层(网卡.交换机).网络层(IP协 ...
- 设计模式のPrototypePattern(原型模式)----创建模式
一.产生的背景 这种模式是实现了一个原型接口,该接口用于创建当前对象的克隆.当直接创建对象的代价比较大时,则采用这种模式.例如,一个对象需要在一个高代价的数据库操作之后被创建.我们可以缓存该对象,在下 ...
- B树索引分裂
一.索引分裂 1. 什么是分裂 在开始介绍之前,我们先来搞清楚什么是索引分裂吧.“索引分裂”就是索引块的分裂,当一次DML事务操作修改了索引块上的数据,但是旧有的索引块没有足够的空间来容纳新修改的数 ...
- JDK动态代理给Spring事务埋下的坑!
一.场景分析 最近做项目遇到了一个很奇怪的问题,大致的业务场景是这样的:我们首先设定两个事务,事务parent和事务child,在Controller里边同时调用这两个方法,示例代码如下: 1.场景A ...