牛客小白月赛23 B 题解

I. 题意

给定一个正整数 \(p\),求一个最小的正整数 \(n\),使得 \(n!\)\(p\) 的倍数.

II. 题解

III. 代码

#include<bits/stdc++.h>
using namespace std;
#define LL long long
const int maxn = 1000001;
bool vis[maxn];
int tot, prime[maxn];
void CalPhi()
{
    for (int i = 2; i < maxn; i++)
    {
        if (!vis[i]) prime[tot++] = i;
        for (int j = 0; j < tot; j++)
        {
            if (i * prime[j] > maxn) break;
            vis[i * prime[j]] = 1;
        }
    }
}
vector<pair<LL, int> > getFactors(LL x)
{
    vector<pair<LL, int> > fact;
    for (int i = 0; prime[i] <= x / prime[i]; i++)
    {
        if (x % prime[i] == 0)
        {
            fact.emplace_back(prime[i], 0);
            while (x % prime[i] == 0) fact.back().second++, x /= prime[i];
        }
    }
    if (x != 1) fact.emplace_back(x, 1);
    return fact;
}
int gett(LL p,LL n)
{
    LL k=0;
    while(n%p==0)n/=p,k++;
    return k;
}
int main()
{
    CalPhi();
    int ttt;
    cin>>ttt;
    while(ttt--)
    {
        LL p,ans=0,t;
        cin>>p;
        if(p==1)
        {
            cout<<1<<endl;
            continue;
        }
        auto v=getFactors(p);
        for(auto it:v)
        {
            if(it.second<it.first)t=(it.second)*(it.first);
            else
            {
                LL k=0,cnt=0;
                while(k<it.second)cnt++,k+=gett(it.first,cnt);
                t=cnt;
            }
            ans=max(ans,t);
        }
        cout<<ans<<endl;
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/DariusOrz/p/12545468.html