Advertisement
Josif_tepe

Untitled

Aug 22nd, 2023
812
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.29 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.InputStreamReader;
  3. import java.util.StringTokenizer;
  4.  
  5. public class Range {
  6.  
  7.     static long proveri(long N, long A, long B) {
  8.         long left = A, right = B;
  9.         while(left <= right) {
  10.             long middle = (left + right) / 2;
  11.             long tmp = middle;
  12.             long sum = 0;
  13.             while(tmp > 0) {
  14.                 sum += tmp % 10;
  15.                 tmp /= 10;
  16.             }
  17.             long resultEquation = middle * middle + 200 * middle + sum;
  18.             if(resultEquation == N) {
  19.                 return middle;
  20.             }
  21.             if(resultEquation < N) {
  22.                 left = middle + 1;
  23.             }
  24.             else {
  25.                 right = middle - 1;
  26.             }
  27.  
  28.         }
  29.         return -1;
  30.     }
  31.  
  32.     public static void main(String[] args) throws Exception {
  33.         long i,j,k;
  34.  
  35.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  36.  
  37.         long N = Long.parseLong(br.readLine());
  38.  
  39.         StringTokenizer st = new StringTokenizer(br.readLine());
  40.         long A = Long.parseLong(st.nextToken());
  41.         long B = Long.parseLong(st.nextToken());
  42.  
  43.         long res = proveri(N, A, B);
  44.         System.out.println(res);
  45.  
  46.         br.close();
  47.  
  48.     }
  49.  
  50. }
  51.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement