Advertisement
Josif_tepe

Untitled

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