1059 Prime Factors (25 分)
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.
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.
97532468
Sample Output:
97532468=2^2*11*17*101*1291素数筛,可能也有点超纲。。。但是也不难。
1 #include <bits/stdc++.h>
2 #define mod 2000005
3 #define ll long long
4 using namespace std;
5 int an[mod], prime[mod];
6 vector<
int>
v,vt;
7 int pos =
0;
8 void init(){
9 an[
0] = an[
1] =
1;
10 for(
int i=
2; i < mod; i++
){
11 if(an[i] ==
0){
12 prime[pos++] =
i;
13 for(
int j =
2; j*i < mod; j++
)
14 an[i*j] =
1;
15 }
16 }
17 }
18
19 ll n;
20
21 int main(){
22 init();
23
24 cin >>
n;
25 if(n ==
1){
26 cout <<n<<
"="<<n<<
endl;
27 return 0;
28 }
29 cout <<n<<
"=";
30 for(
int i =
0; i < pos; i++
){
31 if(n%prime[i] ==
0){
32 int count =
0;
33 while(n%prime[i] ==
0){
34 n /=
prime[i];
35 count++
;
36 if(n ==
0)
37 break;
38 }
39 v.push_back(prime[i]);
40 vt.push_back(count);
41 }
42 if(n ==
1){
43 break;
44 }
45 }
46 for(
int i =
0; i < v.size(); i++
){
47 if(vt[i] ==
1){
48 cout <<
v[i];
49 }
else{
50 cout << v[i] <<
"^"<<
vt[i];
51 }
52 printf(
"%c",i == v.size()-
1?
'\n':
'*');
53 }
54
55 return 0;
56 }
转载于:https://www.cnblogs.com/zllwxm123/p/11190910.html
相关资源:各显卡算力对照表!