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;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章