Problem for Nazar
https://codeforces.com/contest/1151/problem/C
题解:前缀和+等差数列
1、认识到这是一个等差数列;
2、预处理出每一段的第一项、每一段和、每一段前缀和;
3、减少分类情况采用左区间固定1的方法,即[l,r]=[1,r]-[1,l-1];
4、用log2函数判断当前端点第几段;
5、特判右端点0和第0段;
6、完整的段数前缀和+右端点所在的非完整的和;
7、注意求模;
/*
*@Author: STZG
*@Language: C++
*/
#include <bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<bitset>
#include<queue>
#include<deque>
#include<stack>
#include<cmath>
#include<list>
#include<map>
#include<set>
//#define DEBUG
#define RI register int
#define endl "\n"
using namespace std;
typedef long long ll;
//typedef __int128 lll;
const int N=100000+10;
const int M=100000+10;
const int MOD=1e9+7;
const double PI = acos(-1.0);
const double EXP = 1E-8;
const int INF = 0x3f3f3f3f;
ll t,n,m,k,p,l,r,u,v;
int ans,cnt,flag,temp;
ll sum[N];
ll f[N],pow2[N];
char str;
struct node{};
void init(){
bool flag=1;
ll base=1;
ll odd=1;
ll even=2;
sum[0]=1;
for(int i=0;i<=63;i++){
pow2[i]=base;
if(flag){
f[i]=odd;
odd+=2*base;
}else{
f[i]=even;
even+=2*base;
}
if(i)sum[i]=(sum[i-1]+((f[i]%MOD)*(base%MOD))%MOD+((base%MOD)*((base-1)%MOD))%MOD)%MOD;
flag=!flag;
base*=2;
}
}
ll sloved(ll R){
if(R<=0)
return 0;
int r=log2(R);//最后一段指数
if(r==0)
return 1;
ll cnt=R-pow2[r]+1;//cout<<sum[r]<<endl;
return (sum[r-1]+((f[r]%MOD)*(cnt%MOD))%MOD+((cnt%MOD)*((cnt-1)%MOD))%MOD)%MOD;
}
int main()
{
#ifdef DEBUG
freopen("input.in", "r", stdin);
//freopen("output.out", "w", stdout);
#endif
//ios::sync_with_stdio(false);
//cin.tie(0);
//cout.tie(0);
//scanf("%d",&t);
//while(t--){
init();
scanf("%I64d%I64d",&l,&r);
cout<<(sloved(r)-sloved(l-1)+MOD)%MOD<<endl;
//}
#ifdef DEBUG
printf("Time cost : %lf s\n",(double)clock()/CLOCKS_PER_SEC);
#endif
//cout << "Hello world!" << endl;
return 0;
}