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

 



转载于:https://www.cnblogs.com/sdau--codeants/p/3439464.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值