Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- pair<long long, int> 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<long long, int> 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<long long, int> 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<long long, int> result = computeLucky(1, n, k);
- cout << result.first << "\n";
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment