Codeforces Round #553 (Div. 2) C题 C. Problem for Nazar

C. Problem for Nazar

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.

思路:

                                                 n    数量长度      
1                                               1    2^(1-1)
    2 4                                        2    2^(2-1)  
3 5 7 9                                      3    2^(3-1)
    6 8 10 12 14 16 18 20          4    2^(4-1)
11 13 15 17 19 ...                     5    2^(5-1)
    22 24 ....                               6    2^(6-1)  
11+2*(2^5-1) ....   
    22 +2*(2^(6-1)) ... 


                             n:  1 3 5    7 .....             
n为奇数时的首位数   1 3 11  11+2*(2^(5-1)) ...

                             n:  2 4 6    8 .....   
n为偶数时的首位数   2 6 22   22 +2*(2^(6-1)) ...  

f1(1) = 1;
f2(1) = 2;
n为奇数时首位数字的打表f1(i) = f1(i-1)+2*(2^(2*(i-2))))
n为偶数时首位数字的打表f2(i) = 2*f1(i)

 

 根据给出的l r 分别计算l和r内的和相减即可,每一段连续集合求和都可以用公差为2的数列和公式 n*(f(n)+n-1)得到


 

#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ll;
const ll MOD = 1e9+7;
ll f1[50];
ll f2[50];
int pos;
ll q_k(ll x,int n){
	ll res = 1;
	while(n){
		if(n&1) res = res*x;
		x = x*x;
		n>>=1;
	}
	return res;
}
void init(){
	f1[1] = 1;
	f2[1] = 2*f1[1];
	for(int i=2;i<=50;i++){
		f1[i] = f1[i-1]+1ll*2*q_k(1ll*2,2*(i-2));
		f2[i] = 1ll*f1[i]*2;
		if(f1[i]>=1e18){
			pos = i;
			break;
		}
	} 
	return ; 
} 
int getwei(ll n){
	int res = 0;
	while(n){
		res++;
		//cout<<n<<endl;
		n>>=1;
	}
	//cout<<res<<endl;
	return ((res-1)>=0)?res-1:0;
} 
ll he(int n){
	ll res = 0;
	ll x = 1;
	while(n--){
		res+=x;
		x<<=1;
	}
	return res;
}
ll solve(ll x,int mark){
	//cout<<x<<" "<<mark<<endl; 
	if(mark==1) x=x-1;
	ll res = 0;
	ll n = getwei(x);
	ll wei = n;
	ll sum = he(n);
	ll retlen = x-sum;
	//cout<<n<<" "<<sum<<" "<<retlen<<endl;
	while(n>0){
		if(n&1){
			ll len = q_k(2,n-1);
			ll first = f1[n/2+1];
			ll s = ((len-1+first)%MOD*(len%MOD))%MOD;
		//	cout<<"1 "<<s<<endl;
			res = (res+s)%MOD;
		}else{
			ll len = q_k(2,n-1);
			ll first = f2[n/2];
			ll s = ((len-1+first)%MOD*(len%MOD))%MOD;
		//	cout<<"2 "<<s<<endl;
			res = (res+s)%MOD;
		}
		n--;
	}
	if(retlen>0){
		wei++;
		if(wei&1){
			ll first = f1[wei/2+1];
			ll s = ((retlen-1+first)%MOD*(retlen%MOD))%MOD;
		//	cout<<"3 "<<s<<endl;
			res+=s;
		}else{
			ll first = f2[wei/2];
			ll s = ((retlen-1+first)%MOD*(retlen%MOD))%MOD;
		//	cout<<"4 "<<s<<endl;
			res = (res+s)%MOD;
		}
	} 
	return res%MOD;
}
int main(){
	init();
    ll l,r;
    cin>>l>>r;
    ll sum1 = solve(l,1);
    ll sum2 = solve(r,0);
    //cout<<sum1<<" "<<sum2<<endl;
    ll ans = (sum2+MOD-sum1)%MOD;
    cout<<ans<<endl;
	return 0;
}
/*

		              			n    数量长度      
1                               1    2^(1-1)
    2 4                 	    2    2^(2-1)  
3 5 7 9  		                3    2^(3-1)
    6 8 10 12 14 16 18 20       4    2^(4-1)
11 13 15 17 19 ...              5    2^(5-1)
    22 24 ....                  6    2^(6-1)  
11+2*(2^5-1) ....   
    22 +2*(2^(6-1)) ... 
		 
n为奇数时的首位数   1 3 11  11+2*(2^(5-1)) ...11+32
偶数                        2 6 22   22 +2*(2^(6-1)) ...  

f1(1) = 1;
f2(1) = 2;
n为奇数时首位数字的打表f1(i) = f1(i-1)+2*(2^(2*(i-2))))
n为偶数时首位数字的打表f2(i) = 2*f1(i)
比如  l = 7 r = 14
0111 0011 0001
7 >>=1;
n++ ;
n = 2;
int wei = n;
retlen +=7-(3+1) = 3


while(!n--){
       if(n&1){
  	int len = 2^(n-1);
	int first =  f1(n/2+1);
	int sum = first+(len-1)*2;	
       }else{
       	int len = 2^(n-1);
	int first = f2(n/2);
	int sum = first+(len-1)*2;
       }
}
len  = 
wei++ ;
if(n&1){
  	int len = retlen;
	int first =  f1(wei/2+1);
	int sum = first+(len-1)*2;	
       }else{
       	int len = retlen;;
	int first = f2(n/2);
	int sum = first+(len-1)*2;
       }


*/

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值