UVa 10780 (质因数分解) Again Prime? No Time.

求mk整除n!,求k的最大值。

现将m分解质因数,比如对于素数p1分解出来的指数为k1,那么n!中能分解出多少个p1出来呢?

考虑10!中2的个数c:1~10中有10/2个数是2的倍数,c += 5;1~10中有10/4个数是4的倍数,所以c += 2,其中有10/8 = 1个数是8的倍数,所以c += 1;

这样10!中就能分解出8个2

对于每个素数p,求出ci / ki的最小值就是答案。

 1 #include <cstdio>
 2 #include <cmath>
 3 #include <cstring>
 4 #include <vector>
 5 using namespace std;
 6 
 7 const int INF = 1000000000;
 8 const int maxn = 5000;
 9 bool vis[maxn + 10];
10 int prime[700], pcnt = 0;
11 
12 void Init()
13 {
14     int m = sqrt(maxn + 0.5);
15     for(int i = 2; i <= m; i++) if(!vis[i])
16         for(int j = i*i; j<= maxn; j += i) vis[j] = true;
17     for(int i = 2; i <= maxn; i++) if(!vis[i]) prime[pcnt++] = i;
18 }
19 
20 vector<int> p, e, a;
21 
22 int main()
23 {
24     Init();
25     //printf("%d
", pcnt);
26     int T;
27     scanf("%d", &T);
28     for(int kase = 1; kase <= T; kase++)
29     {
30         p.clear(); e.clear(); a.clear();
31         int m, n;
32         scanf("%d%d", &m, &n);
33         for(int i = 0; i < pcnt && m > 1; i++)
34         {
35             if(m % prime[i] == 0)
36             {
37                 int c = 0;
38                 while(m % prime[i] == 0)
39                 {
40                     m /= prime[i];
41                     c++;
42                 }
43                 e.push_back(c);
44                 p.push_back(prime[i]);
45             }
46         }
47 
48         int ans = INF;
49         for(int i = 0; i < p.size(); i++)
50         {
51             int c = 0, base = p[i];
52             while(n >= base)
53             {
54                 c += n / base;
55                 base *= p[i];
56             }
57             ans = min(ans, c / e[i]);
58         }
59         printf("Case %d:
", kase);
60         if(ans) printf("%d
", ans);
61         else puts("Impossible to divide");
62     }
63 
64     return 0;
65 }
代码君
原文地址:https://www.cnblogs.com/AOQNRMGYXLMV/p/4346148.html