-
Notifications
You must be signed in to change notification settings - Fork 0
/
884.java
126 lines (101 loc) · 2.96 KB
/
884.java
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import java.util.*;
import java.io.*;
class Main
{
private static final int N = 1000000;
private static int[] mark = new int[N + 2];
private static int[] pow = new int[N + 2];
private static int[] ans = new int[N + 2];
private static Vector<Integer> primeList = new Vector<Integer>();
private static void seive()
{
Arrays.fill(mark, 1);
mark[1] = 0;
primeList.add(2);
int limit = (int) Math.sqrt(N);
pow[2] = 1;
for(int i = 3; i <=N; i += 2)
{
if(mark[i] == 1)
{
primeList.add(i);
pow[i] = 1;
if(i<=limit)
for(int j = i * i; j <= N; j += i * 2)
{
// pow[j] = pow[i] + pow[j / i];
mark[j] = 0;
}
}
}
for(int i = 0; i < primeList.size(); i++)
{
int p = primeList.get(i);
if(p<=limit+2)
for(int j = p * p; j <= N; j += p)
{
pow[j] = pow[j / p] + 1;
}
}
}
private static void factorialPow()
{
ans[2] = 1;
for(int i = 3; i <=N; i++)
ans[i] = ans[i - 1] + pow[i];
}
public static void main(String[] args) throws Exception
{
seive();
factorialPow();
int n = 100;
Scanner read = new Scanner(System.in);
// Scanner read = FileFactory.read();
// BufferedWriter write =FileFactory.write();
while(read.hasNext())
{
int num = read.nextInt();
//write.write(Integer.toString(ans[num]));
//write.write("\n");
System.out.println(ans[num]);
}
//FileFactory.close();
read.close();
}
static class FileFactory {
private FileFactory() {
try {
reader = new FileReader("in.txt");
buffer = new BufferedReader(reader);
scanner = new Scanner(buffer);
writer = new FileWriter("out.txt");
bufferWriter = new BufferedWriter(writer);
} catch (Exception ex) {
System.out.println(ex.getCause());
}
}
public static Scanner read() {
return instanceFactory.scanner;
}
public static BufferedWriter write() {
return instanceFactory.bufferWriter;
}
public static void close() {
try {
instanceFactory.scanner.close();
instanceFactory.buffer.close();
instanceFactory.reader.close();
instanceFactory.bufferWriter.close();
instanceFactory.writer.close();
} catch (Exception ex) {
System.out.println(ex.getCause());
}
}
private static FileFactory instanceFactory = new FileFactory();
private FileReader reader;
private BufferedReader buffer;
private Scanner scanner;
private FileWriter writer;
private BufferedWriter bufferWriter;
}
}