Given any positive integer N, you are supposed to find all of its prime factors, and write them in the format N = p1k1×p2k2×⋯×pmkm.
Input Specification:
Each input file contains one test case which gives a positive integer N in the range of long int.
Output Specification:
Factor N in the format N =
p1^
k1*
p2^
k2*
…*
pm^
km, where pi’s are prime factors of N in increasing order, and the exponent ki is the number of pi – hence when there is only one pi, ki is 1 and must NOT be printed out.
Sample Input:
97532468
Sample Output:
97532468=2^2*11*17*101*1291
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
struct fractor{
ll x, c;
}fac[11];
ll c;
int main(){
ll n; scanf("%lld", &n);
ll sqr = sqrt(n), oldn = n;
for(ll i = 2; i < sqr; i++)
if(n % i == 0){
while(n % i == 0){
fac[c].c++;
n /= i;
}
fac[c++].x = i;
}
if(n != 1){
fac[c].x = n;
fac[c++].c++;
}
printf("%lld=", oldn);
for(ll i = 0; i < c; i++){
if(i!=0) printf("*");
fac[i].c==1 ? printf("%lld", fac[i].x) : printf("%lld^%lld", fac[i].x, fac[i].c);
}
if(oldn==1) printf("1");
return 0;
}
总结
#数学问题/质因子分解
n==1
需要特判输出1=1
,否则测试点3报错。