Solution: SP22412 DIVFACT3 - Divisors of factorial (hard)

· · 题解

In this article, p_k denotes the k-th smallest prime number.

Statement

Given N, M, calculate:

\sigma_0(N!) \bmod M.

Solution

Let N! = \displaystyle\prod\limits_{p_i \le N} p_i^{c_i}.

By Legendre's theorem, the exponent of p_i in N! is:

\sum\limits_{\substack{c=1\\p_i^c \le N}}\left\lfloor \frac{N}{p_i^c} \right\rfloor.

Therefore:

\sigma_0(N!) &= \prod\limits_{p_i \le N} \sigma_0(p_i^{c_i}) \\ &= \prod\limits_{p_i \le N} (c_i + 1)\\ &= \prod\limits_{p_i \le N} \left( \sum\limits_{\substack{c=1\\p_i^c \le N}}\left\lfloor \frac{N}{p_i^c} \right\rfloor + 1 \right). \end{aligned}

Let B \gets \lfloor \sqrt{N} \rfloor.

Notice that when p_i \in (B,N], \displaystyle\sum\limits_{\substack{c=1\\p_i^c \le N}}\left\lfloor \frac{N}{p_i^c} \right\rfloor = \left\lfloor \dfrac{N}{p_i} \right\rfloor.

Thus:

\sigma_0(N!) &= \left( \prod\limits_{p_i \le B} \left( \sum\limits_{\substack{c=1\\p_i^c \le N}}\left\lfloor \frac{N}{p_i^c} \right\rfloor + 1 \right) \right) \left( \prod\limits_{p_i \in (B,N]} \left( \left\lfloor \frac{N}{p_i} \right\rfloor + 1 \right) \right). \end{aligned}

First part:

\prod\limits_{p_i \le B} \left( \sum\limits_{\substack{c=1\\p_i^c \le N}}\left\lfloor \frac{N}{p_i^c} \right\rfloor + 1 \right).

This part can be calculated after precomputing primes in [2,B].

The number of primes in [2,B] is \mathcal{O}\left( \dfrac{\sqrt{N}}{\log N} \right), and calculating a single \displaystyle\sum\limits_{\substack{c=1\\p_i^c \le N}}\left\lfloor \frac{N}{p_i^c} \right\rfloor takes \mathcal{O}(\log N).

Thus the first part is calculated in \mathcal{O}(\sqrt{N}).

Second part:

\prod\limits_{p_i \in (B,N]} \left( \left\lfloor \frac{N}{p_i} \right\rfloor + 1 \right).

Consider the floor division trick. For a block [l,r], its contribution is:

\left( \left\lfloor \frac{N}{l} \right\rfloor + 1 \right) ^ {\left( \pi(r) - \pi(l-1) \right)}.

Precompute all \pi(i) (i \le N) using the linear sieve, which has complexity of \mathcal{O}(N).

The complexity of the floor division trick is \mathcal{O}(\sqrt{N} \log N).

Thus the second part is calculated in \mathcal{O}(\sqrt{N} \log N).

Total complexity: \mathcal{O}\left( N + T \sqrt{N} \log N\right).

Code

Note that M may be 1.

#include<bits/stdc++.h>
#define int long long

const int _N=5510;

const int N=1e8+10;

using namespace std;

int T;

int n,m,b,ans;

int _in1[_N],_in2[_N];

int cnt,p[N];

bool vis[N];

int pi[N];

inline int read(){
    int ret=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9')ret=ret*10+(ch&15),ch=getchar();
    return ret*f;
}

int _sqrt(int x){
    int ret=floor(sqrtl(x));
    while((ret+1)*(ret+1)<=x)ret++;
    return ret;
}

int qpow(int x,int y){
    x%=m;
    int ret=1;
    while(y){
        if(y&1)ret=ret*x%m;
        x=x*x%m;
        y>>=1;
    }
    return ret;
}

void _init(int _n){
    for(int i=2;i<=_n;i++){
        if(!vis[i])p[++cnt]=i;
        pi[i]=cnt;
        for(int j=1;j<=cnt&&i*p[j]<=_n;j++){
            vis[i*p[j]]=1;
            if(!(i%p[j]))break;
        }
    }
}

int _cal(int p_i){
    int ret=1;
    for(int _p=p_i;_p<=n;_p*=p_i)ret+=n/_p;
    return ret;
}

void _solve(int _n,int _m){
    n=_n,m=_m;
    b=_sqrt(n);
    ans=1ll%m;
    for(int i=1;i<=cnt&&p[i]<=b;i++)ans=ans*_cal(p[i])%m;
    for(int l=b+1,r;l<=n;l=r+1){
        r=n/(n/l);
        ans=ans*qpow(n/l+1,pi[r]-pi[l-1])%m;
    }
    printf("%lld\n",ans);
}

signed main(){
    T=read();
    int mx=0;
    for(int i=1;i<=T;i++)_in1[i]=read(),_in2[i]=read(),mx=max(mx,_in1[i]);
    _init(mx);
    for(int i=1;i<=T;i++)_solve(_in1[i],_in2[i]);
    return 0;
}

Afterword

Why not try DIVFACT4?