-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem0197.h
More file actions
54 lines (48 loc) · 1.25 KB
/
Problem0197.h
File metadata and controls
54 lines (48 loc) · 1.25 KB
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
//
// Created by Fengwei Zhang on 1/10/22.
//
#ifndef ACWINGSOLUTION_PROBLEM0197_H
#define ACWINGSOLUTION_PROBLEM0197_H
#include <iostream>
#include <cstring>
using namespace std;
class Problem0197 {
private:
int CountPrimes(const int n, int primes[], bool filtered[]) {
int cnt = 0;
for (int f = 2; f <= n; ++f) {
if (!filtered[f]) {
primes[cnt++] = f;
}
for (int i = 0; primes[i] <= n / f; ++i) {
filtered[primes[i] * f] = true;
if (f % primes[i] == 0) {
break;
}
}
}
return cnt;
}
int main() {
int n;
scanf("%d", &n);
int primes[n + 1];
bool filtered[n + 1];
memset(primes, 0, sizeof primes);
memset(filtered, 0, sizeof filtered);
auto cnt = CountPrimes(n, primes, filtered);
for (int i = 0; i < cnt; ++i) {
auto p = primes[i];
int s = 0;
for (int j = n; j > 0; j /= p) {
s += j / p;
}
if (s == 0) {
continue;
}
printf("%d %d\n", p, s);
}
return 0;
}
};
#endif //ACWINGSOLUTION_PROBLEM0197_H