CodeForces 1151C Problem for Nazar
题目链接:http://codeforces.com/problemset/problem/1151/C
题目大意:
有一个只存奇数的集合A = {1, 3, 5……2*n - 1,……},和只存偶数的集合B = {2, 4, 6……2*n,……},现在要生成一个序列,这个序列分成i次生成:第1次从A中取1个放入序列,第2次从B中取2个放入序列,第3次从A中取4个放入序列,……,第2*n - 1次从A中取22*n-2个放入序列,第2*n次从B中取22*n-1个放入序列……。取得顺序是从小到大取的。题目给定一个区间,就序列在该区间所有数的累加和。
分析:
只要实现一个求区间[1, i]的函数getSum(i),那么任意区间内的和都可以算出,比如区间[l, r]的和为getSum(r) - getSum(l - 1)。
设odd_len为区间[1, i]上奇数的个数,even_len为区间[1, i]上偶数的个数。
只要求出了odd_len和even_len就可以用快速幂求出和了。
下面给出序列1~40的数:

假设x = 37(100101),x落在偶数部分,even_len = 2 + 8 + 6 = 10 + 1000 + 101 + 1 = (11111 & 01010) + (11111 & x) + 1,odd_len = 1 + 4 + 16 = 11111 & 10101
假设x = 25(11001),x落在奇数部分,odd_len = 1 + 4 + 10 = 1 + 100 + 1001 + 1 = (1111 & 0101) + (1111 & x) + 1,even_len = 2 + 8 = 1111 & 1010
规律还是比较好找的,直接文字叙述太麻烦,直接给例子比较容易理解。
代码如下:
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
#define INIT() std::ios::sync_with_stdio(false);std::cin.tie(0);
#define Rep(i,n) for (int i = 0; i < (n); ++i)
#define For(i,s,t) for (int i = (s); i <= (t); ++i)
#define rFor(i,t,s) for (int i = (t); i >= (s); --i)
#define ForLL(i, s, t) for (LL i = LL(s); i <= LL(t); ++i)
#define rForLL(i, t, s) for (LL i = LL(t); i >= LL(s); --i)
#define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)
#define rforeach(i,c) for (__typeof(c.rbegin()) i = c.rbegin(); i != c.rend(); ++i)
#define pr(x) cout << #x << " = " << x << " "
#define prln(x) cout << #x << " = " << x << endl
#define LOWBIT(x) ((x)&(-x))
#define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin())
#define ms0(a) memset(a,0,sizeof(a))
#define msI(a) memset(a,inf,sizeof(a))
#define msM(a) memset(a,-1,sizeof(a))
#define MP make_pair
#define PB push_back
#define ft first
#define sd second
template<typename T1, typename T2>
istream &operator>>(istream &in, pair<T1, T2> &p) {
in >> p.first >> p.second;
return in;
}
template<typename T>
istream &operator>>(istream &in, vector<T> &v) {
for (auto &x: v)
in >> x;
return in;
}
template<typename T1, typename T2>
ostream &operator<<(ostream &out, const std::pair<T1, T2> &p) {
out << "[" << p.first << ", " << p.second << "]" << "\n";
return out;
}
typedef long long LL;
typedef unsigned long long uLL;
typedef pair< double, double > PDD;
typedef pair< int, int > PII;
typedef set< int > SI;
typedef vector< int > VI;
typedef map< int, int > MII;
const double EPS = 1e-;
const int inf = 1e9 + ;
const LL mod = 1e9 + ;
const int maxN = 2e5 + ;
const LL ONE = ;
const LL evenBits = 0xaaaaaaaaaaaaaaaa;
const LL oddBits = 0x5555555555555555;
LL l, r;
LL ans;
// step[i] = 2^i
LL step[];
// Calculate x^y % mod
inline LL pow_mod(LL x, LL y, LL ans = ){
while(y){
ans *= x;
ans %= mod;
--y;
}
return ans;
}
void STEP_INIT() {
step[] = ;
For(i, , ) {
step[i] = pow_mod(, , step[i - ]);
}
}
// 二分计算x的二进制位数
inline int getBits(LL x) {
int cnt = ;
while(x >>= ) ++cnt;
return cnt;
}
// 计算1~x的和
LL getSum(LL x) {
LL ret = ;
if(x == ) return ;
LL len = (LL)getBits(x);
LL odd_len = , even_len = ;
if(len % == ) { // 落在偶数部分
even_len = (~(ONE << (len - ONE)) & x) + (((ONE << (len - ONE)) - ONE) & evenBits) + ;
odd_len = x - even_len;
}
else { // 落在奇数部分
odd_len = (~(ONE << (len - ONE)) & x) + (((ONE << (len - ONE)) - ONE) & oddBits) + ;
even_len = x - odd_len;
}
odd_len %= mod;
even_len %= mod;
// 快速幂
int i = odd_len, j = ;
while(i > ) {
while(i < step[j]) --j;
ret += (odd_len * step[j]) % mod;
ret %= mod;
i -= step[j];
}
i = even_len + ;
j = ;
while(i > ) {
while(i < step[j]) --j;
ret += (even_len * step[j]) % mod;
ret %= mod;
i -= step[j];
}
return ret % mod;
}
int main(){
INIT();
cin >> l >> r;
STEP_INIT();
ans = getSum(r) - getSum(l - );
ans += mod;
ans %= mod;
cout << ans << endl;
return ;
}
/*
1 88005553534
203795384
1 99999999999
964936500
1 35
651
1 32
573
1 22
254
*/
CodeForces 1151C Problem for Nazar的更多相关文章
- Problem for Nazar CodeForces - 1151C (前缀和)
Problem for Nazar Nazar, a student of the scientific lyceum of the Kingdom of Kremland, is known for ...
- Codeforces Round #553 (Div. 2) C. Problem for Nazar 数学
题意:从奇数列 1 3 5 7 9 .... 偶数列2 4 6 8 10...分别轮流取 1 2 4 ....2^n 个数构成新数列 求新数列的区间和 (就一次询问) 思路:首先单次区间和就是一个简 ...
- Codeforces Round #553 (Div. 2) 【C. Problem for Nazar】
题目大意: 一开始第一行是 1,第二行是2 4 ,第三行是3 5 7 9 ,类似这样下去,每一行的个数是上一行的个数,然后对这些点从第一个进行编号,问你从[l,r]区间数的和. 思路:分别求出奇数和偶 ...
- codeforces 854 problem E
E. Boredom Ilya is sitting in a waiting area of Metropolis airport and is bored of looking at time t ...
- codeforces gym100801 Problem G. Graph
传送门:https://codeforces.com/gym/100801 题意: 给你一个DAG图,你最多可以进行k次操作,每次操作可以连一条有向边,问你经过连边操作后最小拓扑序的最大值是多少 题解 ...
- codeforces gym100801 Problem J. Journey to the “The World’s Start”
传送门:https://codeforces.com/gym/100801 题意: 小明坐地铁,现在有n-1种类型的地铁卡卖,现在小明需要买一种地铁票,使得他可以在t的时间内到达终点站,地铁票的属性为 ...
- Codeforces 1188E - Problem from Red Panda(找性质+组合数学)
Codeforces 题面传送门 & 洛谷题面传送门 咦,题解搬运人竟是我? 一道很毒的计数题. 先转化下题意,每一次操作我们可以视作选择一种颜色并将其出现次数 \(+k\),之后将所有颜色的 ...
- CodeForces 688C-NP-Hard Problem
题意: 给你一个无向图,判断是否能够构成一个二分图,如果能的话,输出二分图左边的集合和右边的集合 分析: 先给每一个顶点的color初始化-1,表示没有被染色,用vector数组v[a],表示元素a所 ...
- codeforces #583 problem D(搜索好题)
题目大意:在一个已经有障碍的地图上,设置尽可能少的障碍使得(1,1)无法到达(n,m),行进路线位向下或向右. 数据范围:n*m<=1e6 解题思路:答案一定是小于等于2的,因为可以直接阻碍(1 ...
随机推荐
- [Redis]Redis的设计与实现-链表/字典/跳跃表
redis的设计与实现:1.假如有一个用户关系模块,要实现一个共同关注功能,计算出两个用户关注了哪些相同的用户,本质上是计算两个用户关注集合的交集,如果使用关系数据库,需要对两个数据表执行join操作 ...
- Java Calendar类的使用总结
在实际项目当中,我们经常会涉及到对时间的处理,例如登陆网站,我们会看到网站首页显示XXX,欢迎您!今天是XXXX年....某些网站会记录下用户登陆的时间,比如银行的一些网站,对于这些经常需要处理的问题 ...
- 如何用java控制你的电脑?
用java控制你的电脑 java,是一门强大的语言,强大的地方在于有很多类,我们可以直接的使用.而java.awt.Robot就很有意思了,顾名思义robot机器人,怎样一个机器法,如:控制鼠标方法: ...
- ArcGIS 网络分析[1] 利用自定义点线数据(shp或数据库)创建网络数据集【小白向】
前言 似乎除了官方介绍的例子,我还没有在网上见过一篇介绍如何“使用自己的数据”创建“网络数据集”的文章. 究其原因,是因为当前的高质量的线数据或保密,或采集困难. 有介绍几何网络的,有介绍如何用官方S ...
- SQLServer修改表数据
使用SSMS数据库管理工具修改数据 修改任意一条或者多条都可以 1:打开数据库,选择数据表,右键点击->编辑所有行(如未配置,点击编辑前200行). 2.编辑需要修改的数据->编辑完成后, ...
- Gitlab利用Webhook实现Push代码后的jenkins自动构建
之前部署了Gitlab的代码托管平台和Jenkins的代码发布平台.通常是开发后的代码先推到Gitlab上管理,然后在Jenkins里通过脚本构建代码发布.这种方式每次在发版的时候,需要人工去执行je ...
- 【导航】Python常用资源(从新手到大牛)
[博客导航] [Python相关] 个人网站/博客/学习平台 中国大学MOOC :高校课程网上公开课学习平台,<Python语言程序设计>是我的入门课. Python123平台 :跟中国 ...
- Java学习笔记记录(一)
1.Java编写的一个基本结构 1 public class demo{ //以下包含权限修饰符.静态修饰符.返回值修饰符以及主方法main() 2 public static void main(S ...
- China Tightens Recycling Import Rules
China Tightens Recycling Import Rules We have all seen the pictures of cities in China with air poll ...
- 打印进度条>>>>
i+1: 当前的数量 300: 总数量 import sys print("下载中...") def process(curr, count): cursor_count = c ...