Link: http://pat.zju.edu.cn/contests/ds/2-07
Given a positive integer N, obtain the factorization result of the prime factor, that is, the factorization expression n = p1 ^ K1 * P2 ^ K2 *... * PM ^ km.
Input format description:
Enter a positive integer N in the long int range.
Output format description:
Output n factorization expressions in the given format, that is, n = p1 ^ K1 * P2 ^ K2 *... * PM ^ km, where Pi is a prime factor and must be output from small to large. The exponent Ki is the number of Pi. When Ki = 1, that is, if the factor Pi has only one, no Ki is output.
Sample input and output:
Serial number |
Input |
Output |
1 |
1024 |
1024=2^10 |
2 |
1323 |
1323=3^3*7^2 |
3 |
97532468 |
97532468=2^2*11*17*101*1291 |
4 |
1 |
1=1 |
5 |
3 |
3=3 |
The Code is as follows:
#include <cstdio>#include <cstring>#include <cmath>#define LL intconst int MAXN = 11117;int main(){ LL n; int p[MAXN], k[MAXN]; while(~scanf("%d",&n)) { LL tt = n; memset(p,0,sizeof(p)); memset(k,0,sizeof(k)); int i = 2; int cont = 0; int flag = 0; int l = 0; if(n == 1) { printf("1=1\n"); continue; } while(n!=1) { while(n%i==0) { flag = 1; cont++; n /= i; } if(flag) { p[l] = i; k[l] = cont; l++; flag = 0; cont = 0; } i++; } printf("%d=",tt); for(int i = 0; i < l-1; i++) { if(k[i] == 1) printf("%d*",p[i]); else printf("%d^%d*",p[i],k[i]); } if(k[l-1] == 1) printf("%d\n",p[l-1]); else printf("%d^%d\n",p[l-1],k[l-1]); } return 0;}
2-07. prime factor decomposition (20) (zjupat mathematics)