题目链接: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的更多相关文章

  1. Problem for Nazar CodeForces - 1151C (前缀和)

    Problem for Nazar Nazar, a student of the scientific lyceum of the Kingdom of Kremland, is known for ...

  2. Codeforces Round #553 (Div. 2) C. Problem for Nazar 数学

    题意:从奇数列 1 3 5 7 9 ....  偶数列2 4 6 8 10...分别轮流取 1 2 4 ....2^n 个数构成新数列 求新数列的区间和 (就一次询问) 思路:首先单次区间和就是一个简 ...

  3. Codeforces Round #553 (Div. 2) 【C. Problem for Nazar】

    题目大意: 一开始第一行是 1,第二行是2 4 ,第三行是3 5 7 9 ,类似这样下去,每一行的个数是上一行的个数,然后对这些点从第一个进行编号,问你从[l,r]区间数的和. 思路:分别求出奇数和偶 ...

  4. codeforces 854 problem E

    E. Boredom Ilya is sitting in a waiting area of Metropolis airport and is bored of looking at time t ...

  5. codeforces gym100801 Problem G. Graph

    传送门:https://codeforces.com/gym/100801 题意: 给你一个DAG图,你最多可以进行k次操作,每次操作可以连一条有向边,问你经过连边操作后最小拓扑序的最大值是多少 题解 ...

  6. codeforces gym100801 Problem J. Journey to the “The World’s Start”

    传送门:https://codeforces.com/gym/100801 题意: 小明坐地铁,现在有n-1种类型的地铁卡卖,现在小明需要买一种地铁票,使得他可以在t的时间内到达终点站,地铁票的属性为 ...

  7. Codeforces 1188E - Problem from Red Panda(找性质+组合数学)

    Codeforces 题面传送门 & 洛谷题面传送门 咦,题解搬运人竟是我? 一道很毒的计数题. 先转化下题意,每一次操作我们可以视作选择一种颜色并将其出现次数 \(+k\),之后将所有颜色的 ...

  8. CodeForces 688C-NP-Hard Problem

    题意: 给你一个无向图,判断是否能够构成一个二分图,如果能的话,输出二分图左边的集合和右边的集合 分析: 先给每一个顶点的color初始化-1,表示没有被染色,用vector数组v[a],表示元素a所 ...

  9. codeforces #583 problem D(搜索好题)

    题目大意:在一个已经有障碍的地图上,设置尽可能少的障碍使得(1,1)无法到达(n,m),行进路线位向下或向右. 数据范围:n*m<=1e6 解题思路:答案一定是小于等于2的,因为可以直接阻碍(1 ...

随机推荐

  1. 【EF】CodeFirst Fluent API使用记录

    我们在使用EF CodeFirst 模式生成数据库的时候进行表的代码映射关系可以采用注解模式和Fluent API模式.这里就是记录一下使用Fluent API进行表关系映射的方法. 注解模式: 回顾 ...

  2. Web前端:博客美化:三、右上角的Github Ribbon

    1.保存图片到博客园相册 2.复制代码到可以放html代码的地方 我因为数量问题把这段sei到了 页首Html代码 <a href="https://github.com/zhengw ...

  3. #WEB安全基础 : HTML/CSS | 0x8CSS进阶

    你以为自己学这么点CSS就厉害了? 学点新东西吧,让你的网页更漂亮 我们只需要用到图片和网页   这是index.html的代码 <html> <head> <title ...

  4. Android人脸识别App(带web上传注册信息)

    人脸识别+本机Web后端人脸sdk采用虹软sdk,本机web采用AndServer:上传姓名+人脸图片即可实现注册源码地址:https://github.com/joetang1989/ArcFace ...

  5. MongoDB副本集及C#程序的连接配置

    1.副本集 高可用是绝大多数数据库管理系统的核心目标之一.如果要想生产数据在发生故障后依然可用,就需要确保为生产数据库多部署一台服务器.MongoDB副本集提供了数据的保护.高可用和灾难恢复的机制. ...

  6. Windows Server 2012 NIC Teaming 网卡绑定介绍及注意事项

    Windows Server 2012 NIC Teaming 网卡绑定介绍及注意事项 转载自:http://www.it165.net/os/html/201303/4799.html Window ...

  7. ASP.NET Zero--后端应用程序

    后端应用程序 这是用户名和密码输入的实际应用程序.您将主要在此应用程序上添加您的业务需求. 应用文件夹 后端应用程序默认内置在专用区域,名为“ App ”,但可以在创建解决方案时确定.因此,所有控制器 ...

  8. 图解slub

    1.前言 在Linux中,伙伴系统(buddy system)是以页为单位管理和分配内存.但是现实的需求却以字节为单位,假如我们需要申请20Bytes,总不能分配一页吧!那岂不是严重浪费内存.那么该如 ...

  9. C 存储类

    存储类定义 C 程序中变量/函数的范围(可见性)和生命周期.这些说明符放置在它们所修饰的类型之前.下面列出 C 程序中可用的存储类: auto.register.static.extern auto ...

  10. #018 C语言刷题 素数问题

    今天做题学会了一个求素数的方法 总分 13 孪生素数 相差为2的两个素数称为孪生素数.例如,3与5,41与43等都是孪生素数.设计程序求出指定区间上的所有孪生素数对.区间上限和下限由键盘获取. 程序运 ...