题目:
You are given an integer N. Find the number of the positive divisors of N!, modulo +7.
约束条件:
1≤N≤
输入:
The input is given from Standard Input in the following format: N
输出:Print the number of the positive divisors of N!, modulo +7.
样例输入1:
3
样例输出1:
4
样例输入2:
6
样例输出2:
30
样例输入3:
1000
样例输出3:
9729269729
题意就是给出一个数n,求n的阶乘的所有因子个数。暴力肯定会超时,而且n特别大的时候数也不好存。这时可以想到唯一分解定理中有求因子个数的方法。下图第一条。
再进一步考虑,当n为3时,1的因子个数是1,2的因子个数是2,3的因子个数是2,而6=1*2*3的因子个数是4。所以我们就不必把n的阶乘这个数算出来,把2~n每个数的因子个数求出来相乘就好了。为什么不从1开始呢,因为1的因子个数就是1,最后定义sum时让sum为1,就相当于考虑1的情况了。
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const int maxn=1e9+7;
const int maxx=1e7;
int a[maxx];
int main()
{
int n;
scanf("%d",&n);
int c=0;
for(int i=2;i<=n;i++)
{
int l=i;
for(int j=2;j<=l;j++)
{
while(l%j==0)
{
a[j]++;//把每个数每个素因子的个数存起来
l/=j;
}
}
}
long long sum=1;
for(int i=2;i<=n;i++)
{
if(a[i])
{
sum*=(1+a[i]);//公式
sum=sum%maxn;
}
}
printf("%lld\n",sum%maxn);
return 0;
}