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

Problem for Nazar

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,1,3,5,7,…), and the second set consists of even positive numbers (2,4,6,8,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.

The ten first written numbers: 1,2,4,3,5,7,9,6,8,101,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 lland rr. The answer may be big, so you need to find the remainder of the division by 10000000071000000007 (109+7109+7).

Nazar thought about this problem for a long time, but didn't come up with a solution. Help him solve this problem.

Input

The first line contains two integers ll and rr (1lr10181≤l≤r≤1018) — the range in which you need to find the sum.

Output

Print a single integer — the answer modulo 10000000071000000007 (109+7109+7).

Examples

Input
1 3
Output
7
Input
5 14
Output
105
Input
88005553535 99999999999
Output
761141116

题意:题目给了一个奇数集合以及一个偶数集合,第一次从奇数集合中取一个数,第二次从偶数集合中去两个数,第三次再从奇数集合中取四个数......以此类推。
给我们数字的左右边界l和r,试求区间[l,r]中元素的和是多少。
解题思路: 求区间 l 到 r 的元素等于区间[1,r]的元素和减去[1,l-1]的元素和(前缀和)
     因为数字是按顺序取得, 所以只要求出区间中奇数的个数 以及 偶数的个数用等差数列求和就可以求出区间元素和。
注意:在取余1e+7之后,可能区间[1.r]的元素和小于[1,l-1]的元素和 所以两者做差之后还要再加上1e9+7 在进行取余!!!
 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<iostream>
 4 #include<algorithm>
 5 #include<map>
 6 #include<string>
 7 #include<set>
 8 #include<stack>
 9 #include<queue>
10 using namespace std;
11 const int maxn = 200000;
12 const int mod = 1e9 + 7;
13 typedef long long ll;
14 ll l,r;
15 ll getsum(ll x){
16     ll num1 = 0,num2 = 0;
17     ll l1 = x;
18     ll mm = 1;
19     ll nn = 1;
20     while(l1 > 0){
21         if(l1 >= mm){
22             if(nn % 2 == 1){
23                 num1 += mm;
24             }else{
25                 num2 += mm;
26             }
27             l1 -=mm;
28         }else{
29             if(nn % 2 == 1){
30                 num1 += l1;
31             }else{
32                 num2 += l1;
33             }
34             l1 = 0;
35         }
36         nn++;
37         mm *=2;
38     }
39     ll sum = 0;
40     sum = sum +(((num2 % mod)*((num2 + 1)%mod))%mod +((num1%mod)*(num1%mod))%mod)%mod;
41     return sum%mod;
42 }
43 int main(){
44     scanf("%lld %lld",&l,&r);
45     ll a = getsum(r);
46     ll b = getsum(l - 1);
47     printf("%lld\n",(getsum(r) - getsum(l - 1) + mod)%mod);
48     return 0;
49 }
AC代码

一个从很久以前就开始做的梦。



猜你喜欢

转载自www.cnblogs.com/DreamACMer/p/10838508.html