Factorial problem in base K Time Limit: 2 seconds memory limit: 65536 KB
How many zeros are there in the endS!If bothSAndS!Are written in baseKWhich is not necessarily to be 10? For general base, the digit order is 0-9, A-Z, A-z (increasingly), for example F4 in base 46 is actually 694 in base 10, and F4 in base 46 is 1890 in base 10.
Input
There are multiple cases (less than 10000). Each case is a line containing two integersSAndK(0 ≤S<2 ^ 63, 2 ≤K≤ 62 ).
Output
For each case, output a single line containing exactly one integer in base 10 indicating the number of zeros in the endS!.
Sample Input
101 212 7
Sample output
31
Q: Give us S and K the K-hexadecimal S. now we want to replace the 10-hexadecimal factorial of s with a number of zeros at the end of K-hexadecimal;
Idea: Find the number of quality factors in K-base [I], and the number of quality factors in S's factorial Pn, then how many zeros after changing to K-base, is the smallest of PN/kV [I.
When I first found the Pn, all the test data was passed, but wa was finished, and, later, I opened the ANS very big and the result was over. Depressing
# Include <cstdio> # include <cstring> # include <cmath> # include <iostream> # define n 65 using namespace STD; char STR [N]; int K; long long s; int PRM [20] = {2, 3, 5, 7, 11, 13, 17,19, 23,29, 31,37, 41,43, 47,53, 59,61}, Kv [20]; /* int judge (int x, int TP) {int y = X, A = 0; while (Y <= TP) {A + = TP/y; // printf ("Y: % d \ ttemp: % d ans: % d \ n", Y, temp, ANS); If (TP/Y <X) break; y * = x;} return a;} */void solve (long x) {int I; long res, ANS = 0x 7 ffffffffffffff; // The opening is small. Actually, wa long PN; for (I = 0; I <18; I ++) {res = X; res = Res/PRM [I]; Pn = res; while (RES) {res = Res/PRM [I]; Pn + = res ;} if (Kv [I] & pn/kV [I] <ans) ans = Pn/kV [I];} printf ("% LLD \ n", ANS );} int main () {int I, j; while (scanf ("% S % d", STR, & K )! = EOF) {int Len = strlen (STR); s = 0; for (I = 0; I <Len; I ++) {int num; if (STR [I] <= 'Z' & STR [I]> = 'A') num = STR [I]-'A' + 36; else if (STR [I] <= 'Z' & STR [I]> = 'A') num = STR [I]-'A' + 10; else num = STR [I]-'0'; S = num + S * k;} memset (Kv, 0, sizeof (Kv); int n = K, X; for (I = 0; I <18; I ++) {While (N % PRM [I] = 0) {kV [I] ++; n/= PRM [I] ;}} solve (s) ;}return 0 ;}
I remember having done similar questions before. This is the question of how many quality factors are included in the factorial.
Factorial problem in base K (zoj3621)