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