跳到正文

题解

Solution: SP22412 DIVFACT3 - Divisors of factorial (hard)

xsyqwq约 7 分钟
原文
文章信息与版本

文章信息

发布
2026年4月12日
收录
2026年4月13日
更新
2026年11月19日
篇幅
约 7 分钟
获赞
15
题目
SP22412

版本

  1. v2当前
  2. v1

In this article, pkp_k denotes the kk-th smallest prime number.

Statement

Given N,MN, M, calculate:

σ0(N!) mod M.\sigma_0(N!) \bmod M.

Solution

Let N!=∏pi≤NpiciN! = \displaystyle\prod\limits_{p_i \le N} p_i^{c_i}.

By Legendre’s theorem, the exponent of pip_i in N!N! is:

∑c=1pic≤N⌊Npic⌋.\sum\limits_{\substack{c=1\\p_i^c \le N}}\left\lfloor \frac{N}{p_i^c} \right\rfloor.

Therefore:

σ0(N!)=∏pi≤Nσ0(pici)=∏pi≤N(ci+1)=∏pi≤N(∑c=1pic≤N⌊Npic⌋+1).\begin{aligned} \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←⌊N⌋B \gets \lfloor \sqrt{N} \rfloor.

Notice that when pi∈(B,N]p_i \in (B,N], ∑c=1pic≤N⌊Npic⌋=⌊Npi⌋\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:

σ0(N!)=(∏pi≤B(∑c=1pic≤N⌊Npic⌋+1))(∏pi∈(B,N](⌊Npi⌋+1)).\begin{aligned} \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:

∏pi≤B(∑c=1pic≤N⌊Npic⌋+1).\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][2,B].

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

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


Second part:

∏pi∈(B,N](⌊Npi⌋+1).\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][l,r], its contribution is:

(⌊Nl⌋+1)(π(r)−π(l−1)).\left( \left\lfloor \frac{N}{l} \right\rfloor + 1 \right) ^ {\left( \pi(r) - \pi(l-1) \right)}.

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

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

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


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

Code

Note that MM may be 11.

C++
#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?

原文
存档评论4 条抓取于