题目链接: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 */