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. 1 3
Output
  1. 7
Input
  1. 5 14
Output
  1. 105
Input
  1. 88005553535 99999999999
Output
  1. 761141116
  2.  
  3. 题意:题目给了一个奇数集合以及一个偶数集合,第一次从奇数集合中取一个数,第二次从偶数集合中去两个数,第三次再从奇数集合中取四个数......以此类推。
    给我们数字的左右边界lr,试求区间[lr]中元素的和是多少。
    解题思路: 求区间 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 = ;
  12. const int mod = 1e9 + ;
  13. typedef long long ll;
  14. ll l,r;
  15. ll getsum(ll x){
  16. ll num1 = ,num2 = ;
  17. ll l1 = x;
  18. ll mm = ;
  19. ll nn = ;
  20. while(l1 > ){
  21. if(l1 >= mm){
  22. if(nn % == ){
  23. num1 += mm;
  24. }else{
  25. num2 += mm;
  26. }
  27. l1 -=mm;
  28. }else{
  29. if(nn % == ){
  30. num1 += l1;
  31. }else{
  32. num2 += l1;
  33. }
  34. l1 = ;
  35. }
  36. nn++;
  37. mm *=;
  38. }
  39. ll sum = ;
  40. sum = sum +(((num2 % mod)*((num2 + )%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 - );
  47. printf("%lld\n",(getsum(r) - getsum(l - ) + mod)%mod);
  48. return ;
  49. }

AC代码

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

  1.  

Problem for Nazar CodeForces - 1151C (前缀和)的更多相关文章

  1. CodeForces 1151C Problem for Nazar

    题目链接:http://codeforces.com/problemset/problem/1151/C 题目大意: 有一个只存奇数的集合A = {1, 3, 5……2*n - 1,……},和只存偶数 ...

  2. Codeforces Round #427 (Div. 2) Problem C Star sky (Codeforces 835C) - 前缀和

    The Cartesian coordinate system is set in the sky. There you can see n stars, the i-th has coordinat ...

  3. Codeforces Round #553 (Div. 2) C. Problem for Nazar 数学

    题意:从奇数列 1 3 5 7 9 ....  偶数列2 4 6 8 10...分别轮流取 1 2 4 ....2^n 个数构成新数列 求新数列的区间和 (就一次询问) 思路:首先单次区间和就是一个简 ...

  4. Codeforces Round #553 (Div. 2) 【C. Problem for Nazar】

    题目大意: 一开始第一行是 1,第二行是2 4 ,第三行是3 5 7 9 ,类似这样下去,每一行的个数是上一行的个数,然后对这些点从第一个进行编号,问你从[l,r]区间数的和. 思路:分别求出奇数和偶 ...

  5. Codeforces 612D 前缀和处理区间问题

    传送门:http://codeforces.com/problemset/problem/612/D (转载请注明出处谢谢) 题意: 给出数字n和k,n表示接下来将输入n个在x轴上的闭区间[li,ri ...

  6. TopCoder SRM 560 Div 1 - Problem 1000 BoundedOptimization & Codeforces 839 E

    传送门:https://284914869.github.io/AEoj/560.html 题目简述: 定义"项"为两个不同变量相乘. 求一个由多个不同"项"相 ...

  7. A Boring Problem UVALive - 7676 (二项式定理+前缀和)

    题目链接: I - A Boring Problem UVALive - 7676 题目大意:就是求给定的式子. 学习的网址:https://blog.csdn.net/weixin_37517391 ...

  8. CodeForces 816B 前缀和

    To stay woke and attentive during classes, Karen needs some coffee! Karen, a coffee aficionado, want ...

  9. Dasha and Photos CodeForces - 761F (前缀优化)

    大意: 给定n*m初始字符矩阵, 有k个新矩阵, 第$i$个矩阵是由初始矩阵区间赋值得到的, 求选择一个新矩阵, 使得其余新矩阵到它距离和最小. 字符集比较小, 可以考虑每次区间覆盖对每个字符的贡献. ...

随机推荐

  1. 156-PHP strrpos和strripos函数

    <?php //定义两个字符串 $str='pasSword'; $position=strrpos($str,'s'); //不区分大小写判断 echo "字母S在{$str}中最后 ...

  2. 手动搭建简单的vue项目

    创建项目根目录 切换到根目录下 , 并执行 npm init , 所有选项都默认即可. 安装 webpack webpack-cli vue vue-loader 添加项目结构         

  3. 线段树&树状数组与离散化的妙用

    牛客2019多校联盟Day7 Fine the median 题意:  每次给数组插入区间[Li,Ri] 内的所有数,每操作一次查询中位数. 遇到这题真的算是巧合,然而就是这种冥冥之中的缘分,给了我线 ...

  4. 使用jackson转换类型时报Unrecognized field

    调用 objectMapper.convertValue(obj, valueType ); 时报错 原因 obj 的属性多于 valueType 导致,添加一条语句即可 objectMapper.c ...

  5. Node.js的启动和调试方式

    通过node命令启动 node server/bin/www webstorm配置启动入口 pm2 全局安装:cnpm i pm2 -g 检查版本:pm2 -v 启动:cd 项目目录 pm2 star ...

  6. 为什么vue中的data用return返回

    1.为什么在项目中data需要使用return返回数据呢? 不使用return包裹的数据会在项目的全局可见,会造成变量污染:使用return包裹后数据中变量只在当前组件中生效,不会影响其他组件. 当一 ...

  7. POJ - 1321 棋盘问题(简单搜索)

    题意:在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子的所有可行的摆放方 ...

  8. ffmpeg 模块简介

    FFmpeg 是一套可以用来记录.转换数字音频.视频,并能将其转化为流的开源计算机程序.它包括了领先的音/视频编码库libavcodec 等.libavformat:用于各种音视频封装格式的生成和解析 ...

  9. Arduino --structure

    The elements of Arduino (C++) code. Sketch loop() setup() Control Structure break continue do...whil ...

  10. UVALive 3977 BFS染色

    这个题意搞了半天才搞明白 就是如果定义一个d-summit,即从该点到另一个更高的点,经过的路径必定是比当前点低至少d高度的,如果该点是最高点,没有比他更高的,就直接视为顶点 其实就是个BFS染色,先 ...