Problem 379
Integer Nth Root
Medium · 20 pts · Math, Binary Search
Problem
Given integers N and M, find the integer X such that X raised to the power N equals M exactly. If no such integer exists, print -1. M can be as large as 2^60 (about 1.15 × 10^18), so floating-point roots are not precise enough; binary search on X and compare X^N with M using exact (or overflow-safe) arithmetic, stopping early as soon as the product exceeds M.
Input
Two integers N and M.
Output
Print the integer N-th root of M, or -1.
Constraints
1 <= N <= 60; 1 <= M <= 2^60
Example input
3 27
Example output
3
Explanation
3 to the power 3 is 27, so the cube root of 27 is the integer 3.