Test instructions: Given two numbers A and B, how many consecutive 0 of the output (1) suffix is obtained after the factorial of a is converted to B-binary ? (2) How many people are there?
Idea: Solve the problem one by one.
Set A! =k. K temporarily does not have to go directly into the B-system.
(1) Factorial suffix 0 problem. First look at this example of the decimal suffix 0 : http://www.cnblogs.com/xcw0754/p/4604473.html
The solution is almost, slightly changed. First, the decomposition of B into a number of prime numbers (such as 8={2*2*2}) is stored in a set a (note that the natural number of prime factorization is unique), as long as there is a sequence a can constitute a 0, because full B on the carry, B can be expressed as 10, representing a 1*b+0. Then we need to know how many sets a is in K. The k also prime number is decomposed into set B, minus one A at a time in set B, and each successful loss of 1 A will have 1 suffix 0.
Just ask for a! Isn't it going to explode? Yes. The prime factorization of K is the same as the prime factorization of {1~a}. Then the traversal [1,a] is decomposed one by one, noting that prime numbers greater than b do not have to be asked for, it is not possible to make up B. The set of the finished prime number B should be 2 have several, 3 have several, 5 have several, 7 have several .... If you need 4 2 in collection A, see how many 2 in B, each two 2 can be a suffix 0, if there are 5 2 in B, then there will be 2 0.
In short, this step needs to decompose B prime numbers into a, break [1,a] prime numbers into set B, and see how many a in B.
(2) The problem of the number of factorial digits.
First example: a three-digit n satisfies 102 <= N < 103
Then his number of bits w satisfies w = log103 = 3. So it is only required that the LGN down +1 is the number of digits since factorial such as 5 factorial is 5 * 4 * 3 * 2 * 1. The number of his digits satisfies LG5 * 4 * 3 * 2 * 1 = lg5 + lg4 + lg3 + lg2 + lg1 Here the sum will not exceed the number limit.
Of course this is the decimal. If it is the M-system, you can change the log10n into LOGMN. High School Knowledge: LOGM (n) is represented by LGN/LGM. Here is a double down to the accuracy of the question to note: when converted to int, to floor (calculated number of digits + 1e-9) + 1. Note: 1e-9 do not use variables to save them.
The final figure is computed as: Floor ( logmn + LOGM (n-1) + ... + logm1 + 1e-9) + 1. The red part is the main number to be counted, with a loop to accumulate.
1#include <bits/stdc++.h>2 #defineLL Long Long3 using namespacestd;4 Const intn=2147483647;5 //const int mod=1e-9; Remember that you can't save it, but it's an inexact value when it's saved up. 6 inthas[999];//Hash7 intneed[999];//decomposition of the binary system8 9 intSecondintAintb//total number of digitsTen { One Doubletmp=0.0; A for(intI=2; i<=a; i++) tmp+=log10 (i); - if(b!=Ten) Tmp/=log10 (b);//10 No need to turn the system - returnFloor (tmp + 1e-9)+1; the } - - intFirstintAintb//suffix 0 Numbers - { +Memset (Has,0,sizeof(has)); -memset (Need,0,sizeof(need)); + for(intI=2; i<=a; i++)//first decompose each number in the factorial A { at intt=i; - for(intj=2; j<=t&&j<=b; J + +)//Prime number is J - { - while(t%j==0)//that can be divisible by J, all removed . - { -has[j]++; inT/=J; - } to } + } - intt=b; the for(intI=2; i<=b; i++)//binary B for decomposition * { $ while(t%i==0)Panax Notoginseng { -need[i]++;//the number of prime numbers to record. theT/=i; + } A } the + intCnt=N; - for(intI=2; i<=b; i++) $ if(need[i]>0) Cnt=min (Cnt,has[i]/need[i]);//The wooden barrel principle, the lowest piece of wood works $ return(cnt>=n?)0: CNT); - } - the - Wuyi intMain () the { - //freopen ("E://input.txt "," R ", stdin); Wu intA, B; - while(~SCANF ("%d%d",&a,&b)) Aboutprintf"%d%d\n", First (a, B), second (A, b)); $ return 0; -}
AC Code
UVA 10061 How many zero's and how many digits? (M-binary, factorial-digit, factorial-suffix 0)