time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
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.
题意:一个数列1 2 4 3 5 7 9 6 8 10 12 14 16 18 20 .一个1,两个偶数2 4 ,四个奇数3 5 7 9 , 8 个偶数8 10 12 14 16 18 20。
以此类推 给出l r 求区间内的和。
做法:预处理每次增加的次数 奇数时 的前缀和a,偶数时的前缀和b,总的前缀和c都跟2的幂次方有关。
然后每次找出 r在数列位置中,有多少个偶数,多少个奇数。两个等差数列求和即可
自己做出来就很开心~~~
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=(b);++i)
#define rep2(i,a,b) for(int i=a;i<(b);++i)
#define per(i,a,b) for(int i=a;i>=(b);--i)
using namespace std;
typedef long long ll;
const ll mod=1e9+7;
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
ll powmod(ll a,ll b) {ll res=1;a%=mod;
for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
const int N=100;
ll a[N],b[N],c[N];
ll l,r;
ll cal(ll l)
{
if(l==0) return 0;
ll l1=0,l2=0;
if(l==c[1]) l1=1,l2=0;
else{
rep(i,1,64)
{
if(c[i]<l&&l<=c[i+1])//找到第一个大于l的位置
{
if(i%2==0) //奇数有多余 偶数刚好等于偶数的前缀和
{
l1=a[i]+l-c[i];
l2=b[i];
}
else//偶数有多余 奇数刚好等于奇数的前缀和
{
l1=a[i];
l2=b[i]+l-c[i];
}
break;
}
}
}
l1%=mod; //a1=1 d=2,len=l1的等差数列
l2%=mod; //a1=2 d=2,len=l2的等差数列
return (l1*l1%mod+l2*(l2+1)%mod)%mod;
}
int main()
{
cin>>l>>r;
for(ll j=0;j<64;j++)
{
int i=j+1;
if(i%2==1) a[i]=a[i-1]+(1ll<<j),b[i]=b[i-1];//奇数每次增加的数量
else b[i]=b[i-1]+(1ll<<j),a[i]=a[i-1];//偶数每次增加的数量
c[i]+=c[i-1]+(1ll<<j);//总的每次增加的数量,方便找到r和l的位置
}
printf("%lld\n",(cal(r)-cal(l-1)+mod)%mod);
}