Factors of Factorial 求因子数

题目:

You are given an integer N. Find the number of the positive divisors of N!, modulo 10^{9}+7.

约束条件:

1≤N10^{3}

输入:

The input is given from Standard Input in the following format: N

输出:Print the number of the positive divisors of N!, modulo 10^{9}+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;

}

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值