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 (1≤l≤r≤10181≤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 在进行取余!!!
 #include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<map>
#include<string>
#include<set>
#include<stack>
#include<queue>
using namespace std;
const int maxn = ;
const int mod = 1e9 + ;
typedef long long ll;
ll l,r;
ll getsum(ll x){
ll num1 = ,num2 = ;
ll l1 = x;
ll mm = ;
ll nn = ;
while(l1 > ){
if(l1 >= mm){
if(nn % == ){
num1 += mm;
}else{
num2 += mm;
}
l1 -=mm;
}else{
if(nn % == ){
num1 += l1;
}else{
num2 += l1;
}
l1 = ;
}
nn++;
mm *=;
}
ll sum = ;
sum = sum +(((num2 % mod)*((num2 + )%mod))%mod +((num1%mod)*(num1%mod))%mod)%mod;
return sum%mod;
}
int main(){
scanf("%lld %lld",&l,&r);
ll a = getsum(r);
ll b = getsum(l - );
printf("%lld\n",(getsum(r) - getsum(l - ) + mod)%mod);
return ;
}

AC代码

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


最新文章

  1. http协议get、post请求分析及用HttpRequester测试的报错及可能原因
  2. Sound Generator 原理
  3. Android Studio在导入eclipse的项目时一直卡在gradle:Configure project
  4. react入门笔记
  5. 10款Windows命令行工具
  6. IntelliJ IDEA 15激活
  7. Java应用短信猫
  8. MonkeyRunner 连续两次点击报“Error sending touch event”
  9. Delphi Socket的最好项目——FastMsg IM(还有一些IM控件),RTC,RO,Sparkle等等,FileZilla Client/Server,wireshark,NSClient
  10. C# winform Datagridview 标题居中
  11. BFS-九宫格重排(详解)
  12. Android 6.0 默认关闭定位和GPS,开启后默认选省电
  13. luogu P4515 [COCI2009-2010#6] XOR
  14. mysql之mysqldump——备份与还原
  15. nodejs开篇基础&lt;①&gt;
  16. Java Netty (2)
  17. LHC大神问的矩阵转置问题
  18. Python的isdigit()和isalpha()
  19. Java注解Annotation(一)
  20. Connecting to a Remote Serial Port over TCP/IP

热门文章

  1. 068-PHP定义并输出数组
  2. (22)Canny算法
  3. 心形java和C语言2019/10/17
  4. Java算法练习——最长公共前缀
  5. js 月份选择器(只选择到月)
  6. python复习——数据输入输出
  7. 一名资深架构师规划Java程序员五年职业生涯指南
  8. 2020/2/2 PHP代码审计之反序列化
  9. windows FTP上传
  10. Java四类八种