fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. // Problem : Find Nth root of M
  13. Scanner sc = new Scanner(System.in);
  14. int n = sc.nextInt();
  15. int m = sc.nextInt();
  16. int l=1;
  17. int r = m;
  18.  
  19. while(l<=r){
  20. int mid = (l+r)/2;
  21.  
  22. if(Math.pow(mid,n)>m){
  23. r = mid - 1;
  24. }
  25. else if(Math.pow(mid,n)<m){
  26. l = mid + 1;
  27. }
  28. else{
  29. System.out.println(mid);
  30. return;
  31. }
  32. }
  33. System.out.println("-1");
  34. }
  35. }
Success #stdin #stdout 0.18s 56732KB
stdin
2 9
stdout
3