#include <bits/stdc++.h>
using namespace std;
//return 1, if == m:
//return 0, if < m:
//return 2, if > m:
int func(int mid, int n, int m) {
long long ans = 1;
for (int i = 1; i <= n; i++) {
ans = ans * mid;
if (ans > m) return 2;
}
if (ans == m) return 1;
return 0;
}
int NthRoot(int n, int m) {
//Use Binary search on the answer space:
int low = 1, high = m;
while (low <= high) {
int mid = (low + high) / 2;
int midN = func(mid, n, m);
if (midN == 1) {
return mid;
}
else if (midN == 0) low = mid + 1;
else high = mid - 1;
}
return -1;
}
int main()
{
int n = 3, m = 27;
int ans = NthRoot(n, m);
cout << "The answer is: " << ans << "\n";
return 0;
}
CgoKI2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgovL3JldHVybiAxLCBpZiA9PSBtOgovL3JldHVybiAwLCBpZiA8IG06Ci8vcmV0dXJuIDIsIGlmID4gbToKaW50IGZ1bmMoaW50IG1pZCwgaW50IG4sIGludCBtKSB7CiAgICBsb25nIGxvbmcgYW5zID0gMTsKICAgIGZvciAoaW50IGkgPSAxOyBpIDw9IG47IGkrKykgewogICAgICAgIGFucyA9IGFucyAqIG1pZDsKICAgICAgICBpZiAoYW5zID4gbSkgcmV0dXJuIDI7CiAgICB9CiAgICBpZiAoYW5zID09IG0pIHJldHVybiAxOwogICAgcmV0dXJuIDA7Cn0KCmludCBOdGhSb290KGludCBuLCBpbnQgbSkgewogICAgLy9Vc2UgQmluYXJ5IHNlYXJjaCBvbiB0aGUgYW5zd2VyIHNwYWNlOgogICAgaW50IGxvdyA9IDEsIGhpZ2ggPSBtOwogICAgd2hpbGUgKGxvdyA8PSBoaWdoKSB7CiAgICAgICAgaW50IG1pZCA9IChsb3cgKyBoaWdoKSAvIDI7CiAgICAgICAgaW50IG1pZE4gPSBmdW5jKG1pZCwgbiwgbSk7CiAgICAgICAgaWYgKG1pZE4gPT0gMSkgewogICAgICAgICAgICByZXR1cm4gbWlkOwogICAgICAgIH0KICAgICAgICBlbHNlIGlmIChtaWROID09IDApIGxvdyA9IG1pZCArIDE7CiAgICAgICAgZWxzZSBoaWdoID0gbWlkIC0gMTsKICAgIH0KICAgIHJldHVybiAtMTsKfQoKaW50IG1haW4oKQp7CiAgICBpbnQgbiA9IDMsIG0gPSAyNzsKICAgIGludCBhbnMgPSBOdGhSb290KG4sIG0pOwogICAgY291dCA8PCAiVGhlIGFuc3dlciBpczogIiA8PCBhbnMgPDwgIlxuIjsKICAgIHJldHVybiAwOwp9Cgo=