The answer may be random.AlgorithmBut it is still fed down by the simple prime number table...
N, K (n is the ll range, K is 10 ^ 14). Calculate the largest X so that K ^ X is divisible by N!
When K is not equal to 1, K is first divided into prime number product form, record index, find n! The minimum value of the ratio of the relative prime number index to the index after K decomposition.
# Include <cstdio> # include <cstring> # define ABS (a)> (0 )? (A) :(-(a) # define min (A, B) (A> B? B: A) typedef long ll; const int primerange = 10000000; int prime [primerange + 1]; // prime [0] is the counterint getprime () {memset (prime, 0, sizeof (INT) * (primerange + 1); For (INT I = 2; I <= primerange; I ++) {If (! Prime [I]) prime [++ prime [0] = I; for (Int J = 1; j <= prime [0] & prime [J] <= primerange/I; j ++) {Prime [prime [J] * I] = 1; if (I % prime [J] = 0) break;} return prime [0];} long factor [100] [2]; int faccnt; int getfactors (long x) {faccnt = 0; long TMP = x; For (INT I = 1; prime [I] <= tmp/prime [I]; I ++) {factor [faccnt] [1] = 0; If (TMP % prime [I] = 0) {factor [faccnt] [0] = prime [I]; W Hile (TMP % prime [I] = 0) factor [faccnt] [1] ++, TMP/= prime [I]; faccnt ++ ;}} if (TMP! = 1) factor [faccnt] [0] = TMP, factor [faccnt ++] [1] = 1; return faccnt;} ll N, K; int main () {getprime (); int CAS; scanf ("% d", & CAS); For (INT I = 1; I <= CAS; ++ I) {scanf ("% i64d % i64d", & N, & K); If (k = 1) {printf ("case % d: INF \ n", I ); continue;} ll nn; ll ans; int fcnt = getfactors (k); For (INT I = 0; I <fcnt; ++ I) {ll CNT = 0; for (nn = N; nn/= factor [I] [0]) {CNT + = nn/factor [I] [0];} ll TMP = CNT/factor [I] [1]; if (I = 0) ans = TMP; else ans = min (TMP, ANS );} printf ("case % d: % i64d \ n", I, ANS);} return 0 ;}