码迷,mamicode.com
首页 > 其他好文 > 详细

阶乘分解质因子指数和

时间:2017-11-11 17:39:47      阅读:216      评论:0      收藏:0      [点我收藏+]

标签:factorial   nbsp   res   scan   -o   cout   cstring   cto   example   

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-?le. 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 ?rst 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;
    }
}

  

阶乘分解质因子指数和

标签:factorial   nbsp   res   scan   -o   cout   cstring   cto   example   

原文地址:http://www.cnblogs.com/ygtzds/p/7819452.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!