Codeforces Round #553 (Div. 2) C
Nazar, a student of the scientific lyceum of the Kingdom of Kremland, is known for his outstanding mathematical abilities. Today a math teacher gave him a very difficult task.
Consider two infinite sets of numbers. The first set consists of odd positive numbers (1,3,5,7,…), and the second set consists of even positive numbers (2,4,6,8,…). At the first stage, the teacher writes the first number on the endless blackboard from the first set, in the second stage — the first two numbers from the second set, on the third stage — the next four numbers from the first set, on the fourth — the next eight numbers from the second set and so on. In other words, at each stage, starting from the second, he writes out two times more numbers than at the previous one, and also changes the set from which these numbers are written out to another.
SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。The ten first written numbers: 1,2,4,3,5,7,9,6,8,10. Let's number the numbers written, starting with one.
The task is to find the sum of numbers with numbers from ll to rr for given integers ll and rr. The answer may be big, so you need to find the remainder of the division by 1000000007 (109+7).
Nazar thought about this problem for a long time, but didn't come up with a solution. Help him solve this problem.
InputThe first line contains two integers ll and rr (1≤l≤r≤1018) — the range in which you need to find the sum.
OutputPrint a single integer — the answer modulo 1000000007 (109+7).
Examples input1 3output
7input
5 14output
105input
88005553535 99999999999output
761141116Note
In the first example, the answer is the sum of the first three numbers written out (1+2+4=7).
In the second example, the numbers with numbers from 5 to 14: 5,7,9,6,8,10,12,14,16,18. Their sum is 105.
链接: http://codeforces.com/contest/1151/problem/C
赛后补题。
题意:给你一个无穷的数列 数列的第一部分长度为 1 ,由(1)组成、第二部分长度是2,由 (2 4) 组成、第三部分长度为4 由(3 5 7 9)组成,接下来的还是同样的规律:长度*2 奇偶互换。 求[l,r]这个区间的区间和。
常规的区间操作 【l,r】 = 【1,r】- 【1,l-1】

#include<cstdio> #include<algorithm> using namespace std; #define ll long long const ll mod = 1e9+7; ll solve(ll x){ ll cnt[2] = {0,0}; int k = 0;// 记录奇偶位 ll c = 1; // 记录区间的长度 while(x){ cnt[k] += min(c,x); // 当最后的那个区间长度小于 2^(n-1) (n是区间的个数) 时 // 才会 cnt[k] += x; x -= min(c,x); k^=1; c <<= 1; } ll sum = (cnt[0]%mod)*(cnt[0]%mod)%mod; sum += (cnt[1]%mod)*((cnt[1]+1)%mod)%mod; return sum%mod; } int main(){ ll l, r; scanf("%I64d%I64d",&l,&r); printf("%I64d\n",(solve(r)-solve(l-1)+mod)%mod); return 0; }View Code
