n! decomposition of the element factor

Source: Internet
Author: User

n! = p1^t1*p2^t2* ... .. Pi^ti ... .. *PK^TK (where P1 , P2 ... PK is the prime number, 1<n<= 10^6 )

Obviously, we first have to sift out all primes that are less than or equal to N.

And then we're going to divide N into two parts,

1) is a multiple of a factor, 2) is not a multiple of this factor

Example: F (n,2)

n!= (2*4*6*....N) * (1*3*5*.....* (n-1))

=2^ (N/2) * (1*2*3*4*......N/2) * (1*3*5*.  (n-1)) Got a

......

So we get the number of times a recursive formula has a factor f (n,p) = f (n/p,p) + n/p;


The following one also speaks about the promotion of this issue

Http://www.cnblogs.com/openorz/archive/2011/11/14/2248992.html


The code is as follows:

#include <iostream> #include <cstdio> #include <algorithm> #include <cstring>using namespace std;const int maxn = 10000010;int prime[maxn],cnt;int ans[maxn];bool is[maxn];void init (int n) {    cnt=0;    Memset (Is,1,sizeof (IS));    memset (ans,0,sizeof (ans));    memset (prime,0,sizeof (Prime));    for (int i=2;i<=n;i++) {        if (Is[i]) {            prime[cnt++]=i;            for (int j=i+i;j<=n;j+=i)                is[j]=0;}}    } int fen (int n,int p) {    if (n==0) return 0;    Return Fen (n/p,p) +n/p;} int main () {    int n;    while (~SCANF ("%d", &n)) {        init (n);        for (int i=0;i<cnt;i++)            Ans[i]=fen (N,prime[i]);        for (int i=0;i<cnt;i++)            printf ("%d^%d\n", Prime[i],ans[i]);    }    return 0;}


n! decomposition of the element factor

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.