Factorial Problem in Base K 质因数分解,然后求阶乘的质因数个数

Description

How many zeros are there in the end of s! if both s and s! are written in base k which 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 integers s and k(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 end of s!.

Sample Input

101 2
12 7

Sample Output

3
1
***********************************************************************************************************************************************************
加个链接,很详细(阶乘的质因数求法)http://blog.csdn.net/hackbuteer1/article/details/6690015
***********************************************************************************************************************************************************
 1 #include<iostream>
 2 #include<string>
 3 #include<cstring>
 4 #include<cstdio>
 5 using namespace std;
 6 #define inf  0x7fffffff
 7 long long  a[1001],b[1001],ge;
 8 void init(long long kk)//对k进行质因数分解
 9 {
10     long long  k=kk,it,jt;
11     ge=0;
12     for(it=2;it<=kk;it++)
13     {
14         if(k%it==0&&k>0)
15         {
16             ge++;
17             jt=0;
18             a[ge]=it;
19             while(k%it==0)
20             {
21                 jt++;
22                 k/=it;
23             }
24             b[ge]=jt;
25         }
26     }
27 }
28 int main()
29 {
30     char str1[1001];
31     long long n,k,i,j;
32     while(scanf("%s %lld",str1,&k)!=EOF)
33     {
34         init(k);
35         long long len=strlen(str1);
36         //cout<<"len:: "<<len<<endl;
37         n=0;
38         for(i=0;i<len;i++)
39         {
40             if(str1[i]>='0'&&str1[i]<='9')
41               n=n*k+(str1[i]-'0');
42             if(str1[i]>='A'&&str1[i]<='Z')
43               n=n*k+(str1[i]-'A'+10);
44             if(str1[i]>='a'&&str1[i]<='z')
45               n=n*k+(str1[i]-'a'+36);
46         }
47         //cout<<"n:: "<<n<<endl;
48         //cout<<"ge:: "<<ge<<endl;
49         //for(i=1;i<ge;i++)
50         //{
51             //printf("a[%lld]: %lld   b[%lld]: %lld\n",i,a[i],i,b[i]);
52         //}
53         long long min=-1,sum,m;
54         for(i=1;i<=ge;i++)
55         {
56             sum=0;
57             m=n;
58             while(m>0)
59             {
60                 sum+=(m/a[i]);
61                 m/=a[i];
62             }
63             if(min==-1)
64                min=sum/b[i];
65             else
66             {
67                 if(min>(sum/b[i]))
68                   min=(sum/b[i]);
69             }
70         }
71         printf("%lld\n",min);
72 
73     }
74 
75 }
View Code

 



posted @ 2013-11-23 21:29  persistent codeants  阅读(321)  评论(0编辑  收藏  举报