题目描述:
C - Factors of Factorial
Time Limit: 2 sec / Memory Limit: 256 MB
Score : 300300 points
Problem Statement
You are given an integer NN. Find the number of the positive divisors of N!N!,
modulo 109+7109+7.
Constraints
- 1≤N≤1031≤N≤103
Input
The input is given from Standard Input in the following format:
NNOutput
Print the number of the positive divisors of N!N!, modulo 109+7。
Sample Input 1
3Sample Output 1
4There are four divisors of 3!3! =6=6: 11, 22, 33 and 66.
Thus, the output should be 44.
Sample Input 2
6Sample Output 2
30Sample Input 3
1000Sample Output 3
972926972
题意:
给定一正整数N,求N!的因子个数。
思路:
① 假如是求N的因子个数,不考虑O(N)的暴力方法,可以
对N进行质因
子分解,然后N=p1^a1*p1^a2*...*pk^ak,对于p1,我们可以选择0个、1个、2个,,,,,,a1个,
p2、p3、p4同理。所以最终能产生的因子个数为 (a1+1)*(a2+1)*(a3+1)*(a4+1)*......*(ak+1)
。至于为什么这些组合中不会出现相同的因子,可以再考虑一下。假设最后乘出了相同因子,
那么一定是某几个质因子想乘==另外几个质因子相乘,显然不成立。。。
②N!的因子个数,相同的思路,即求各质因子各有多少个,此时小于等于N的所有质数都会
是质因子,但每个数有多少个,可以如下考虑(转载):
例如:20!
1.先求出20以内的素数,(2,3,5,7,11,13,17,19)
2.再求各个素数的阶数
e(2)=[20/2]+[20/4]+[20/8]+[20/16]=18;
e(3)=[20/3]+[20/9]=8;
e(5)=[20/5]=4;
...
e(19)=[20/19]=1;
所以
20!=2^18*3^8*5^4*...*19^1
解释:
2、4、6、8、10、12、14、16、18、20能被2整除
4、8、12、16、20能被4整除(即被2除一次后还能被2整除)
8、16能被8整除(即被2除两次后还能被2整除)
16能被16整除(即被2除三次后还能被2整除)
这样就得到了2的阶。其它可以依次递推。
求每个质数在N!中出现了几次:
PS知识点:
若正整数n可分解为p1^a1*p1^a2*...*pk^ak,
其中pi为两两不同的素数,ai为对应指数,
则n的约数个数为(1+a1)*(1+a2)*....*(1+ak)。
如180=2*2*3*3*5=2^2*3^2*5。
180的约数个数为(1+2)*(1+2)*(1+1)=18个。
若求A/B的约数个数,A可分解为p1^a1*p2^a2*...*pk^ak,B可分解为q1^b1*q1^b2*...*qk^bk,
则A/B的约数个数 为(a1-b1+1)*(a2-b2+1)*(a3-b3+1)...*(ak-bk+1)。
int cal(int n, int p)
{
if(n < p) return 0;
else return n / p + cal(n / p, p);
}
代码实现:
#include<bits/stdc++.h>
#define LL long long
using namespace std;
const int N=2e5+100;
const int mod=1e9+7;
bool judge(int x)
{
if(x<=1)return 0;
else if(x==2)return 1;
int up=sqrt(x);
for(int i=2;i<=up;i++)
if(x%i==0)return 0;
return 1;
}
int prime[N];
int tot=0;
int cal(int n,int x)//求某个质因子的个数
{
if(n<x)return 0;
return (n/x+cal(n/x,x))%mod;
}
int main()
{
int n;
while(cin>>n)
{
tot=0;
for(int i=1;i<=n;i++)
if(judge(i))prime[++tot]=i;//暴力求<=N的质数
LL ans=1;
for(int i=1;i<=tot;i++)
{
int x=prime[i];
ans=(ans*(LL)(cal(n,x)+1))%mod; //求某个质因子的个数
}
cout<<ans<<endl;
}
return 0;
}
The end;