1151C - Problem for Nazar - CodeForces Solution


constructive algorithms math *1800

Please click on ads to support us..

C++ Code:

/*
Problem: 1151C
Date: 17-01-2024 05:48 AM
*/


#define P 1000000007

#include <iostream>

using namespace std;

long long l, r;

long long sumA(long long l, long long n) {
	return (((l + n - 1) * (l + n - 1) - (l - 1) * (l - 1)) % P + P) % P;
}
long long sumB(long long l, long long n) {
	return (((l + n - 1) * (l + n) - (l - 1) * l) % P + P) % P;
}

long long sumTo(long long n) {
	if(n == 0) {
		return 0;
	}
	long long p = 1;
	long long a = 1;
	long long b = 1;
	long long idx = 0;
	long long sum = 0;
	while(true) {
		if(idx + p >= n) {
			sum = (sum + sumA(a % P, (n - idx) % P)) % P;
			break;
		}else {
			sum = (sum + sumA(a % P, p % P)) % P;
		}
		idx += p;
		a += p;
		p *= 2;

		if(idx + p >= n) {
			sum = (sum + sumB(b % P, (n - idx) % P)) % P;
			break;
		}else {
			sum = (sum + sumB(b % P, p % P)) % P;
		}
		idx += p;
		b += p;
		p *= 2;
	}
	return sum;
}

int main() {
	cin >> l >> r;
	cout << ((((sumTo(r) - sumTo(l - 1)) % P) + P) % P) << endl;
}


Comments

Submit
0 Comments
More Questions

1725G - Garage
1725B - Basketball Together
735A - Ostap and Grasshopper
1183B - Equalize Prices
1481A - Space Navigation
1437B - Reverse Binary Strings
1362B - Johnny and His Hobbies
1299A - Anu Has a Function
1111A - Superhero Transformation
954A - Diagonal Walking
39F - Pacifist frogs
1451C - String Equality
386A - Second-Price Auction
1690E - Price Maximization
282B - Painting Eggs
440A - Forgotten Episode
233B - Non-square Equation
628B - New Skateboard
262B - Roma and Changing Signs
755C - PolandBall and Forest
456B - Fedya and Maths
376B - IOU
1623B - Game on Ranges
1118A - Water Buying
1462C - Unique Number
301A - Yaroslav and Sequence
38A - Army
38C - Blinds
1197A - DIY Wooden Ladder
1717D - Madoka and The Corruption Scheme