uva10061 - How many zero's and how many digits ?
题目大意: 给出一个数num和一个进制base 求出num在该base下的末尾0进制和总位数
第一部分求末尾0进制:
先求出进制的素因数分解 例如 10=2*5
然后找出找出num的素因数分解 中有多少个 2或者5 由此找出多少个0
第二部分求出位数,就是求出 num = base^x 的最大X
利用两边去log计算
因为精读问题WA了N次。。最后也是看网上的代码解决的orz
Problem G
How many zeros and how many digits?
Input: standard input
Output: standard output
Given a decimal integer number you will have to find out how many trailing zeros will be there in its factorial in a given number system and also you will have to find how many digits will its factorial have in a given number system? You can assume that for a b based number system there are b different symbols to denote values ranging from 0 ... b-1.
Input
There will be several lines of input. Each line makes a block. Each line will contain a decimal number N (a 20bit unsigned number) and a decimal number B (1<B<=800), which is the base of the number system you have to consider. As for example 5! = 120 (in decimal) but it is 78 in hexadecimal number system. So in Hexadecimal 5! has no trailing zeros
Output
For each line of input output in a single line how many trailing zeros will the factorial of that number have in the given number system and also how many digits will the factorial of that number have in that given number system. Separate these two numbers with a single space. You can be sure that the number of trailing zeros or the number of digits will not be greater than 2^31-1
Sample Input:
2 10
5 16
5 10
Sample Output:
0 1
0 2
1 3
代码:
1 #include <iostream> 2 #include <vector> 3 #include <algorithm> 4 #include <limits.h> 5 #include <memory.h> 6 #include <malloc.h> 7 #include <math.h> 8 #include <cstdio> 9 using namespace std; 10 11 const int maxn = 900; 12 int f[maxn]; 13 int fn[maxn]; 14 int p=0; 15 16 void div(long long num) 17 { 18 int many=0; 19 memset(f,0,sizeof(f)); 20 memset(fn,0,sizeof(fn)); 21 p=-1; 22 for(long long i=2;i<=num;i++) 23 { 24 while(num%i==0) 25 { 26 if(f[p]==i) 27 { 28 fn[p]++; 29 } 30 else 31 { 32 p++; 33 f[p]=i; 34 fn[p]++; 35 } 36 num/=i; 37 } 38 } 39 } 40 int main() 41 { 42 long long num,base; 43 int numn[maxn]; 44 while(cin>>num>>base) 45 { 46 div(base); 47 memset(numn,0,sizeof(numn)); 48 49 int pn=0; 50 for(int i=1;i<=num;i++) 51 { 52 int temp=i; 53 for(int j=0;j<=p;j++) 54 { 55 while(temp%f[j]==0) 56 { 57 numn[j]++; 58 temp/=f[j]; 59 } 60 } 61 } 62 63 long long MIN=LONG_LONG_MAX; 64 for(int i=0;i<=p;i++) 65 { 66 numn[i]/=fn[i]; 67 if(numn[i]<MIN) 68 MIN=numn[i]; 69 } 70 71 72 double digits=0; 73 for(int i=1;i<=num;i++) 74 { 75 digits =digits+ log10(i); 76 } 77 digits/=log10(base); 78 79 if (digits - floor(digits) < 1e-10) 80 digits ++; 81 else 82 digits = floor(digits+ 1); 83 84 printf("%lld %.0f\n",MIN,digits); 85 } 86 return 0; 87 }