C. 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 ll and 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

Copy

1 3

output

Copy

7

input

Copy

5 14

output

Copy

105

input

Copy

88005553535 99999999999

output

Copy

761141116

Note

In the first example, the answer is the sum of the first three numbers written out (1+2+4=71+2+4=7).

In the second example, the numbers with numbers from 55 to 1414: 5,7,9,6,8,10,12,14,16,185,7,9,6,8,10,12,14,16,18. Their sum is 105105.

题解:题目很好理解,就是要找L,R区间和。我们要知道对于奇数数列的前n项和为n^2,偶数数列的前n项和为n*(n+1).则根据题目的奇偶排列顺序对于奇偶分别加值即可,题目的数据比较大,要加个快速乘比较好。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod=1e9+7;
ll quickmul(ll a,ll b)//b个a相乘,即a*b
{
    ll ans=0;
    while(b)
    {
        if(b&1)
            ans=(ans+a)%mod;
        a=(a+a)%mod;
        b>>=1;
    }
    return ans;
}

ll sum(ll x)
{
    ll num=0,even=0,odd=0;
    while(x>0)
    {
        if(num%2)
            even+=min(x, (ll)1<<num);
        else
            odd+=min(x, (ll)1<<num);
        x-=(ll)1<<num;
        num++;
    }
    return (quickmul(odd,odd)+quickmul(even,even+1))%mod;
}
int main()
{
    ll L,R;
    cin>>L>>R;
    cout<<(sum(R)-sum(L-1)+mod)%mod<<endl;
    return 0;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值