long long totalLucky = 2 * leftResult.first + mid * leftResult.second; int totalSegments = 2 * leftResult.second; return {totalLucky, totalSegments}; } } int main() { ios::sync_with_stdio(false); cin.tie(NULL); int test_cases; cin >> test_cases; while (test_cases--) { long long n, k; cin >> n >> k;
C.
In C++ language
#include
using namespace std;
pair computeLucky(long long left, long long right, int threshold) {
if (right - left + 1 < threshold) {
return {0, 0};
}
if (right - left + 1 == 1) {
return {left, 1};
}
long long mid = left + (right - left) / 2;
if ((right - left + 1) % 2) {
pair leftResult = computeLucky(left, mid - 1, threshold);
long long totalLucky = mid + 2 * leftResult.first + mid * leftResult.second;
int totalSegments = 2 * leftResult.second + 1;
return {totalLucky, totalSegments};
} else {
pair leftResult = computeLucky(left, mid, threshold);
long long totalLucky = 2 * leftResult.first + mid * leftResult.second;
int totalSegments = 2 * leftResult.second;
return {totalLucky, totalSegments};
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
int test_cases;
cin >> test_cases;
while (test_cases--) {
long long n, k;
cin >> n >> k;
pair result = computeLucky(1, n, k);
cout
Like and Subscribe guys..👍