Advertisement
Josif_tepe

Untitled

Oct 28th, 2024
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.74 KB | None | 0 0
  1. #include <iostream>
  2. #include <queue>
  3. #include <vector>
  4. using namespace std;
  5.  
  6. typedef long long ll;
  7. int main() {
  8.     ll Z, C;
  9.     cin >> Z >> C;
  10.    
  11.     ll L = 1, R = 1;
  12.     for(int i = 0; i < C; i++) {
  13.         L *= 10;
  14.         R *= 10;
  15.     }
  16.     L /= 10;
  17.     R -= 1;
  18.    
  19.     while(L <= R) {
  20.         ll middle = (L + R)  / 2;
  21.         ll number = middle;
  22.         ll sum = number;
  23.        
  24.         while(number > 0) {
  25.             number /= 10;
  26.             sum += number;
  27.         }
  28.        
  29.         if(sum == Z) {
  30.             cout << middle << endl;
  31.             return 0;
  32.         }
  33.         if(sum < Z) {
  34.             L = middle + 1;
  35.         }
  36.         else {
  37.             R = middle - 1;
  38.         }
  39.     }
  40.    
  41.     return 0;
  42. }
  43.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement