阶乘分解质因子指数和

The factorial function, n! = 1·2·...·n, has many interesting properties. In this problem, we want to determine the maximum number of integer terms (excluding 1) that can be used to express n!. For example: 8! = 1·2·3·4·5·6·7·8 = 2·3·2·2·5·3·2·7·2·2·2 = 27 ·32 ·5·7 By inspection, it is clear that the maximum number of terms (excluding 1) that can be multiplied together to produce 8! is 11.

Input
The input for your program consists of a series of test cases on separate lines, ended by end-of-file. Each line contains one number, n, 2 ≤ n ≤ 1000000.

Output
For each test case, print the maximum number of factors (excluding 1) that can be multiplied together to produce n!. Put the output from each test case on a separate line, starting in the first column.

Sample Input
2

1000000

1996

5

8

123456

 

Sample Output
1

3626619

5957

5

11

426566

 

 

#include<iostream>
#include<cstring>
#include<cmath>
#include<cstdio>
#define N 1000010
using namespace std;
bool vis[N];
int val[N];
int prime[N];
int ans[N];
int pn=0;
void gp()
{
    for (int i = 2; i < N; i++) {
        if (vis[i]) continue;
        prime[pn++] = i;
        val[i]=1;
        for (int j = i; j < N; j += i)
            vis[j]=1;
    }
}
int main()
{
    gp();
    for(int i=2;i<=1000000;i++)
    {
        if(val[i]){
            ans[i]=1;
            for(int j=i*2;j<=1000000;j+=i)
            {
                if(!ans[j]&&ans[j/i])
                    ans[j]=ans[j/i]+1;
            }
        }
    }
    for(int i=3;i<=1000000;i++)
    {
        ans[i]+=ans[i-1];
    }
    int m,n;
    while(~scanf("%d",&n))
    {
        cout<<ans[n]<<endl;
    }
}

  

posted @ 2017-11-11 17:02  会飞的雅蠛蝶  阅读(251)  评论(0编辑  收藏  举报