-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1059.cpp
59 lines (59 loc) · 830 Bytes
/
1059.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include<stdio.h>
#include<vector>
#include<math.h>
using namespace std;
bool isPrime(long n)
{
long m=(long)sqrt(n);
for(long i=2;i<=m;i++)
if(n%i == 0)
return false;
return true;
}
int main()
{
vector<long> pf;
vector<long> kf;
long n;
scanf("%ld",&n);
if(n==1)
{
printf("1=1");
return 0;
}
long nn=n;
long i=2;
int top=-1;
for(i=2;i<=nn && nn!=1;i++)
{
if(!isPrime(i))
continue;
bool newF=true;
while(i<=nn && nn%i == 0)
{
if(newF)
{
newF=false;
pf.push_back(i);
kf.push_back(1);
top++;
}
else
{
kf[top]=kf[top]+1;
}
nn/=i;
}
}
printf("%ld=",n);
if(kf[0]==1)
printf("%ld",pf[0]);
else
printf("%ld^%ld",pf[0],kf[0]);
for(int j=1;j<pf.size();j++)
if(kf[j]==1)
printf("*%ld",pf[j]);
else
printf("*%ld^%ld",pf[j],kf[j]);
return 0;
}