forked from algorithmzuo/algorithm-journey
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCode02_InverseSerial.java
More file actions
276 lines (246 loc) · 5.3 KB
/
Code02_InverseSerial.java
File metadata and controls
276 lines (246 loc) · 5.3 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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package class099;
// 连续数字逆元的线性递推
// 给定n、p,求1∼n中所有整数在模p意义下的乘法逆元
// 1 <= n <= 3 * 10^6
// n < p < 20000528
// 输入保证p为质数
// 测试链接 : https://www.luogu.com.cn/problem/P3811
// 请同学们务必参考如下代码中关于输入、输出的处理
// 这是输入输出处理效率很高的写法
// 提交以下的code,提交时请把类名改成"Main",可以直接通过
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.Writer;
import java.util.InputMismatchException;
// 如下代码可以通过全部测试用例
// 但是这个题卡常数比较严重
// 一般情况下不会如此卡常数
// 需要使用快读、快写增加IO效率
public class Code02_InverseSerial {
public static int MAXN = 3000001;
public static int[] inv = new int[MAXN];
public static int n, p;
public static void build(int n) {
inv[1] = 1;
for (int i = 2; i <= n; i++) {
inv[i] = (int) (p - (long) inv[p % i] * (p / i) % p);
}
}
public static void main(String[] args) {
FastReader in = new FastReader(System.in);
FastWriter out = new FastWriter(System.out);
n = in.readInt();
p = in.readInt();
build(n);
for (int i = 1; i <= n; i++) {
out.println(inv[i]);
}
out.close();
}
// 快读
public static class FastReader {
InputStream is;
private byte[] inbuf = new byte[1024];
public int lenbuf = 0;
public int ptrbuf = 0;
public FastReader(final InputStream is) {
this.is = is;
}
public int readByte() {
if (lenbuf == -1) {
throw new InputMismatchException();
}
if (ptrbuf >= lenbuf) {
ptrbuf = 0;
try {
lenbuf = is.read(inbuf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (lenbuf <= 0) {
return -1;
}
}
return inbuf[ptrbuf++];
}
public int readInt() {
return (int) readLong();
}
public long readLong() {
long num = 0;
int b;
boolean minus = false;
while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
;
if (b == '-') {
minus = true;
b = readByte();
}
while (true) {
if (b >= '0' && b <= '9') {
num = num * 10 + (b - '0');
} else {
return minus ? -num : num;
}
b = readByte();
}
}
}
// 快写
public static class FastWriter {
private static final int BUF_SIZE = 1 << 13;
private final byte[] buf = new byte[BUF_SIZE];
private OutputStream out;
private Writer writer;
private int ptr = 0;
public FastWriter(Writer writer) {
this.writer = new BufferedWriter(writer);
out = new ByteArrayOutputStream();
}
public FastWriter(OutputStream os) {
this.out = os;
}
public FastWriter(String path) {
try {
this.out = new FileOutputStream(path);
} catch (FileNotFoundException e) {
throw new RuntimeException("FastWriter");
}
}
public FastWriter write(byte b) {
buf[ptr++] = b;
if (ptr == BUF_SIZE) {
innerflush();
}
return this;
}
public FastWriter write(String s) {
s.chars().forEach(c -> {
buf[ptr++] = (byte) c;
if (ptr == BUF_SIZE) {
innerflush();
}
});
return this;
}
private static int countDigits(long l) {
if (l >= 1000000000000000000L) {
return 19;
}
if (l >= 100000000000000000L) {
return 18;
}
if (l >= 10000000000000000L) {
return 17;
}
if (l >= 1000000000000000L) {
return 16;
}
if (l >= 100000000000000L) {
return 15;
}
if (l >= 10000000000000L) {
return 14;
}
if (l >= 1000000000000L) {
return 13;
}
if (l >= 100000000000L) {
return 12;
}
if (l >= 10000000000L) {
return 11;
}
if (l >= 1000000000L) {
return 10;
}
if (l >= 100000000L) {
return 9;
}
if (l >= 10000000L) {
return 8;
}
if (l >= 1000000L) {
return 7;
}
if (l >= 100000L) {
return 6;
}
if (l >= 10000L) {
return 5;
}
if (l >= 1000L) {
return 4;
}
if (l >= 100L) {
return 3;
}
if (l >= 10L) {
return 2;
}
return 1;
}
public FastWriter write(long x) {
if (x == Long.MIN_VALUE) {
return write("" + x);
}
if (ptr + 21 >= BUF_SIZE) {
innerflush();
}
if (x < 0) {
write((byte) '-');
x = -x;
}
int d = countDigits(x);
for (int i = ptr + d - 1; i >= ptr; i--) {
buf[i] = (byte) ('0' + x % 10);
x /= 10;
}
ptr += d;
return this;
}
public FastWriter writeln(long x) {
return write(x).writeln();
}
public FastWriter writeln() {
return write((byte) '\n');
}
private void innerflush() {
try {
out.write(buf, 0, ptr);
ptr = 0;
} catch (IOException e) {
throw new RuntimeException("innerflush");
}
}
public void flush() {
innerflush();
try {
if (writer != null) {
writer.write(((ByteArrayOutputStream) out).toString());
out = new ByteArrayOutputStream();
writer.flush();
} else {
out.flush();
}
} catch (IOException e) {
throw new RuntimeException("flush");
}
}
public FastWriter println(long x) {
return writeln(x);
}
public void close() {
flush();
try {
out.close();
} catch (Exception e) {
}
}
}
}