题目链接: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. Java基础:HashMap中putAll方法的疑惑

    最近回顾了下HashMap的源码(JDK1.7),当读到putAll方法时,发现了之前写的TODO标记,当时由于时间匆忙没来得及深究,现在回顾到了就再仔细思考了下 @Override public v ...

  2. Linux基础(Ubuntu16.04):安装vim及配置

    1.进入终端  Ctrl + Alt +T 出现终端窗口 2.输入命令: sudo apt-get install vim-gtk 3.验证是否成功 安装完vim后查看命令 vi tab键,就会关联出 ...

  3. 常用开发环境搭建配置教程(OneStall)

    最近想要做一个小东西,用到了下面几个中间件或者环境: Java Tomcat Maven MongoDB ZooKeeper Node 并且恰好碰到腾讯云打折,云主机原价100多一个月,花了30块钱买 ...

  4. 使用 Lombok 优雅编码

    一.介绍和使用 Lombok 是一个 java 库,能以简单的注解形式来简化 java 代码,提高开发人员的开发效率. 常见使用在开发过程中需要写的 javabean,往往开发需要花时间去添加相应的 ...

  5. LeetCode 176. 第二高的薪水(MySQL版)

    0.前言 最近刷LeetCode 刷数据库题目 由于数据库课上的是SQL,而MySQL有许多自己的函数的,怕把刚学会的函数忘记 特在此记录! 1.题目 编写一个 SQL 查询,获取 Employee ...

  6. 使用String. localeCompare比较字符串

    javascript提供stringA.localeCompare(stringB)方法,来判断一个字符串stringB是否排在stringA的前面. 返回值:    如果引用字符存在于比较字符之前则 ...

  7. Oracle11g: datetime

    --上一月,上一年 select add_months(sysdate,-1) last_month,add_months(sysdate,-12) last_year from dual; --下一 ...

  8. [ SHELL编程 ] 字符串空格和文件空行删除

    1.删除字符串中空格 (1)删除行首空格 (2)删除行尾空格 (3)删除前.后空格,不删除中间空格 (4) 删除字符串中所有空格 echo " 123 567 " | sed 's ...

  9. 面板JPanel,滚动面板JScrollPane,文本域JTextArea

    [面板JPanel] 面板就是一个容器 每一个容器都可以有一个自己的独立的布局和组件,这些容器之间也不会互相干扰 //导入Java类 import javax.swing.*; import java ...

  10. 三、View的事件体系

    1.View基础知识 1.1.什么是View View是Android中所有控件的基类.View是一种界面层的控件的一种抽象,代表了一个控件.除了View,还有ViewGroup,内部包含了许多个控件 ...