Guest User

Untitled

a guest
Dec 28th, 2024
931
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4.  
  5. pair<long long, int> computeLucky(long long left, long long right, int threshold) {
  6.  
  7. if (right - left + 1 < threshold) {
  8. return {0, 0};
  9. }
  10.  
  11.  
  12. if (right - left + 1 == 1) {
  13. return {left, 1};
  14. }
  15.  
  16.  
  17. long long mid = left + (right - left) / 2;
  18.  
  19. if ((right - left + 1) % 2) {
  20.  
  21. pair<long long, int> leftResult = computeLucky(left, mid - 1, threshold);
  22.  
  23.  
  24. long long totalLucky = mid + 2 * leftResult.first + mid * leftResult.second;
  25. int totalSegments = 2 * leftResult.second + 1;
  26.  
  27. return {totalLucky, totalSegments};
  28. } else {
  29.  
  30. pair<long long, int> leftResult = computeLucky(left, mid, threshold);
  31.  
  32.  
  33. long long totalLucky = 2 * leftResult.first + mid * leftResult.second;
  34. int totalSegments = 2 * leftResult.second;
  35.  
  36. return {totalLucky, totalSegments};
  37. }
  38. }
  39.  
  40. int main() {
  41. ios::sync_with_stdio(false);
  42. cin.tie(NULL);
  43.  
  44. int test_cases;
  45. cin >> test_cases;
  46.  
  47. while (test_cases--) {
  48. long long n, k;
  49. cin >> n >> k;
  50.  
  51.  
  52. pair<long long, int> result = computeLucky(1, n, k);
  53.  
  54.  
  55. cout << result.first << "\n";
  56. }
  57.  
  58. return 0;
  59. }
  60.  
Advertisement
Add Comment
Please, Sign In to add comment