题解:
At first let’s simplify the problem. Let’s denote as f(x) function that returns sum of the elements that are on positions from 1 to x inclusive.
How to implement function f(x)?
To find the answer we can find the sum of even and sum of odd numbers and add them. Let’s find how many there are even and odd numbers. Let’s call them cureven and curodd. Iterate through the powers of two. If current power is even, then add minimum from x and current power of two to curodd, otherwise add minimum from x and current power of two to cureven, after that reduce x by current power of two. If x becomes less than or equal to 0 break.
Now our task is reduced to twos: for given number n find the sum of first n even numbers and for given number m find the sum of first m odd numbers. The answer for first task is n⋅(n+1). The answer for second task is m2.
Now the answer for the problem is f®−f(l−1).
Don’t forget about modulo
Complexity is O(logN).
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<iostream>
#include<cstring>
#include<set>
#include<map>
#include<iterator>
#include<queue>
#include<vector>
#include<string>
using namespace std;
typedef long long ll;
const int N=1e6+10;
const long long INF=1e18;
const double eps=0.0000001;
const ll mod=1e9+7;
int a[600][600];
int vis[600][2];
vector<int>cup;
ll f(ll x)
{
ll odd=0,even=0,sum=0,num=1;
while(x-num>=0)
{
x-=num;
if(sum%2==0)
odd=(odd+num)%mod;
else
even=(even+num)%mod;
num*=2;
sum++;
}
if(sum%2==0)
odd=(odd+x)%mod;
else
even=(even+x)%mod;
return ((odd*odd)%mod+(even*even)%mod+even)%mod;
}
int main()
{
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
ll n,m;
cin>>n>>m;
cout<<(f(m)-f(n-1)+mod)%mod;
}