CF 1114 C. Trailing Loves (or L'oeufs?)

C. Trailing Loves (or L'oeufs?)

链接

题意:

  问n!化成b进制后,末尾的0的个数。

分析:

  考虑十进制的时候怎么求的,类比一下。

  十进制转化b进制的过程中是不断mod b,/ b,所以末尾的0就是可以mod b等于0,那么就是这个数中多少个b的幂。

  所以考虑哪些数和乘起来构成b,对b质因数分解后,这些质因数可以构成一个b。

  对于n个阶乘,可以直接求出每个质因数中幂是多少。然后取下min。

代码:

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<cmath>
#include<cctype>
#include<set>
#include<queue>
#include<vector>
#include<map>
using namespace std;
typedef long long LL;

inline int read() {
    int x=0,f=1;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-1;
    for(;isdigit(ch);ch=getchar())x=x*10+ch-'0';return x*f;
}

int cnt[1000000];
vector<LL> p;

int main() {
    LL n, b, t;
    cin >> n >> b; t = b;
    for (LL i = 2; 1ll * i * i <= t; ++i) {
        if (t % i) continue;
        p.push_back(i);
        while (t % i == 0) cnt[(int)p.size() - 1] ++, t /= i;
        if (t == 1) break;
    }
    if (t != 1) { p.push_back(t); cnt[(int)p.size() - 1] ++; }
    LL ans = 1e18;
    for (int i = 0; i < (int)p.size(); ++i) {
        LL tmp = n, now = p[i], sum = 0;
        while (tmp) { sum += (tmp / now); tmp /= now; }
        ans = min(ans, sum / cnt[i]);
    }
    cout << ans;
    return 0;
}
原文地址:https://www.cnblogs.com/mjtcn/p/10361243.html