Advertisement
ilevishinov

Опсег

Nov 6th, 2017
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.36 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 zbirCifri(long x) {
  8.         long zbir=0;
  9.         while(x!=0) {
  10.             zbir+=x%10;
  11.             x/=10;
  12.         }
  13.         return zbir;
  14.     }
  15.    
  16.     static long proveri(long N, long A, long B) {
  17.         // Vasiot kod tuka
  18.         if(A==B) return -1;
  19.         //System.out.println(A);
  20.         //System.out.println(B);
  21.        
  22.         if(A*A + zbirCifri(A) + 200*A == N) {
  23.             return A;
  24.         }
  25.         if(B*B + zbirCifri(B) + 200*B == N) {
  26.             return B;
  27.         }
  28.        
  29.         long mid=(B+A)/2;
  30.        
  31.         long shouldBe=mid*mid + zbirCifri(mid) + mid*200;
  32.        
  33.         if(shouldBe==N) return mid;
  34.        
  35.         if(N<shouldBe) {
  36.             return proveri(N,A,mid);
  37.         } else return proveri(N,mid,B);
  38.        
  39.     }
  40.    
  41.     public static void main(String[] args) throws Exception {
  42.         int i,j,k;
  43.        
  44.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  45.        
  46.         long N = Long.parseLong(br.readLine());
  47.        
  48.         StringTokenizer st = new StringTokenizer(br.readLine());
  49.         long A = Long.parseLong(st.nextToken());
  50.         long B = Long.parseLong(st.nextToken());
  51.        
  52.         long res = proveri(N, A, B);
  53.         System.out.println(res);
  54.        
  55.         br.close();
  56.        
  57.     }
  58.    
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement