ZOJ Monthly, June 2012 - K Factorial Problem in Base K - zoj 3621

本场比赛,相对最好做的一题,看出本质,就容易了。

问题简化下,比如给你十进制数 s, 问 s! 末尾有几个0。我们一般都是直接找有多少个 2,5。因为2*5 = 10

那么 现在是 k进制数 s, 同理,就是找 s! 里面,有多少个因子的能够组成 k,最多组成 ans 个 k, ans 就是答案。

这个 自己可以简单证明下。后面好处理。

View Code
 1 #include<cstdio>
 2 #include<iostream>
 3 #include<algorithm>
 4 #include<cstring>
 5 #include<cmath>
 6 using namespace std;
 7 #define N 65
 8 #define M 10010
 9 #define inf 2000000000
10 typedef long long LL;
11 char str[N];
12 LL k;
13 LL oksu(LL x) {
14     for (LL i = 2; i < x; ++i)
15         if (x % i == 0) return 0;
16     return 1;
17 }
18 LL solve(LL x, LL temp) {
19     LL y = x, ans = 0;
20     while (y <= temp) {
21         ans += temp / y;
22         if (temp/y < x) break;
23         y *= x;
24     }
25     return ans;
26 }
27 int main() {
28     while (scanf("%s%d", str, &k) != EOF) {
29         LL len = strlen(str);
30         LL s = 0;
31         for (LL i = 0; i < len; ++i) {
32             LL num;
33             if (str[i] <= 'z' && str[i] >= 'a') num = str[i]-'a'+36;
34             else if (str[i] <= 'Z' && str[i] >= 'A') num = str[i]-'A'+10;
35             else num = str[i]-'0';
36             s = num + s * k;
37         }
38         LL ans = -1;
39         for (LL i = 2;i <= k; ++i) {
40             if (k % i == 0 && oksu(i)){
41                 LL temp = k, bia = 0, x = 0;
42                 while(temp%i== 0) {
43                     temp /= i;
44                     bia++;
45                 }
46                 x = solve(i, s);
47                 if (ans == -1 || ans > x/bia)
48                     ans = x/bia;
49             }
50         }
51         printf("%lld\n", ans);
52     }
53     return 0;
54 }

转载于:https://www.cnblogs.com/slon/archive/2012/06/30/2571053.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值