Given any positive integer N, you are supposed to find all of its prime factors, and write them in the format N = p1 k1 ×p2 k2 ×⋯×pm km
.
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^211171011291
// 欧拉筛选出素数+质因数分解
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <cmath>
using namespace std;
typedef long long ll;
struct node{
int x; //保存质因子
int num; //保存个数
}; //开到11就超过int表示的范围
ll prime[10005]; //保存素数通过
// 结论,一个数的质因数范围为(2,sqrt(n));
int vis[100000];
vector<node> fac;
ll n;
int main()
{
memset(vis,0,sizeof(vis));
memset(prime,0,sizeof(prime));
cin>>n;
vis[0]=vis[1]=1;
// 欧拉筛
for(int i = 2;i<=sqrt(n);i++)
{
if(vis[i]==0) prime[++prime[0]] = i;
for(int j=1;j<=prime[0]&&i*prime[j]<=sqrt(n);j++)
{
vis[i*prime[j]]=1;
if(i%prime[j]==0) break;
}
}
// 寻找质因数
ll t = n;
for(int i = 1;i<=prime[0]&&prime[i]<=sqrt(n);i++)
{
if(t%prime[i]==0) //质因数
{
node tem;
tem.num = 0;
tem.x = prime[i];
while(t%prime[i]==0)
{
tem.num++;
t/=prime[i];
}
fac.push_back(tem);
}
if(t==1) break;
}
cout<<n<<"=";
if(fac.size()==0)
{
printf("%lld\n",n);
return 0;
}
for(int i = 0;i<fac.size();i++)
{
if(fac[i].num==1) printf("%d",fac[i].x);
else printf("%d^%d",fac[i].x,fac[i].num);
if(i!=fac.size()-1) printf("*");
}
return 0;
}