Description
给你一个数 n (1 < n <= 1000000) ,求 n! (n的阶乘)的质因数分解形式,质因数分解形式为 n=p1^m1*p2^m2*p3^m3…… * 这里 p1 < p2 < p3 < …… 为质数 * 如果 mi = 1, 则 ^ mi 就不需要输出Input
输入是多case的,每行一个数n,1 < n <= 1000000,当n等于0时输入结束Output
每个n输出一行,为它的质因数分解形式Sample Input
6 7 0Sample Output
6=2^4*3^2*5 7=2^4*3^2*5*7Author
windy7926778
#include<iostream> #include<cmath> #include<cstring> using namespace std; int isprime[1010000]; long long prime[1010000]; int pnum[1010000]; int cnt; void getP() { for(int i = 1; i < 1010000; i++) isprime[i] = 1; for(int i = 2; i < 1010000; i++) { if(!isprime[i])continue; prime[cnt++] = i; for(int j = 2 * i; j < 1010000; j += i) isprime[j] = 0; } } long long get( long long n ,long long key) { int tem = key; long long num = 0; while(n >= key) { num += n / key; key *= tem; } return num; } int main() { long long n; getP(); while(cin >> n &&n) { if(n == 1) { cout << "1=1" << endl; continue; } memset( pnum, 0, sizeof pnum ); for(int i = 0; prime[i] <= n&&prime[i]; i++) { pnum[i] = get( n, prime[i] ); } cout << n << "="; bool fst = 0; for(int i = 0; prime[i] <= n&&prime[i]; i++) { if(!fst) fst = true; else cout << "*"; if(pnum[i]) cout << prime[i]; if(pnum[i]>=2) cout << "^" << pnum[i]; } cout << endl; } return 0; }
原文地址:http://blog.csdn.net/maxichu/article/details/45595631