/******************************************************* Problem - Factorials and exponents Solution by - Sean Falconer Algorithm: The solution involves a little math. Given n! >= m^d, we want to find a valid d. If we take the log of both sides we get: log(n!) >= d*log(m) However, n! = n*(n-1)*(n-2)*...*1, so log(n!) = log(n) + log(n-1) + ... + log(1). Thus, we can rewrite the equation as: log(n) + log(n-1) + ... + log(1) >= d*log(m). Now solve for d and we get: (log(n) + log(n-1) + ... + log(1)) / log(m) >= d. Now, we just need to compute the first part in a loop and divide by m. *******************************************************/ #include #include using namespace std; const double EPS = 1e-5; int main() { int T; cin >> T; for(int i = 0; i < T; i++) { int n; double m; cin >> n >> m; double logfac = 0; for(int j = 1; j <= n; j++) logfac += log((double)j); logfac /= log(m); if(fabs(m - 1.0) < EPS || m < 1.0) cout << "infinity" << endl; else cout << (int)logfac << endl; } return 0; }