-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathatom.xml
459 lines (233 loc) · 727 KB
/
atom.xml
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title>恁村扛把子</title>
<subtitle>天行健君子以自强不息, 地势坤君子以厚德载物!</subtitle>
<link href="/atom.xml" rel="self"/>
<link href="http://yoursite.com/"/>
<updated>2020-07-10T01:07:40.910Z</updated>
<id>http://yoursite.com/</id>
<author>
<name>fangsong</name>
</author>
<generator uri="https://hexo.io/">Hexo</generator>
<entry>
<title>Mynotes/计算机组成原理/计算机运算方法</title>
<link href="http://yoursite.com/2020/07/10/Mynotes/%E8%AE%A1%E7%AE%97%E6%9C%BA%E7%BB%84%E6%88%90%E5%8E%9F%E7%90%86/%E8%AE%A1%E7%AE%97%E6%9C%BA%E8%BF%90%E7%AE%97%E6%96%B9%E6%B3%95/"/>
<id>http://yoursite.com/2020/07/10/Mynotes/%E8%AE%A1%E7%AE%97%E6%9C%BA%E7%BB%84%E6%88%90%E5%8E%9F%E7%90%86/%E8%AE%A1%E7%AE%97%E6%9C%BA%E8%BF%90%E7%AE%97%E6%96%B9%E6%B3%95/</id>
<published>2020-07-10T01:07:40.910Z</published>
<updated>2020-07-10T01:07:40.910Z</updated>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><h1 id="循环冗余校验码"><a href="#循环冗余校验码" class="headerlink" title="循环冗余校验码"></a>循环冗余校验码</h1><p><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1gg7041egvgj31380ogqh6.jpg" alt="image.png"></p><p><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1gg706yhnubj311g0g2gq3.jpg" alt="image.png"></p><p><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1gg708ncdqpj30yl08bjwr.jpg" alt="image.png"></p><h1 id="定点数的表示与运算"><a href="#定点数的表示与运算" class="headerlink" title="定点数的表示与运算"></a>定点数的表示与运算</h1><p><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1gg70bbv0rhj314m0jntf9.jpg" alt="image.png"></p><h2 id="无符号数及其原码"><a href="#无符号数及其原码" class="headerlink" title="无符号数及其原码"></a>无符号数及其原码</h2><p><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1gg70bzdt5lj314902p418.jpg" alt="image.png"></p><p><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1gg70d4rri5j30h007ztbs.jpg" alt="image.png"></p><p><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1gg70gzbtalj319p0q2dzo.jpg" alt="image.png"></p><p><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1gg70k9l14fj31840qxtv0.jpg" alt="image.png"></p><p><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1gg70lpx25jj31790fzwr8.jpg" alt="image.png"></p><h2 id="补码反码移码"><a href="#补码反码移码" class="headerlink" title="补码反码移码"></a>补码反码移码</h2><h3 id="补码"><a href="#补码" class="headerlink" title="补码"></a>补码</h3><p><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1gg70u6kryqj31gm0m3nec.jpg" alt="image.png"></p><h4 id="纯整数补码"><a href="#纯整数补码" class="headerlink" title="纯整数补码"></a>纯整数补码</h4><p><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1gg70xjsmpej30x90ewtgy.jpg" alt="image.png"></p><h4 id="纯小数补码"><a href="#纯小数补码" class="headerlink" title="纯小数补码"></a>纯小数补码</h4><p><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1gg70zc3zctj30vi0eiahi.jpg" alt="image.png"></p><h3 id="反码"><a href="#反码" class="headerlink" title="反码"></a>反码</h3><p><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1gg710xb1a0j30zj0n016u.jpg" alt="image.png"></p><h3 id="三码转换"><a href="#三码转换" class="headerlink" title="三码转换"></a>三码转换</h3><p><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1gg7120gb2gj31180lytgv.jpg" alt="image.png"></p><h3 id="移码"><a href="#移码" class="headerlink" title="移码"></a>移码</h3><p><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1gg714t2i9lj31cp0qw1kx.jpg" alt="image.png"></p><h2 id="移位运算"><a href="#移位运算" class="headerlink" title="移位运算"></a>移位运算</h2><p><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1gg718xjmc7j31el0maarv.jpg" alt="image.png"></p><h4 id="有无符号移位"><a href="#有无符号移位" class="headerlink" title="有无符号移位"></a>有无符号移位</h4><p><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1gg71cw5lpsj314c0ljk36.jpg" alt="image.png"></p><p><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1gg71dkklz8j31870n6qe1.jpg" alt="image.png"></p><h4 id="循环移位"><a href="#循环移位" class="headerlink" title="循环移位"></a>循环移位</h4><p><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1gg71eevmezj31330i6q91.jpg" alt="image.png"></p><h2 id="加减运算-符号扩展-溢出判断"><a href="#加减运算-符号扩展-溢出判断" class="headerlink" title="加减运算/符号扩展/溢出判断"></a>加减运算/符号扩展/溢出判断</h2><p>###加减</p><p><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1gg71nj3lfzj31270fmn4w.jpg" alt="image.png"></p><h3 id="溢出"><a href="#溢出" class="headerlink" title="溢出"></a>溢出</h3><p><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1gg71p6swt4j310p0kmgtl.jpg" alt="image.png"></p><h2 id="乘"><a href="#乘" class="headerlink" title="乘"></a>乘</h2><p><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1gg71wytriij318k0rc1br.jpg" alt="image.png"></p><p><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1gg721otn3nj31ba0s5npd.jpg" alt="image.png"></p><h2 id="除"><a href="#除" class="headerlink" title="除"></a>除</h2><p><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1gg733gnjuoj31970ryttx.jpg" alt="image.png"></p><p><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1gg737dnleej315e0nn7pk.jpg" alt="image.png"></p><p><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1gg737zj0a6j315n0cnafr.jpg" alt="image.png"></p><h2 id="强制类型转换"><a href="#强制类型转换" class="headerlink" title="强制类型转换"></a>强制类型转换</h2><p><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1gg72uuowhtj319a0oce1r.jpg" alt="image.png"></p><h1 id="浮点数的表示"><a href="#浮点数的表示" class="headerlink" title="浮点数的表示"></a>浮点数的表示</h1><p><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1gg741ot3c4j30yc0h479d.jpg" alt="image.png"></p>]]></content>
<summary type="html">
<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" cla
</summary>
</entry>
<entry>
<title>Mynotes/计算机组成原理/指令系统</title>
<link href="http://yoursite.com/2020/07/10/Mynotes/%E8%AE%A1%E7%AE%97%E6%9C%BA%E7%BB%84%E6%88%90%E5%8E%9F%E7%90%86/%E6%8C%87%E4%BB%A4%E7%B3%BB%E7%BB%9F/"/>
<id>http://yoursite.com/2020/07/10/Mynotes/%E8%AE%A1%E7%AE%97%E6%9C%BA%E7%BB%84%E6%88%90%E5%8E%9F%E7%90%86/%E6%8C%87%E4%BB%A4%E7%B3%BB%E7%BB%9F/</id>
<published>2020-07-10T01:07:40.760Z</published>
<updated>2020-07-10T01:07:40.760Z</updated>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><h1 id="指令系统"><a href="#指令系统" class="headerlink" title="指令系统"></a>指令系统</h1><p><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1ggbuk9g2uvj30zn0gegsx.jpg" alt="image.png"></p><h2 id="指令的定义"><a href="#指令的定义" class="headerlink" title="指令的定义"></a>指令的定义</h2><p><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1ggbv1l69jaj31860l14pk.jpg" alt="image.png"></p><p><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1ggbxbwpqa0j316c0qrgzo.jpg" alt="image.png"></p><p><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1ggbxh4o6t5j31bp0re4qp.jpg" alt="image.png"></p>]]></content>
<summary type="html">
<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" cla
</summary>
</entry>
<entry>
<title>Mynotes/计算机组成原理/存储系统</title>
<link href="http://yoursite.com/2020/07/10/Mynotes/%E8%AE%A1%E7%AE%97%E6%9C%BA%E7%BB%84%E6%88%90%E5%8E%9F%E7%90%86/%E5%AD%98%E5%82%A8%E7%B3%BB%E7%BB%9F/"/>
<id>http://yoursite.com/2020/07/10/Mynotes/%E8%AE%A1%E7%AE%97%E6%9C%BA%E7%BB%84%E6%88%90%E5%8E%9F%E7%90%86/%E5%AD%98%E5%82%A8%E7%B3%BB%E7%BB%9F/</id>
<published>2020-07-10T01:07:40.745Z</published>
<updated>2020-07-10T01:07:40.746Z</updated>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><h1 id="存储系统"><a href="#存储系统" class="headerlink" title="存储系统"></a>存储系统</h1><p><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1gg75tro9zpj30h20cpn0s.jpg" alt="image.png"></p><h2 id="主存简单模型及其寻址的概念"><a href="#主存简单模型及其寻址的概念" class="headerlink" title="主存简单模型及其寻址的概念"></a>主存简单模型及其寻址的概念</h2><h3 id="存储芯片的基本结构"><a href="#存储芯片的基本结构" class="headerlink" title="存储芯片的基本结构"></a>存储芯片的基本结构</h3><p><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1gg75xhcxc5j31e80mhnjk.jpg" alt="image.png"></p><p><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1gg76bigmntj31ck0neh9y.jpg" alt="image.png"></p><h3 id="主存与CPU连接"><a href="#主存与CPU连接" class="headerlink" title="主存与CPU连接"></a>主存与CPU连接</h3><h4 id="主存容量扩展-位扩展"><a href="#主存容量扩展-位扩展" class="headerlink" title="主存容量扩展-位扩展"></a>主存容量扩展-位扩展</h4><p><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1gg76hz6rouj314b0qlqdi.jpg" alt="image.png"></p><p><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1gg76hfcrygj31cg0mun6u.jpg" alt="image.png"></p><h4 id="主存容量扩展-字扩展"><a href="#主存容量扩展-字扩展" class="headerlink" title="主存容量扩展-字扩展"></a>主存容量扩展-字扩展</h4><p>不能同时工作,交替工作</p><p><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1gg76lihrjuj31640mnti7.jpg" alt="image.png"></p><p><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1gg76muy36ej311e0mqthh.jpg" alt="image.png"></p><p>或者:</p><p><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1gg76re2arbj31db0pj7m6.jpg" alt="image.png"></p><h4 id="主存容量扩展-译码器"><a href="#主存容量扩展-译码器" class="headerlink" title="主存容量扩展-译码器"></a>主存容量扩展-译码器</h4><p> <img src="http://ww1.sinaimg.cn/large/006Uqzbtly1gg76x15crvj314u0nfanv.jpg" alt="image.png"></p><p><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1gg7701qdy7j31e30nqaou.jpg" alt="image.png"></p><p><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1gg76zkigmqj31fr0o6k8f.jpg" alt="image.png"></p><h3 id="主存与CPU连接-例题"><a href="#主存与CPU连接-例题" class="headerlink" title="主存与CPU连接-例题"></a>主存与CPU连接-例题</h3><p><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1gg776upxz7j317c09g7h1.jpg" alt="image.png"></p><p><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1gg79oy3o7fj31a90h74g6.jpg" alt="image.png"></p><p><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1gg79pcqvcuj315t0mf7et.jpg" alt="image.png"></p><h3 id="双口RAM和多模块存储器"><a href="#双口RAM和多模块存储器" class="headerlink" title="双口RAM和多模块存储器"></a>双口RAM和多模块存储器</h3><p><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1gg9krs3v3xj31420or48g.jpg" alt="image.png"></p><p><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1gg9l0uwhejj31640pn1cw.jpg" alt="image.png"></p><p><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1gg9lbbx4hbj31gh0sctuy.jpg" alt="image.png"></p><p><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1gg9liooigrj31gs0t31kx.jpg" alt="image.png"></p><p><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1gg9lqtizglj31cy0rs4qp.jpg" alt="image.png"></p><p><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1gg9mzsssj7j31010mo7cw.jpg" alt="image.png"></p><h3 id="高速缓冲存储器"><a href="#高速缓冲存储器" class="headerlink" title="高速缓冲存储器"></a>高速缓冲存储器</h3><p><strong>局部性原理性能分析</strong></p><p><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1gga65rf69sj31gv0ruqrw.jpg" alt="image.png"></p><p><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1ggbahsgtsyj31dv0nmx04.jpg" alt="image.png"></p><p><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1ggbaj4dcxhj31eu0nz4qp.jpg" alt="image.png"></p><h3 id="Cache工作原理"><a href="#Cache工作原理" class="headerlink" title="Cache工作原理"></a>Cache工作原理</h3><p><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1ggbb6coulyj31d00n77hs.jpg" alt="image.png"></p><h4 id="全相联映射"><a href="#全相联映射" class="headerlink" title="全相联映射"></a>全相联映射</h4><p><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1ggbatl7cpxj31gl0r11kx.jpg" alt="image.png"></p><h4 id="直接映射"><a href="#直接映射" class="headerlink" title="直接映射"></a>直接映射</h4><p><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1ggbawy806xj31gw0pg1kx.jpg" alt="image.png"></p><h4 id="组相联映射"><a href="#组相联映射" class="headerlink" title="组相联映射"></a>组相联映射</h4><p><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1ggbb0tchcwj31h10n71kx.jpg" alt="image.png"></p><p>#### 三者对比</p><p><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1ggbb4vnqepj31go0nm4hr.jpg" alt="image.png"></p><h4 id="替换算法"><a href="#替换算法" class="headerlink" title="替换算法"></a>替换算法</h4><p><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1ggbb97wjf3j317w0n9e6d.jpg" alt="image.png"></p><h3 id="虚拟存储器"><a href="#虚拟存储器" class="headerlink" title="虚拟存储器"></a>虚拟存储器</h3><p><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1ggbbx5w96cj319x0l97gd.jpg" alt="image.png"></p><p><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1ggbbho6g1sj30bx0bpgqk.jpg" alt="image.png"></p><p><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1ggbbkpka04j31c00manac.jpg" alt="image.png"></p><p><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1ggbbo2ftrwj31ay0p312a.jpg" alt="image.png"></p><p><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1ggbbpf1m0hj315v0ra4cd.jpg" alt="image.png"></p><p><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1ggbbqyy0hsj30ox0cqn5b.jpg" alt="image.png"></p><p><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1ggbbst6344j317f0qunbl.jpg" alt="image.png"></p><p><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1ggbbvmx5hfj315j0nsnjv.jpg" alt="image.png"></p><h3 id="半导体存储器"><a href="#半导体存储器" class="headerlink" title="半导体存储器"></a>半导体存储器</h3><p><img src="http://ww1.sinaimg.cn/mw690/006Uqzbtly1ggbc1jilq3j30yv0r1avt.jpg" alt="image.png"></p><h4 id="RAM"><a href="#RAM" class="headerlink" title="RAM"></a>RAM</h4><p><img src="http://ww1.sinaimg.cn/mw690/006Uqzbtly1ggbc8q2y7vj31f40rh1kx.jpg" alt="image.png"></p><p><img src="http://ww1.sinaimg.cn/mw690/006Uqzbtly1ggbcbxu1hqj31f50s8h8c.jpg" alt="image.png"></p><p><img src="http://ww1.sinaimg.cn/mw690/006Uqzbtly1ggbcg0gfnqj31gu0izayx.jpg" alt="image.png"></p><p><img src="http://ww1.sinaimg.cn/mw690/006Uqzbtly1ggbckgi6qcj31ed0qkqic.jpg" alt="image.png"></p><p><img src="http://ww1.sinaimg.cn/mw690/006Uqzbtly1ggbclradwnj31gn0qq7f1.jpg" alt="image.png"></p><p><img src="http://ww1.sinaimg.cn/mw690/006Uqzbtly1ggbcld07cbj31do0rdqj3.jpg" alt="image.png"></p><h4 id="ROM"><a href="#ROM" class="headerlink" title="ROM"></a>ROM</h4><p><img src="http://ww1.sinaimg.cn/mw690/006Uqzbtly1ggbcpvv4rjj319p0ra4h9.jpg" alt="image.png"></p><p><img src="http://ww1.sinaimg.cn/mw690/006Uqzbtly1ggbcqfp6klj30v40gjwki.jpg" alt="image.png"></p><p><img src="http://ww1.sinaimg.cn/mw690/006Uqzbtly1ggbcrz4ealj313x0jydwe.jpg" alt="image.png"></p><p>### 存储器的总结</p><p><img src="http://ww1.sinaimg.cn/mw690/006Uqzbtly1ggbcwg901sj31h30nnto9.jpg" alt="image.png"></p><p><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1ggbcy5u6o8j31340qbqnx.jpg" alt="image.png"></p><p><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1ggbd0f2u64j313o0qwk4a.jpg" alt="image.png"></p><p><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1ggbczrxx16j311d0ps4da.jpg" alt="image.png"></p>]]></content>
<summary type="html">
<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" cla
</summary>
</entry>
<entry>
<title>Mynotes/Python/Python基础/README</title>
<link href="http://yoursite.com/2020/07/10/Mynotes/Python/Python%E5%9F%BA%E7%A1%80/README/"/>
<id>http://yoursite.com/2020/07/10/Mynotes/Python/Python%E5%9F%BA%E7%A1%80/README/</id>
<published>2020-07-10T01:07:38.875Z</published>
<updated>2020-07-10T01:07:38.875Z</updated>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><p>[TOC]</p><h1 id="Python基础"><a href="#Python基础" class="headerlink" title="Python基础"></a>Python基础</h1><h3 id="Python简介"><a href="#Python简介" class="headerlink" title="Python简介"></a>Python简介</h3><blockquote><p>Python 被称为“胶水”语言,可以同其他语言粘合着用。</p></blockquote><h4 id="Python的历史"><a href="#Python的历史" class="headerlink" title="Python的历史"></a>Python的历史</h4><blockquote><ol><li>1989年圣诞节:Guido von Rossum开始写Python语言的编译器。</li><li>1991年2月:第一个Python编译器(同时也是解释器)诞生,它是用C语言实现的(后面),可以调用C语言的库函数。在最早的版本中,Python已经提供了对“类”,“函数”,“异常处理”等构造块的支持,还有对列表、字典等核心数据类型,同时支持以模块为基础来构造应用程序。</li><li>1994年1月:Python 1.0正式发布。</li><li>2000年10月16日:Python 2.0发布,增加了完整的<a href="https://zh.wikipedia.org/wiki/垃圾回收_(計算機科學)" target="_blank" rel="noopener">垃圾回收</a>,提供了对<a href="https://zh.wikipedia.org/wiki/Unicode" target="_blank" rel="noopener">Unicode</a>的支持。与此同时,Python的整个开发过程更加透明,社区对开发进度的影响逐渐扩大,生态圈开始慢慢形成。</li><li>2008年12月3日:Python 3.0发布,它并不完全兼容之前的Python代码,不过因为目前还有不少公司在项目和运维中使用Python 2.x版本,所以Python 3.x的很多新特性后来也被移植到Python 2.6/2.7版本中。</li></ol></blockquote><p>目前我们使用的<code>Python 3.7.x</code>的版本是在2018年发布的,Python的版本号分为三段,形如A.B.C。其中A表示大版本号,一般当整体重写,或出现不向后兼容的改变时,增加A;B表示功能更新,出现新功能时增加B;C表示小的改动(例如:修复了某个Bug),只要有修改就增加C。如果对Python的历史感兴趣,可以阅读名为<a href="http://www.cnblogs.com/vamei/archive/2013/02/06/2892628.html" target="_blank" rel="noopener">《Python简史》</a>的网络文章。</p><h4 id="Python的优缺点"><a href="#Python的优缺点" class="headerlink" title="Python的优缺点"></a>Python的优缺点</h4><blockquote><p>Python的优点很多,简单的可以总结为以下几点。</p><ol><li>简单明了,学习曲线低,比很多编程语言都容易上手。</li><li>开放源代码,拥有强大的社区和生态圈,尤其是在数据分析和机器学习领域。</li><li>解释型语言,天生具有平台可移植性,代码可以工作于不同的操作系统。</li><li>对两种主流的编程范式(面向对象编程和函数式编程)都提供了支持。</li><li>代码规范程度高,可读性强,适合有代码洁癖和强迫症的人群。</li></ol><p>Python的缺点主要集中在以下几点。</p><ol><li>执行效率稍低,对执行效率要求高的部分可以由其他语言(如:C、C++)编写。</li><li>代码无法加密,但是现在很多公司都不销售卖软件而是销售服务,这个问题会被弱化。</li><li>在开发时可以选择的框架太多(如Web框架就有100多个),有选择的地方就有错误。</li></ol></blockquote><h4 id="Python的应用领域"><a href="#Python的应用领域" class="headerlink" title="Python的应用领域"></a>Python的应用领域</h4><blockquote><p>目前Python在Web应用后端开发、云基础设施建设、DevOps、网络数据采集(爬虫)、自动化测试、数据分析、机器学习等领域都有着广泛的应用。运维->自动化,人工智能,大数据处理,游戏开发。</p></blockquote><h3 id="安装Python解释器"><a href="#安装Python解释器" class="headerlink" title="安装Python解释器"></a>安装Python解释器</h3><blockquote><p>想要开始Python编程之旅,首先得在自己使用的计算机上安装Python解释器环境,下面将以安装官方的Python解释器为例,讲解如何在不同的操作系统上安装Python环境。官方的Python解释器是用C语言实现的,也是使用最为广泛的Python解释器,通常称之为CPython。除此之外,Python解释器还有Java语言实现的Jython、C#语言实现的IronPython以及PyPy、Brython、Pyston等版本,有兴趣的读者可以自行了解。</p></blockquote><h4 id="Windows环境"><a href="#Windows环境" class="headerlink" title="Windows环境"></a>Windows环境</h4><blockquote><p>可以在<a href="https://www.python.org/" target="_blank" rel="noopener">Python官方网站</a>下载到Python的Windows安装程序(exe文件),需要注意的是如果在Windows 7环境下安装Python 3.x,需要先安装Service Pack 1补丁包(可以通过一些工具软件自动安装系统补丁的功能来安装),安装过程建议勾选“Add Python 3.x to PATH”(将Python 3.x添加到PATH环境变量)并选择自定义安装,在设置“Optional Features”界面最好将“pip”、“tcl/tk”、“Python test suite”等项全部勾选上。强烈建议选择自定义的安装路径并保证路径中没有中文。安装完成会看到“Setup was successful”的提示。如果稍后运行Python程序时,出现因为缺失一些动态链接库文件而导致Python解释器无法工作的问题,可以按照下面的方法加以解决。</p><p>如果系统显示api-ms-win-crt<em>.dll文件缺失,可以参照[《api-ms-win-crt</em>.dll缺失原因分析和解决方法》](<a href="https://zhuanlan.zhihu.com/p/32087135)一文讲解的方法进行处理或者直接在[微软官网](https://www.microsoft.com/zh-cn/download/details.aspx?id=48145)下载Visual" target="_blank" rel="noopener">https://zhuanlan.zhihu.com/p/32087135)一文讲解的方法进行处理或者直接在[微软官网](https://www.microsoft.com/zh-cn/download/details.aspx?id=48145)下载Visual</a> C++ Redistributable for Visual Studio 2015文件进行修复;如果是因为更新Windows的DirectX之后导致某些动态链接库文件缺失问题,可以下载一个<a href="https://dl.pconline.com.cn/download/360074-1.html" target="_blank" rel="noopener">DirectX修复工具</a>进行修复。</p></blockquote><h4 id="Linux环境"><a href="#Linux环境" class="headerlink" title="Linux环境"></a>Linux环境</h4><p>Linux环境自带了Python 2.x版本,但是如果要更新到3.x的版本,可以在<a href="https://www.python.org/" target="_blank" rel="noopener">Python的官方网站</a>下载Python的源代码并通过源代码构建安装的方式进行安装,具体的步骤如下所示(以CentOS为例)。</p><ol><li>安装依赖库(因为没有这些依赖库可能在源代码构件安装时因为缺失底层依赖库而失败)。</li></ol><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">yum -y install wget gcc zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel libffi-devel</span><br></pre></td></tr></table></figure><ol start="2"><li>下载Python源代码并解压缩到指定目录。</li></ol><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">wget https://www.python.org/ftp/python/3.7.6/Python-3.7.6.tar.xz</span><br><span class="line">xz -d Python-3.7.6.tar.xz</span><br><span class="line">tar -xvf Python-3.7.6.tar</span><br></pre></td></tr></table></figure><ol start="3"><li>切换至Python源代码目录并执行下面的命令进行配置和安装。</li></ol><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">cd Python-3.7.6</span><br><span class="line">./configure --prefix=/usr/local/python37 --enable-optimizations</span><br><span class="line">make && make install</span><br></pre></td></tr></table></figure><ol start="4"><li>修改用户主目录下名为.bash_profile的文件,配置PATH环境变量并使其生效。</li></ol><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">cd ~</span><br><span class="line">vim .bash_profile</span><br><span class="line"># ... 此处省略上面的代码 ...</span><br><span class="line"></span><br><span class="line">export PATH=$PATH:/usr/local/python37/bin</span><br><span class="line"></span><br><span class="line"># ... 此处省略下面的代码 ...</span><br></pre></td></tr></table></figure><ol start="5"><li>激活环境变量。</li></ol><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">source .bash_profile</span><br></pre></td></tr></table></figure><p><code>切换版本:</code></p><p>Ubantu中默认python路径:</p><p><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1ggilw800fvj30m007mgna.jpg" alt="image.png"></p><p>首先我们先看一下有没有关于Python的可选项:</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ update-alternatives --display pythonupdate-alternatives: 错误: 无 python 的候选项</span><br></pre></td></tr></table></figure><p>那首先先建立python的组,并添加Python2和Python3的可选项</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 2 # 添加Python2可选项,优先级为2$ sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.4 1 #添加Python3可选项,优先级为1</span><br></pre></td></tr></table></figure><p><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1ggilkrz1hsj30m607njt2.jpg" alt="image.png"></p><h3 id="保留字"><a href="#保留字" class="headerlink" title="保留字"></a>保留字</h3><p><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1ggio2xutbdj30qk06pq5l.jpg" alt="image.png"></p><h3 id="标识符"><a href="#标识符" class="headerlink" title="标识符"></a>标识符</h3><ol><li>不能用Pthon中的保留字</li><li>由字母,下划线,数字</li><li>区分大小写字母</li><li><code>__add</code>表示类的私有成员;</li><li><code>__init__()</code>构造函数;</li></ol><h3 id="变量"><a href="#变量" class="headerlink" title="变量"></a>变量</h3><p>变量的定义与使用</p><p>弱类型(动态类型语言):</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">>>> </span>print(a)</span><br><span class="line"><span class="number">123</span></span><br><span class="line"><span class="meta">>>> </span>type(a)</span><br><span class="line"><<span class="class"><span class="keyword">class</span> '<span class="title">int</span>'></span></span><br><span class="line">>>> a = "hello world"</span><br><span class="line"><span class="meta">>>> </span>print(a)</span><br><span class="line">hello world</span><br><span class="line"><span class="meta">>>> </span>type(a)</span><br><span class="line"><<span class="class"><span class="keyword">class</span> '<span class="title">str</span>'></span></span><br><span class="line"><span class="class">>>> <span class="title">id</span><span class="params">(a)</span> #地址</span></span><br><span class="line"><span class="class">139811477572656</span></span><br><span class="line">>>> a = 123</span><br><span class="line"><span class="meta">>>> </span>id(a)</span><br><span class="line"><span class="number">9306048</span></span><br><span class="line"><span class="meta">>>> </span>id(<span class="number">123</span>)</span><br><span class="line"><span class="number">9306048</span></span><br><span class="line"><span class="meta">>>> </span>id(<span class="number">123</span>)</span><br><span class="line"><span class="number">9306048</span></span><br><span class="line"><span class="meta">>>> </span>b = <span class="number">123</span></span><br><span class="line"><span class="meta">>>> </span>id(b)</span><br><span class="line"><span class="number">9306048</span></span><br><span class="line"><span class="meta">>>> </span>id(<span class="string">"hello world"</span>)</span><br><span class="line"><span class="number">139811477572656</span></span><br><span class="line"><span class="meta">>>> </span>a = <span class="string">"hello world"</span></span><br><span class="line"><span class="meta">>>> </span>id(a)</span><br><span class="line"><span class="number">139811477572656</span></span><br><span class="line">>>></span><br></pre></td></tr></table></figure><h3 id="变量和类型"><a href="#变量和类型" class="headerlink" title="变量和类型"></a>变量和类型</h3><p>在程序设计中,变量是一种存储数据的载体。计算机中的变量是实际存在的数据或者说是存储器中存储数据的一块内存空间,变量的值可以被读取和修改,这是所有计算和控制的基础。计算机能处理的数据有很多种类型,除了数值之外还可以处理文本、图形、音频、视频等各种各样的数据,那么不同的数据就需要定义不同的存储类型。Python中的数据类型很多,而且也允许我们自定义新的数据类型(这一点在后面会讲到),我们先介绍几种常用的数据类型。</p><ul><li>整型:Python中可以处理任意大小的整数(Python 2.x中有<code>int</code>和<code>long</code>两种类型的整数,但这种区分对Python来说意义不大,因此在Python 3.x中整数只有int这一种了),而且支持二进制(如<code>0b100</code>,换算成十进制是4)、八进制(如<code>0o100</code>,换算成十进制是64)、十进制(<code>100</code>)和十六进制(<code>0x100</code>,换算成十进制是256)的表示法。</li><li>浮点型:浮点数也就是小数,之所以称为浮点数,是因为按照科学记数法表示时,一个浮点数的小数点位置是可变的,浮点数除了数学写法(如<code>123.456</code>)之外还支持科学计数法(如<code>1.23456e2</code>)。</li><li>字符串型:字符串是以单引号或双引号括起来的任意文本,比如<code>'hello'</code>和<code>"hello"</code>,字符串还有原始字符串表示法、字节字符串表示法、Unicode字符串表示法,而且可以书写成多行的形式(用三个单引号或三个双引号开头,三个单引号或三个双引号结尾)。</li><li>布尔型:布尔值只有<code>True</code>、<code>False</code>两种值,要么是<code>True</code>,要么是<code>False</code>,在Python中,可以直接用<code>True</code>、<code>False</code>表示布尔值(请注意大小写),也可以通过布尔运算计算出来(例如<code>3 < 5</code>会产生布尔值<code>True</code>,而<code>2 == 1</code>会产生布尔值<code>False</code>)。</li><li>复数型:形如<code>3+5j</code>,跟数学上的复数表示一样,唯一不同的是虚部的<code>i</code>换成了<code>j</code>。实际上,这个类型并不常用,大家了解一下就可以了。</li></ul><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">a = int(input(<span class="string">'a = '</span>))</span><br><span class="line">b = int(input(<span class="string">'b = '</span>))</span><br><span class="line">print(<span class="string">'%d + %d = %d'</span> % (a, b, a + b))</span><br><span class="line">print(<span class="string">'%d - %d = %d'</span> % (a, b, a - b))</span><br><span class="line">print(<span class="string">'%d * %d = %d'</span> % (a, b, a * b))</span><br><span class="line">print(<span class="string">'%d / %d = %f'</span> % (a, b, a / b))<span class="comment">#精确值</span></span><br><span class="line">print(<span class="string">'%d // %d = %d'</span> % (a, b, a // b))<span class="comment">#取整</span></span><br><span class="line">print(<span class="string">'%d %% %d = %d'</span> % (a, b, a % b))<span class="comment">#取余</span></span><br><span class="line">print(<span class="string">'%d ** %d = %d'</span> % (a, b, a ** b))<span class="comment">#幂次方</span></span><br></pre></td></tr></table></figure><h3 id="运算符"><a href="#运算符" class="headerlink" title="运算符"></a>运算符</h3><p>Python支持多种运算符,下表大致按照优先级从高到低的顺序列出了所有的运算符,运算符的优先级指的是多个运算符同时出现时,先做什么运算然后再做什么运算。除了我们之前已经用过的赋值运算符和算术运算符,我们稍后会陆续讲到其他运算符的使用。</p><table><thead><tr><th>运算符</th><th>描述</th></tr></thead><tbody><tr><td><code>[]</code> <code>[:]</code></td><td>下标,切片</td></tr><tr><td><code>**</code></td><td>指数</td></tr><tr><td><code>~</code> <code>+</code> <code>-</code></td><td>按位取反, 正负号</td></tr><tr><td><code>*</code> <code>/</code> <code>%</code> <code>//</code></td><td>乘,除,模,整除</td></tr><tr><td><code>+</code> <code>-</code></td><td>加,减</td></tr><tr><td><code>>></code> <code><<</code></td><td>右移,左移</td></tr><tr><td><code>&</code></td><td>按位与</td></tr><tr><td><code>^</code> `</td><td>`</td></tr><tr><td><code><=</code> <code><</code> <code>></code> <code>>=</code></td><td>小于等于,小于,大于,大于等于</td></tr><tr><td><code>==</code> <code>!=</code></td><td>等于,不等于</td></tr><tr><td><code>is</code> <code>is not</code></td><td>身份运算符</td></tr><tr><td><code>in</code> <code>not in</code></td><td>成员运算符</td></tr><tr><td><code>not</code> <code>or</code> <code>and</code></td><td>逻辑运算符</td></tr><tr><td><code>=</code> <code>+=</code> <code>-=</code> <code>*=</code> <code>/=</code> <code>%=</code> <code>//=</code> <code>**=</code> <code>&=</code> `</td><td>=<code></code>^=<code></code>>>=<code></code><<=`</td></tr></tbody></table><blockquote><p><strong>说明:</strong> 在实际开发中,如果搞不清楚运算符的优先级,可以使用括号来确保运算的执行顺序。</p></blockquote><h3 id="if语句的使用"><a href="#if语句的使用" class="headerlink" title="if语句的使用"></a>if语句的使用</h3><p>在Python中,要构造分支结构可以使用<code>if</code>、<code>elif</code>和<code>else</code>关键字。所谓<strong>关键字</strong>就是有特殊含义的单词,像<code>if</code>和<code>else</code>就是专门用于构造分支结构的关键字,很显然你不能够使用它作为变量名(事实上,用作其他的标识符也是不可以)。下面的例子中演示了如何构造一个分支结构。</p><h3 id="while循环"><a href="#while循环" class="headerlink" title="while循环"></a>while循环</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> random</span><br><span class="line"></span><br><span class="line">answer = random.randint(<span class="number">1</span>, <span class="number">100</span>)</span><br><span class="line">counter = <span class="number">0</span></span><br><span class="line"><span class="keyword">while</span> <span class="literal">True</span>:</span><br><span class="line"> counter += <span class="number">1</span></span><br><span class="line"> number = int(input(<span class="string">'请输入: '</span>))</span><br><span class="line"> <span class="keyword">if</span> number < answer:</span><br><span class="line"> print(<span class="string">'大一点'</span>)</span><br><span class="line"> <span class="keyword">elif</span> number > answer:</span><br><span class="line"> print(<span class="string">'小一点'</span>)</span><br><span class="line"> <span class="keyword">else</span>:</span><br><span class="line"> print(<span class="string">'恭喜你猜对了!'</span>)</span><br><span class="line"> <span class="keyword">break</span></span><br><span class="line">print(<span class="string">'你总共猜了%d次'</span> % counter)</span><br><span class="line"><span class="keyword">if</span> counter > <span class="number">7</span>:</span><br><span class="line"> print(<span class="string">'你的智商余额明显不足'</span>)</span><br></pre></td></tr></table></figure><h3 id="for-in循环"><a href="#for-in循环" class="headerlink" title="for-in循环"></a>for-in循环</h3><p>如果明确的知道循环执行的次数或者要对一个容器进行迭代(后面会讲到),那么我们推荐使用<code>for-in</code>循环,例如下面代码中计算1~100求和的结果($\displaystyle\sum \limits_{n=1}^{100}n$)。</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">sum = <span class="number">0</span></span><br><span class="line"><span class="keyword">for</span> x <span class="keyword">in</span> range(<span class="number">101</span>):</span><br><span class="line"> sum += x</span><br><span class="line">print(sum)</span><br></pre></td></tr></table></figure><p><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1ggkzxjash4j30o00bmta8.jpg" alt="image.png"></p><ul><li><code>range(101)</code>:可以用来产生0到100范围的整数,需要注意的是取不到101。</li><li><code>range(1, 101)</code>:可以用来产生1到100范围的整数,相当于前面是闭区间后面是开区间。</li><li><code>range(1, 101, 2)</code>:可以用来产生1到100的奇数,其中2是步长,即每次数值递增的值。</li><li><code>range(100, 0, -2)</code>:可以用来产生100到1的偶数,其中-2是步长,即每次数字递减的值。</li></ul>]]></content>
<summary type="html">
<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" cla
</summary>
</entry>
<entry>
<title>Mynotes/Luogu_code/最小生成树/最小生成树</title>
<link href="http://yoursite.com/2020/07/10/Mynotes/Luogu_code/%E6%9C%80%E5%B0%8F%E7%94%9F%E6%88%90%E6%A0%91/%E6%9C%80%E5%B0%8F%E7%94%9F%E6%88%90%E6%A0%91/"/>
<id>http://yoursite.com/2020/07/10/Mynotes/Luogu_code/%E6%9C%80%E5%B0%8F%E7%94%9F%E6%88%90%E6%A0%91/%E6%9C%80%E5%B0%8F%E7%94%9F%E6%88%90%E6%A0%91/</id>
<published>2020-07-10T01:07:38.866Z</published>
<updated>2020-07-10T01:07:38.867Z</updated>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><h1 id="最小生成树"><a href="#最小生成树" class="headerlink" title="最小生成树"></a>最小生成树</h1><h2 id="Kruskal"><a href="#Kruskal" class="headerlink" title="Kruskal"></a>Kruskal</h2><p><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1ggjqf2bfs3j30sa0fu77z.jpg" alt="image.png"></p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*************************************************************************</span></span><br><span class="line"><span class="comment">> File Name: 3366-最小生成树.cpp</span></span><br><span class="line"><span class="comment">> Author:fangsong</span></span><br><span class="line"><span class="comment">> Mail: </span></span><br><span class="line"><span class="comment">> Created Time: 2020年07月08日 星期三 18时22分38秒</span></span><br><span class="line"><span class="comment"> ************************************************************************/</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iostream></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstdio></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cmath></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstring></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iomanip></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><algorithm></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><map></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><vector></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">edge</span>{</span><span class="comment">//边-起点 终点 权值-</span></span><br><span class="line"> <span class="keyword">int</span> from, to, val;</span><br><span class="line">}edg[<span class="number">200005</span>];</span><br><span class="line"></span><br><span class="line"><span class="keyword">int</span> n, m, ans, my_union[<span class="number">5005</span>], already = <span class="number">1</span>;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">bool</span> <span class="title">cmp</span><span class="params">(edge a, edge b)</span> </span>{</span><br><span class="line"> <span class="keyword">return</span> a.val < b.val;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">find_root</span><span class="params">(<span class="keyword">int</span> x)</span> </span>{<span class="comment">//并查集</span></span><br><span class="line"> <span class="keyword">if</span>(my_union[x] != x) {</span><br><span class="line"> <span class="keyword">return</span> my_union[x] = find_root(my_union[x]);</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> x;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="built_in">cin</span> >> n >> m;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i < m; i++) {</span><br><span class="line"> <span class="built_in">cin</span> >> edg[i].from >> edg[i].to >> edg[i].val;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">1</span>; i <= n; i++) {</span><br><span class="line"> my_union[i] = i;</span><br><span class="line"> }</span><br><span class="line"> sort(edg, edg + m, cmp);<span class="comment">//先排序</span></span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i < m; i++) {</span><br><span class="line"> <span class="keyword">int</span> aroot = find_root(edg[i].from), broot = find_root(edg[i].to);</span><br><span class="line"> <span class="keyword">if</span>(aroot != broot) {<span class="comment">//不连通</span></span><br><span class="line"> already ++;</span><br><span class="line"> ans += edg[i].val;</span><br><span class="line"> my_union[aroot] = broot;<span class="comment">//联通</span></span><br><span class="line"> <span class="keyword">if</span>(already == n) {</span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span>(already == n) {<span class="comment">//全部都在树上</span></span><br><span class="line"> <span class="built_in">cout</span> << ans << <span class="built_in">endl</span>;</span><br><span class="line"> } <span class="keyword">else</span> {</span><br><span class="line"> <span class="built_in">cout</span> << <span class="string">"orz"</span> << <span class="built_in">endl</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h2 id="Prim"><a href="#Prim" class="headerlink" title="Prim"></a>Prim</h2><ol><li>边权 </li><li>终点</li></ol>]]></content>
<summary type="html">
<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" cla
</summary>
</entry>
<entry>
<title>Mynotes/高级数据结构/线段树</title>
<link href="http://yoursite.com/2020/06/15/Mynotes/%E9%AB%98%E7%BA%A7%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/%E7%BA%BF%E6%AE%B5%E6%A0%91/"/>
<id>http://yoursite.com/2020/06/15/Mynotes/%E9%AB%98%E7%BA%A7%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/%E7%BA%BF%E6%AE%B5%E6%A0%91/</id>
<published>2020-06-15T07:14:25.989Z</published>
<updated>2020-06-15T07:14:25.989Z</updated>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><h1 id="线段树"><a href="#线段树" class="headerlink" title="线段树"></a>线段树</h1><p><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1gfkxpe4ivmj30u60fuwmz.jpg" alt="image.png"></p><p>如果我们想求<code>2-8</code>的值,那么只要将<code>2, 3-4, 5-7, 8-9</code>的和加在一起;</p><p>可知我们可以得到任意区间的和值;</p><h2 id="1-线段树-单点修改"><a href="#1-线段树-单点修改" class="headerlink" title="1.线段树-单点修改"></a>1.线段树-单点修改</h2><p><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1gfkxwmix5rj30uj0g2n69.jpg" alt="image.png"></p><p><strong>时间复杂度:</strong>log(n)</p><p>当将2处的8修改为3,那么<code>0-2 0 -4 0 - 9</code>分别修改为<code>15 25 52</code>,</p><h2 id="2-线段树-区间查询"><a href="#2-线段树-区间查询" class="headerlink" title="2.线段树-区间查询"></a>2.线段树-区间查询</h2><p><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1gfky6gbvjmj30uk0gd7e2.jpg" alt="image.png"></p><p><strong>时间复杂度:</strong>log(n)</p><p><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1gfl550vvffj30nk081q6s.jpg" alt="image.png"></p><p><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1gfl574avdnj30uh0gfds1.jpg" alt="image.png"></p><p>如果采用完全二叉树那么要开辟空间为<code>2n+n+n-1 = 4n-1</code></p><h2 id="3-线段树-区间修改"><a href="#3-线段树-区间修改" class="headerlink" title="3.线段树-区间修改"></a>3.线段树-区间修改</h2><p><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1gfkyfobzt4j30uj0gsn5u.jpg" alt="image.png"></p><p><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1gfkyihojd1j314h0lnnd6.jpg" alt="image.png"></p><h2 id="线段树-关键词"><a href="#线段树-关键词" class="headerlink" title="线段树-关键词"></a>线段树-关键词</h2><p><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1gfl5dv9w4oj30jm0af43h.jpg" alt="image.png"></p><h2 id="线段树模板1"><a href="#线段树模板1" class="headerlink" title="线段树模板1"></a><a href="http://oj.haizeix.com/problem/222" target="_blank" rel="noopener">线段树模板1</a></h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iostream></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstdio></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cmath></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstring></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iomanip></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><algorithm></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><map></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><vector></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> MAX_N 10000</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> lc(ind) (ind << 1)<span class="comment">//左孩子</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> rc(ind) (ind << 1 | 1)<span class="comment">//右孩子</span></span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">node</span>{</span></span><br><span class="line"> <span class="keyword">int</span> sum;</span><br><span class="line">}tree[(MAX_N << <span class="number">2</span>) + <span class="number">5</span>];<span class="comment">//最多开4倍空间</span></span><br><span class="line"><span class="keyword">int</span> root = <span class="number">1</span>;<span class="comment">//根从1开始的</span></span><br><span class="line"><span class="keyword">int</span> arr[MAX_N + <span class="number">5</span>]; <span class="comment">//记录原数组</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">UP</span><span class="params">(<span class="keyword">int</span> ind)</span> </span>{</span><br><span class="line"> tree[ind].sum = max(tree[lc(ind)].sum , tree[rc(ind)].sum);<span class="comment">//root 是左右最大的值</span></span><br><span class="line"> <span class="keyword">return</span> ;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">build</span><span class="params">(<span class="keyword">int</span> ind, <span class="keyword">int</span> l, <span class="keyword">int</span> r)</span> </span>{<span class="comment">//建立线段树ind节点下表,lr是范围</span></span><br><span class="line"> <span class="keyword">if</span>(l == r) {<span class="comment">//叶子节点</span></span><br><span class="line"> tree[ind].sum = arr[l];<span class="comment">//和值为原数组的值</span></span><br><span class="line"> <span class="keyword">return</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">int</span> mid = (l + r) >> <span class="number">1</span>;<span class="comment">//不是叶子分两端</span></span><br><span class="line"> build(lc(ind), l, mid);<span class="comment">//递归建立左子树</span></span><br><span class="line"> build(rc(ind), mid + <span class="number">1</span>, r);<span class="comment">//递归建立右子树</span></span><br><span class="line"> UP(ind);<span class="comment">//左右子树建立完后,节点的值都有了,向上更新</span></span><br><span class="line"> <span class="keyword">return</span>;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">modify</span><span class="params">(<span class="keyword">int</span> ind, <span class="keyword">int</span> x, <span class="keyword">int</span> y, <span class="keyword">int</span> l, <span class="keyword">int</span> r)</span> </span>{<span class="comment">//单点修改x位置修改为y</span></span><br><span class="line"> <span class="keyword">if</span>(l == r) {<span class="comment">//叶子节点</span></span><br><span class="line"> tree[ind].sum = y;<span class="comment">//直接修改</span></span><br><span class="line"> <span class="keyword">return</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">int</span> mid = (l + r) >> <span class="number">1</span>;</span><br><span class="line"> <span class="keyword">if</span>(x <= mid) modify(lc(ind), x, y, l, mid);<span class="comment">//左边区间查询修改</span></span><br><span class="line"> <span class="keyword">else</span> modify(rc(ind), x, y, mid + <span class="number">1</span>, r);<span class="comment">//右边区间查询修改</span></span><br><span class="line"> UP(ind);<span class="comment">//上提操作//只要值变就得上提高</span></span><br><span class="line"> <span class="keyword">return</span>;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">query</span><span class="params">(<span class="keyword">int</span> ind, <span class="keyword">int</span> x, <span class="keyword">int</span> y, <span class="keyword">int</span> l, <span class="keyword">int</span> r)</span> </span>{<span class="comment">//查询lr区间的值</span></span><br><span class="line"> <span class="keyword">if</span>(x <= l && r <= y) {<span class="comment">//完全包含的时候</span></span><br><span class="line"> <span class="keyword">return</span> tree[ind].sum;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">int</span> ans = <span class="number">0x80000000</span>, mid = (l + r) >> <span class="number">1</span>;<span class="comment">//ans极小值</span></span><br><span class="line"> <span class="keyword">if</span>(x <= mid) ans = max(ans, query(lc(ind), x, y, l , mid));<span class="comment">//左子树包含查询信息</span></span><br><span class="line"> <span class="keyword">if</span>(y > mid) ans = max(ans, query(rc(ind), x , y, mid + <span class="number">1</span>, r));<span class="comment">//右子书包含查询信息</span></span><br><span class="line"> <span class="keyword">return</span> ans;<span class="comment">//返回lr区间值</span></span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">int</span> n, m;</span><br><span class="line"> <span class="built_in">cin</span> >> n >> m;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">1</span>; i <= n; i++) {</span><br><span class="line"> <span class="built_in">cin</span> >> arr[i];</span><br><span class="line"> }</span><br><span class="line"> build(root, <span class="number">1</span>, n);</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i < m; i++) {</span><br><span class="line"> <span class="keyword">int</span> a, b, c;</span><br><span class="line"> <span class="built_in">cin</span> >> a >> b >> c;</span><br><span class="line"> <span class="keyword">if</span>(a == <span class="number">1</span>) modify(root, b, c, <span class="number">1</span>, n);</span><br><span class="line"> <span class="keyword">else</span> {</span><br><span class="line"> <span class="built_in">cout</span> << query(root, b, c, <span class="number">1</span>, n) << <span class="built_in">endl</span>;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h2 id="线段树模板2"><a href="#线段树模板2" class="headerlink" title="线段树模板2"></a><a href="http://oj.haizeix.com/problem/223" target="_blank" rel="noopener">线段树模板2</a></h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iostream></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstdio></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cmath></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstring></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iomanip></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><algorithm></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><map></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><vector></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> MAX_N 10000</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> lc(ind) (tree[ind].lind)<span class="comment">//左孩子,当前结点左子树的下表</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> rc(ind) (tree[ind].rind)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> tag(ind) tree[ind].tag</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> sum(ind) tree[ind].sum</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> cnt(ind) tree[ind].cnt</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">node</span>{</span><span class="comment">//结点</span></span><br><span class="line"> <span class="keyword">long</span> <span class="keyword">long</span> sum;<span class="comment">//和值</span></span><br><span class="line"> <span class="keyword">long</span> <span class="keyword">long</span> lind, rind;<span class="comment">//左孩子下表,右孩子下表</span></span><br><span class="line"> <span class="keyword">long</span> <span class="keyword">long</span> tag, cnt;</span><br><span class="line">} tree[(MAX_N << <span class="number">1</span>) + <span class="number">5</span>];</span><br><span class="line"><span class="keyword">long</span> <span class="keyword">long</span> root = <span class="number">0</span>, cnt = <span class="number">1</span>;<span class="comment">//root从0开始, cnt已经用了多少节点</span></span><br><span class="line"><span class="keyword">long</span> <span class="keyword">long</span> arr[MAX_N + <span class="number">5</span>];<span class="comment">//原数组</span></span><br><span class="line"><span class="function"><span class="keyword">long</span> <span class="keyword">long</span> <span class="title">getNode</span><span class="params">()</span> </span>{<span class="keyword">return</span> cnt++;}<span class="comment">//分配结点</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">DOWN</span><span class="params">(<span class="keyword">long</span> <span class="keyword">long</span> ind)</span> </span>{<span class="comment">//分发</span></span><br><span class="line"> <span class="keyword">if</span>(tag(ind)) {<span class="comment">//蓝标记不为零</span></span><br><span class="line"> tag(lc(ind)) += tag(ind);<span class="comment">//加到左右子树</span></span><br><span class="line"> tag(rc(ind)) += tag(ind);</span><br><span class="line"> sum(lc(ind)) += tag(ind) * cnt(lc(ind));</span><br><span class="line"> sum(rc(ind)) += tag(ind) * cnt(rc(ind));</span><br><span class="line"> tag(ind) = <span class="number">0</span>;<span class="comment">//已经下沉标记,赋值为0</span></span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> ;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">UP</span><span class="params">(<span class="keyword">long</span> <span class="keyword">long</span> ind)</span> </span>{</span><br><span class="line"> tree[ind].sum = tree[lc(ind)].sum + tree[rc(ind)].sum;<span class="comment">//求得是和值</span></span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">build</span><span class="params">(<span class="keyword">long</span> <span class="keyword">long</span> ind, <span class="keyword">long</span> <span class="keyword">long</span> l, <span class="keyword">long</span> <span class="keyword">long</span> r)</span> </span>{<span class="comment">//建立结点,</span></span><br><span class="line"> tree[ind].cnt = (r - l + <span class="number">1</span>);<span class="comment">//cnt是区间范围内总结点</span></span><br><span class="line"> <span class="keyword">if</span>(l == r) {<span class="comment">//叶子</span></span><br><span class="line"> tree[ind].sum = arr[l];<span class="comment">//sum</span></span><br><span class="line"> <span class="keyword">return</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">long</span> <span class="keyword">long</span> mid = (l + r) >> <span class="number">1</span>;<span class="comment">//不是叶子求中间位置</span></span><br><span class="line"> tree[ind].lind = getNode();<span class="comment">//左孩子分配一个节点</span></span><br><span class="line"> tree[ind].rind = getNode();<span class="comment">//右孩子分配一个节点</span></span><br><span class="line"> build(lc(ind), l, mid);<span class="comment">//左边</span></span><br><span class="line"> build(rc(ind), mid + <span class="number">1</span>, r);<span class="comment">//右边</span></span><br><span class="line"> UP(ind);<span class="comment">//上提操作</span></span><br><span class="line"> <span class="keyword">return</span> ;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">modify</span><span class="params">(<span class="keyword">long</span> <span class="keyword">long</span> ind, <span class="keyword">long</span> <span class="keyword">long</span> x, <span class="keyword">long</span> <span class="keyword">long</span> y, <span class="keyword">long</span> <span class="keyword">long</span> d, <span class="keyword">long</span> <span class="keyword">long</span> l, <span class="keyword">long</span> <span class="keyword">long</span> r)</span> </span>{</span><br><span class="line"> <span class="comment">//区间修改, x y之间加上d,此时范围在l , r</span></span><br><span class="line"> <span class="keyword">if</span>(x <= l && r <= y) {<span class="comment">//若l, r在x, y 中间</span></span><br><span class="line"> tree[ind].tag += d;<span class="comment">//tag是拦标记, 区间内+d</span></span><br><span class="line"> tree[ind].sum += d * tree[ind].cnt;<span class="comment">//总sum = d * cnt</span></span><br><span class="line"> <span class="keyword">return</span>;</span><br><span class="line"> }</span><br><span class="line"> DOWN(ind);<span class="comment">//标记下沉//递归之前</span></span><br><span class="line"> <span class="keyword">long</span> <span class="keyword">long</span> mid = (l + r) >> <span class="number">1</span>;</span><br><span class="line"> <span class="keyword">if</span>(x <= mid) modify(lc(ind), x, y, d, l , mid);</span><br><span class="line"> <span class="keyword">if</span>(y > mid) modify(rc(ind), x, y, d, mid + <span class="number">1</span>, r);</span><br><span class="line"> UP(ind);<span class="comment">//标记上提//递归之后</span></span><br><span class="line"> <span class="keyword">return</span>;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">long</span> <span class="keyword">long</span> <span class="title">query</span><span class="params">( <span class="keyword">long</span> <span class="keyword">long</span> ind, <span class="keyword">long</span> <span class="keyword">long</span> x, <span class="keyword">long</span> <span class="keyword">long</span> y, <span class="keyword">long</span> <span class="keyword">long</span> l, <span class="keyword">long</span> <span class="keyword">long</span> r)</span> </span>{<span class="comment">//区间查询</span></span><br><span class="line"> <span class="comment">//x, y, 当前l , r</span></span><br><span class="line"> <span class="keyword">if</span>(x <= l && r <= y) {<span class="comment">//包含</span></span><br><span class="line"> <span class="keyword">return</span> sum(ind);<span class="comment">//直接返回值</span></span><br><span class="line"> }</span><br><span class="line"> DOWN(ind);<span class="comment">//不包含 下沉标记</span></span><br><span class="line"> <span class="keyword">long</span> <span class="keyword">long</span> mid = (l + r) >> <span class="number">1</span>, ans = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">if</span>(x <= mid) ans += query(lc(ind), x, y, l ,mid);</span><br><span class="line"> <span class="keyword">if</span>(y > mid) ans += query(rc(ind), x, y, mid + <span class="number">1</span>, r);</span><br><span class="line"> <span class="keyword">return</span> ans;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">long</span> <span class="keyword">long</span> n, m;</span><br><span class="line"> <span class="built_in">cin</span> >> n >> m;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">long</span> <span class="keyword">long</span> i = <span class="number">1</span>; i <= n; i++) <span class="built_in">cin</span> >> arr[i];</span><br><span class="line"> build(root, <span class="number">1</span>, n);</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">long</span> <span class="keyword">long</span> i = <span class="number">0</span>; i < m; i++) {</span><br><span class="line"> <span class="keyword">long</span> <span class="keyword">long</span> a, b, c, d;</span><br><span class="line"> <span class="built_in">cin</span> >> a >> b >> c;</span><br><span class="line"> <span class="keyword">if</span>(a == <span class="number">1</span>) {</span><br><span class="line"> <span class="built_in">cin</span> >> d;</span><br><span class="line"> modify(root, b, c, d, <span class="number">1</span>, n);</span><br><span class="line"> } <span class="keyword">else</span> {</span><br><span class="line"> <span class="built_in">cout</span> << query(root, b, c, <span class="number">1</span> , n) << <span class="built_in">endl</span>;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>]]></content>
<summary type="html">
<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" cla
</summary>
</entry>
<entry>
<title>Mynotes/算法/最短路径算法</title>
<link href="http://yoursite.com/2020/06/15/Mynotes/%E7%AE%97%E6%B3%95/%E6%9C%80%E7%9F%AD%E8%B7%AF%E5%BE%84%E7%AE%97%E6%B3%95/"/>
<id>http://yoursite.com/2020/06/15/Mynotes/%E7%AE%97%E6%B3%95/%E6%9C%80%E7%9F%AD%E8%B7%AF%E5%BE%84%E7%AE%97%E6%B3%95/</id>
<published>2020-06-15T07:14:25.988Z</published>
<updated>2020-06-15T07:14:25.988Z</updated>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><h1 id="最短路径算法"><a href="#最短路径算法" class="headerlink" title="最短路径算法"></a>最短路径算法</h1><h2 id="Floyd算法"><a href="#Floyd算法" class="headerlink" title="Floyd算法"></a>Floyd算法</h2><p><code>ans[x][y]</code>从x=>y的最短</p><p><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1gf7vp6oadmj31030metpe.jpg" alt="image.png"></p><p>如果以1为中间结点时:<br>$$<br>a[x][y] = min { a[x][y], a[x][1]+a[1][y] }<br>$$<br>如果以2为中间节点时<br>$$<br>a[x][y] = min { a[x][y], a[x][2]+a[2][y] }<br>$$</p><p>$$<br>.<br>.<br>.<br>.<br>$$</p><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iostream></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstring></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">int</span> n, m, s, ans[<span class="number">1005</span>][<span class="number">1005</span>];</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="built_in">memset</span>(ans, <span class="number">0x3f</span>, <span class="keyword">sizeof</span>(ans));</span><br><span class="line"> <span class="built_in">cin</span> >> n >> m >> s;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i < m; i++) {</span><br><span class="line"> <span class="keyword">int</span> a, b , c;</span><br><span class="line"> <span class="built_in">cin</span> >> a >> b >> c;</span><br><span class="line"> ans[a][b] = c;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">1</span>; i <= n; i++) {</span><br><span class="line"> ans[i][i] = <span class="number">0</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">1</span>; i <= n; i++) {</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> j = <span class="number">1</span>; j <= n; j++) {</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> k = <span class="number">1</span>; k <= n; k++) {</span><br><span class="line"> ans[j][k] = min(ans[j][k], ans[j][i] + ans[i][k]);</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">1</span>; i <= n; i++) {</span><br><span class="line"> <span class="keyword">if</span>(i != <span class="number">1</span>)<span class="built_in">cout</span> << <span class="string">" "</span>;</span><br><span class="line"> <span class="built_in">cout</span> << ans[s][i];</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h2 id="Dijkstra算法"><a href="#Dijkstra算法" class="headerlink" title="Dijkstra算法"></a>Dijkstra算法</h2><p><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1gf7wavfb2bj30zs0mhqd1.jpg" alt="image.png"></p><p>每次选择一个最小权值的结点固定</p><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><iostream></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><cstring></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><queue></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">node</span> {</span></span><br><span class="line"> <span class="keyword">int</span> now, dist;</span><br><span class="line"> <span class="keyword">bool</span> <span class="keyword">operator</span><(<span class="keyword">const</span> node &b) <span class="keyword">const</span> {</span><br><span class="line"> <span class="keyword">return</span> <span class="keyword">this</span>->dist > b.dist;</span><br><span class="line"> }</span><br><span class="line">};</span><br><span class="line"></span><br><span class="line"><span class="keyword">int</span> n, m, s, dis[<span class="number">1005</span>][<span class="number">1005</span>], ans[<span class="number">1005</span>];</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="built_in">memset</span>(dis, <span class="number">0x3F</span>, <span class="keyword">sizeof</span>(dis));</span><br><span class="line"> <span class="built_in">memset</span>(ans, <span class="number">0x3F</span>, <span class="keyword">sizeof</span>(ans));</span><br><span class="line"> <span class="built_in">cin</span> >> n >> m >> s;</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i < m; i++) {</span><br><span class="line"> <span class="keyword">int</span> a, b, c;</span><br><span class="line"> <span class="built_in">cin</span> >> a >> b >> c;</span><br><span class="line"> dis[a][b] = min(dis[a][b], c);</span><br><span class="line"> }</span><br><span class="line"> priority_queue<node> que;</span><br><span class="line"> que.push({s, <span class="number">0</span>});</span><br><span class="line"> <span class="keyword">while</span> (!que.empty()) {</span><br><span class="line"> node t = que.top();</span><br><span class="line"> que.pop();</span><br><span class="line"> <span class="keyword">if</span> (ans[t.now] != <span class="number">0x3F3F3F3F</span>) <span class="keyword">continue</span>;</span><br><span class="line"> ans[t.now] = t.dist;</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i <= n; i++) {</span><br><span class="line"> <span class="keyword">if</span> (dis[t.now][i] != <span class="number">0x3F3F3F3F</span> && ans[i] == <span class="number">0x3F3F3F3F</span>) {</span><br><span class="line"> que.push({i, t.dist + dis[t.now][i]});</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i <= n; i++) {</span><br><span class="line"> <span class="keyword">if</span> (i != <span class="number">1</span>) <span class="built_in">cout</span> << <span class="string">" "</span>;</span><br><span class="line"> <span class="keyword">if</span> (ans[i] != <span class="number">0x3F3F3F3F</span>) {</span><br><span class="line"> <span class="built_in">cout</span> << ans[i];</span><br><span class="line"> } <span class="keyword">else</span> {</span><br><span class="line"> <span class="built_in">cout</span> << <span class="number">0x7FFFFFFF</span>;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br></pre></td></tr></table></figure><h2 id="链式前向星"><a href="#链式前向星" class="headerlink" title="链式前向星"></a>链式前向星</h2><p>主要是解决了迪杰斯特拉中的遍历所有连接的点,影响时间</p><p>终点 权值 </p><p>next: </p><p>head: 结点i为起点的最后一条边的编号</p><p>1=>2 2</p><p>2=>3 2</p><p>2=>4 1</p><p>1=>3 5</p><p>3=>4 3</p><p>1=》4 4</p><p><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1gf7xwggh69j30sm0hszx7.jpg" alt="image.png"></p><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iostream></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstdio></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cmath></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstring></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iomanip></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><algorithm></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><queue></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><vector></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">node</span> {</span></span><br><span class="line"> <span class="keyword">int</span> e, dis, next;<span class="comment">//e终点,dis起点到终点的距离, next上一条边的编号</span></span><br><span class="line">};</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">point</span> {</span><span class="comment">//优先队列结构</span></span><br><span class="line"> <span class="keyword">int</span> now, dist;<span class="comment">//现在位置,到这的距离</span></span><br><span class="line"> <span class="keyword">bool</span> <span class="keyword">operator</span>< (<span class="keyword">const</span> point &b) <span class="keyword">const</span> {<span class="comment">//重载<号</span></span><br><span class="line"> <span class="keyword">return</span> <span class="keyword">this</span>->dist > b.dist;<span class="comment">//重载<号反着写 ></span></span><br><span class="line"> }</span><br><span class="line">};</span><br><span class="line"><span class="keyword">int</span> n, m, s, cnt, ans[<span class="number">100005</span>], head[<span class="number">100005</span>];<span class="comment">//head</span></span><br><span class="line">node edge[<span class="number">200005</span>];</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="built_in">memset</span>(head, <span class="number">-1</span>, <span class="keyword">sizeof</span>(head));<span class="comment">//head初始化为-1</span></span><br><span class="line"> <span class="built_in">memset</span>(ans, <span class="number">0x3f3f3f3f</span>, <span class="keyword">sizeof</span>(ans));<span class="comment">//ans初始化为最大值</span></span><br><span class="line"> <span class="built_in">cin</span> >> n >> m >> s;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i < m; i++) {<span class="comment">//边的信息</span></span><br><span class="line"> <span class="keyword">int</span> a, b, c;</span><br><span class="line"> <span class="built_in">cin</span> >> a >> b >> c;</span><br><span class="line"> edge[i].e = b;<span class="comment">//终点</span></span><br><span class="line"> edge[i].dis = c;<span class="comment">//权值</span></span><br><span class="line"> edge[i].next = head[a];</span><br><span class="line"> head[a] = i;</span><br><span class="line"> }</span><br><span class="line"> priority_queue<point>que;</span><br><span class="line"> que.push({s, <span class="number">0</span>});</span><br><span class="line"> <span class="keyword">while</span>(!que.empty()) {</span><br><span class="line"> point t = que.top();</span><br><span class="line"> que.pop();</span><br><span class="line"> <span class="keyword">if</span>(ans[t.now] != <span class="number">0x3f3f3f3f</span>)<span class="keyword">continue</span>;</span><br><span class="line"> ans[t.now] = t.dist;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = head[t.now]; i != <span class="number">-1</span>; i = edge[i].next){</span><br><span class="line"> <span class="keyword">if</span>(ans[edge[i].e] == <span class="number">0x3f3f3f3f</span>) {</span><br><span class="line"> que.push({edge[i].e, t.dist + edge[i].dis});</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">1</span>; i <= n; i++) {</span><br><span class="line"> <span class="keyword">if</span>(i!=<span class="number">1</span>)<span class="built_in">cout</span> << <span class="string">" "</span>;</span><br><span class="line"> <span class="built_in">cout</span> << ans[i];</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h2 id="bellman-ford算法"><a href="#bellman-ford算法" class="headerlink" title="bellman-ford算法"></a>bellman-ford算法</h2><p>所有点所有边一起松弛</p><p>有多少边松弛多少</p><p>$$<br>ans[边的终点]=ans[边的起点]+边的权值<br>$$<br><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1gffg166c0mj30kk07074u.jpg" alt="image.png"></p><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iostream></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstdio></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cmath></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstring></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iomanip></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><algorithm></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><map></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><vector></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">int</span> n, m, s, dis[<span class="number">100005</span>][<span class="number">3</span>], ans[<span class="number">10005</span>];</span><br><span class="line"><span class="comment">//n个点,m个边,s起点,dis边的信息0起点,1终点,2权值,答案数组到这个点最短是多少</span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="built_in">memset</span>(ans, <span class="number">0x3f</span>, <span class="keyword">sizeof</span>(ans));<span class="comment">//初始化所有答案的距离很大</span></span><br><span class="line"> <span class="built_in">cin</span> >> n >> m >> s;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i < m; i++) {<span class="comment">//输入m组</span></span><br><span class="line"> <span class="keyword">int</span> a, b, c;</span><br><span class="line"> <span class="built_in">cin</span> >> a >> b >> c;</span><br><span class="line"> dis[i][<span class="number">0</span>] = a;<span class="comment">//a起点</span></span><br><span class="line"> dis[i][<span class="number">1</span>] = b;<span class="comment">//b终点</span></span><br><span class="line"> dis[i][<span class="number">2</span>] = c;<span class="comment">//c权值</span></span><br><span class="line"> }</span><br><span class="line"> ans[s] = <span class="number">0</span>;<span class="comment">//初始化起点答案为0</span></span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">2</span>; i <= n; i++) {<span class="comment">//点,因为求出来一个,从2开始</span></span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> j = <span class="number">0</span>; j < m; j++) {<span class="comment">//边</span></span><br><span class="line"> ans[dis[j][<span class="number">1</span>]] = min(ans[dis[j][<span class="number">1</span>]], ans[dis[j][<span class="number">0</span>]] + dis[j][<span class="number">2</span>]);<span class="comment">//这条边的终点可能变化,到这条边的值与这条边的起点加上这条边的权值去一个最小的</span></span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">1</span>; i <= n; i++) {<span class="comment">//输出到每一个点的最小距离</span></span><br><span class="line"> <span class="built_in">cout</span> << ans[i] << <span class="built_in">endl</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h2 id="SPFA-算法(Bellman-Ford-队列优化)"><a href="#SPFA-算法(Bellman-Ford-队列优化)" class="headerlink" title="SPFA 算法(Bellman-Ford 队列优化)"></a>SPFA 算法(Bellman-Ford 队列优化)</h2><p>由于<code>bellman-ford</code>比较慢所以要优化</p><p><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1gffgwczkk0j30ih0600tq.jpg" alt="image.png"></p><p><strong>为什么慢呢?</strong></p><p>1、每次会遍历所有边</p><p>2、后面有一些点还没有算到那也遍历了</p><p><strong>解决?</strong></p><p>SPFA优化:方法,队列<code>queue</code>,只会便利以谁为起点的边,只便利有可能会被松弛的点。</p><p><strong>时间复杂度?</strong></p><p>最坏的时间复杂度n * m;(玄学优化)</p><p><a href="https://www.luogu.com.cn/problem/P3371" target="_blank" rel="noopener">P3371 【模板】单源最短路径(弱化版)</a></p><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iostream></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstdio></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cmath></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstring></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iomanip></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><algorithm></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><queue></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><vector></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">node</span> {</span></span><br><span class="line"> <span class="keyword">int</span> e, dis, next;<span class="comment">//终点,权值,下一个结点</span></span><br><span class="line">};</span><br><span class="line"><span class="keyword">int</span> n, m, s, cnt, ans[<span class="number">10005</span>], head[<span class="number">10005</span>], que_mark[<span class="number">10005</span>];</span><br><span class="line"><span class="comment">//n个节点m条边,最多一万结点,链式前向星,que_mark标记避免重复</span></span><br><span class="line">node edge[<span class="number">500005</span>];</span><br><span class="line"><span class="keyword">char</span> mark[<span class="number">10005</span>][<span class="number">10005</span>];</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="built_in">memset</span>(head, <span class="number">-1</span>, <span class="keyword">sizeof</span>(head));<span class="comment">//初始化head为-1</span></span><br><span class="line"> <span class="built_in">memset</span>(ans, <span class="number">0x3f</span>, <span class="keyword">sizeof</span>(ans));<span class="comment">//初始化答案数组极大值</span></span><br><span class="line"> <span class="built_in">cin</span> >> n >> m >> s;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i < m; i++) {<span class="comment">//插入边</span></span><br><span class="line"> <span class="keyword">int</span> a, b, c;</span><br><span class="line"> <span class="built_in">cin</span> >> a >> b >> c;</span><br><span class="line"> <span class="keyword">if</span>(mark[a][b]) {<span class="comment">//如果存在边集里 查找边在哪</span></span><br><span class="line"> <span class="keyword">int</span> ind = head[a];</span><br><span class="line"> <span class="keyword">while</span>(edge[ind].e != b) {<span class="comment">//如果边的终点不等于b</span></span><br><span class="line"> ind = edge[ind].next;<span class="comment">//一直往下找</span></span><br><span class="line"> }</span><br><span class="line"> edge[ind].dis = min(c, edge[ind].dis);<span class="comment">//找到更新权值</span></span><br><span class="line"> } <span class="keyword">else</span> {<span class="comment">//不存在</span></span><br><span class="line"> mark[a][b] = <span class="number">1</span>;<span class="comment">//标记a-b存在了</span></span><br><span class="line"> edge[cnt].e = b;<span class="comment">//终点</span></span><br><span class="line"> edge[cnt].dis = c;<span class="comment">//权值</span></span><br><span class="line"> edge[cnt].next = head[a];<span class="comment">//next数组</span></span><br><span class="line"> head[a] = cnt;</span><br><span class="line"> cnt++;<span class="comment">//第几条边,边的数量</span></span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> ans[s] = <span class="number">0</span>;<span class="comment">//起点为0</span></span><br><span class="line"> <span class="built_in">queue</span><<span class="keyword">int</span>>que;</span><br><span class="line"> que.push(s);</span><br><span class="line"> que_mark[s] = <span class="number">1</span>;<span class="comment">//标记为已经在队列</span></span><br><span class="line"> <span class="keyword">while</span>(!que.empty()) {</span><br><span class="line"> <span class="keyword">int</span> t = que.front();<span class="comment">//取除队首元素</span></span><br><span class="line"> que.pop();</span><br><span class="line"> que_mark[t] = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = head[t]; i != <span class="number">-1</span>; i = edge[i].next) {<span class="comment">//从取出的边走知道走到-1</span></span><br><span class="line"> <span class="keyword">if</span>(ans[edge[i].e] > ans[t] + edge[i].dis) {<span class="comment">//如果这条边的起点+权值小于,直接到终点的值,更新</span></span><br><span class="line"> ans[edge[i].e] = ans[t] + edge[i].dis;<span class="comment">//更新</span></span><br><span class="line"> <span class="keyword">if</span>(!que_mark[edge[i].e]) {<span class="comment">//如果不在队列里,边的终点</span></span><br><span class="line"> que_mark[edge[i].e] = <span class="number">1</span>;<span class="comment">//先标记后放入</span></span><br><span class="line"> que.push(edge[i].e);</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">1</span>; i <= n; i++) {</span><br><span class="line"> <span class="keyword">if</span>(i!=<span class="number">1</span>) {</span><br><span class="line"> <span class="built_in">cout</span> << <span class="string">" "</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span>(ans[i] != <span class="number">0x3f3f3f3f</span>) {</span><br><span class="line"> <span class="built_in">cout</span> << ans[i];</span><br><span class="line"> } <span class="keyword">else</span> {</span><br><span class="line"> <span class="built_in">cout</span> << <span class="number">0x7fffffff</span>;</span><br><span class="line"> } </span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h1 id="最短路算法总结"><a href="#最短路算法总结" class="headerlink" title="最短路算法总结"></a>最短路算法总结</h1><p><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1gffi4wumryj30oo0b2aco.jpg" alt="image.png"></p><h1 id="P1629-邮递员送信"><a href="#P1629-邮递员送信" class="headerlink" title="P1629 邮递员送信"></a><a href="https://www.luogu.com.cn/problem/P1629" target="_blank" rel="noopener">P1629 邮递员送信</a></h1><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*************************************************************************</span></span><br><span class="line"><span class="comment">> File Name: 1629-邮递员送信.cpp</span></span><br><span class="line"><span class="comment">> Author:fangsong</span></span><br><span class="line"><span class="comment">> Mail: </span></span><br><span class="line"><span class="comment">> Created Time: 2020年06月03日 星期三 23时48分25秒</span></span><br><span class="line"><span class="comment"> ************************************************************************/</span></span><br><span class="line"></span><br><span class="line"><span class="comment">/*Dijkstra版本</span></span><br><span class="line"><span class="comment">#include<iostream></span></span><br><span class="line"><span class="comment">#include<cstdio></span></span><br><span class="line"><span class="comment">#include<cmath></span></span><br><span class="line"><span class="comment">#include<cstring></span></span><br><span class="line"><span class="comment">#include<iomanip></span></span><br><span class="line"><span class="comment">#include<algorithm></span></span><br><span class="line"><span class="comment">#include<queue></span></span><br><span class="line"><span class="comment">#include<vector></span></span><br><span class="line"><span class="comment">using namespace std;</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">struct edge{</span></span><br><span class="line"><span class="comment"> int to, val, next;//终点,权值,next</span></span><br><span class="line"><span class="comment">};</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">struct node {</span></span><br><span class="line"><span class="comment"> int now, val;//现在在哪,到这位置路径最小多少</span></span><br><span class="line"><span class="comment"> bool operator < (const node &b) const{//重载小于号,权值小的排在前面</span></span><br><span class="line"><span class="comment"> return this->val > b.val;</span></span><br><span class="line"><span class="comment"> }</span></span><br><span class="line"><span class="comment">};</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">int n, m, head1[1005], head2[1005], ans1[1005], ans2[1005];</span></span><br><span class="line"><span class="comment">// 最短路答案ans1, ans2</span></span><br><span class="line"><span class="comment">edge edg1[100005], edg2[100005];//边集</span></span><br><span class="line"><span class="comment">int main() {</span></span><br><span class="line"><span class="comment"> memset(head1, -1, sizeof(head1));//head初始化为-1</span></span><br><span class="line"><span class="comment"> memset(head2, -1, sizeof(head2));</span></span><br><span class="line"><span class="comment"> memset(ans1, 0x3f, sizeof(ans1));//ans初始化为很大值</span></span><br><span class="line"><span class="comment"> memset(ans2, 0x3f, sizeof(ans2));</span></span><br><span class="line"><span class="comment"> cin >> n >> m;</span></span><br><span class="line"><span class="comment"> for(int i = 0; i < m; i++) {</span></span><br><span class="line"><span class="comment"> int a, b, c;</span></span><br><span class="line"><span class="comment"> cin >> a >> b >>c;//起点终点权值</span></span><br><span class="line"><span class="comment"> edg1[i].to = b;</span></span><br><span class="line"><span class="comment"> edg1[i].val = c;</span></span><br><span class="line"><span class="comment"> edg1[i].next = head1[a];</span></span><br><span class="line"><span class="comment"> head1[a] = i;</span></span><br><span class="line"><span class="comment"> edg2[i].to = a;</span></span><br><span class="line"><span class="comment"> edg2[i].val = c;</span></span><br><span class="line"><span class="comment"> edg2[i].next = head2[b];</span></span><br><span class="line"><span class="comment"> head2[b] = i;</span></span><br><span class="line"><span class="comment"> }</span></span><br><span class="line"><span class="comment"> priority_queue<node> que;</span></span><br><span class="line"><span class="comment"> que.push({1, 0});</span></span><br><span class="line"><span class="comment"> ans1[1] = 0;</span></span><br><span class="line"><span class="comment"> while(!que.empty()) {</span></span><br><span class="line"><span class="comment"> node temp = que.top();</span></span><br><span class="line"><span class="comment"> que.pop();</span></span><br><span class="line"><span class="comment"> if(temp.val > ans1[temp.now]) {//结点到这为止的路径大于计数的这个</span></span><br><span class="line"><span class="comment"> continue;</span></span><br><span class="line"><span class="comment"> }//小的话松弛</span></span><br><span class="line"><span class="comment"> for(int cnt = head1[temp.now]; cnt != -1; cnt = edg1[cnt].next) {</span></span><br><span class="line"><span class="comment"> if(ans1[edg1[cnt].to] > temp.val + edg1[cnt].val) {</span></span><br><span class="line"><span class="comment"> //终点的值 > 起点的值+权值</span></span><br><span class="line"><span class="comment"> ans1[edg1[cnt].to] = temp.val + edg1[cnt].val;</span></span><br><span class="line"><span class="comment"> que.push({edg1[cnt].to, ans1[edg1[cnt].to]});//终点,答案入队</span></span><br><span class="line"><span class="comment"> } </span></span><br><span class="line"><span class="comment"> }</span></span><br><span class="line"><span class="comment"> }</span></span><br><span class="line"><span class="comment"> que.push({1, 0});</span></span><br><span class="line"><span class="comment"> ans2[1] = 0;</span></span><br><span class="line"><span class="comment"> while(!que.empty()) {</span></span><br><span class="line"><span class="comment"> node temp = que.top();</span></span><br><span class="line"><span class="comment"> que.pop();</span></span><br><span class="line"><span class="comment"> if(temp.val > ans2[temp.now]) {</span></span><br><span class="line"><span class="comment"> continue;</span></span><br><span class="line"><span class="comment"> }</span></span><br><span class="line"><span class="comment"> for(int cnt = head2[temp.now]; cnt != -1; cnt = edg2[cnt].next) {</span></span><br><span class="line"><span class="comment"> if(ans2[edg2[cnt].to] > temp.val + edg2[cnt].val) {</span></span><br><span class="line"><span class="comment"> ans2[edg2[cnt].to] = temp.val + edg2[cnt].val;</span></span><br><span class="line"><span class="comment"> que.push({edg2[cnt].to, ans2[edg2[cnt].to]});</span></span><br><span class="line"><span class="comment"> } </span></span><br><span class="line"><span class="comment"> }</span></span><br><span class="line"><span class="comment"> }</span></span><br><span class="line"><span class="comment"> int ans = 0;</span></span><br><span class="line"><span class="comment"> for(int i = 2; i <= n; i++) {</span></span><br><span class="line"><span class="comment"> ans += ans1[i] + ans2[i];</span></span><br><span class="line"><span class="comment"> }</span></span><br><span class="line"><span class="comment"> cout << ans << endl;</span></span><br><span class="line"><span class="comment"> return 0;</span></span><br><span class="line"><span class="comment">}</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="comment">/*SPFA版本*/</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><iostream></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><queue></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><cstring></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">edge</span> {</span></span><br><span class="line"> <span class="keyword">int</span> to, val, next;</span><br><span class="line">};</span><br><span class="line"></span><br><span class="line"><span class="keyword">int</span> n, m, head1[<span class="number">1005</span>], head2[<span class="number">1005</span>], ans1[<span class="number">1005</span>], ans2[<span class="number">1005</span>], que_mark[<span class="number">1005</span>];</span><br><span class="line">edge edg1[<span class="number">100005</span>], edg2[<span class="number">100005</span>];</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="built_in">memset</span>(head1, <span class="number">-1</span>, <span class="keyword">sizeof</span>(head1));</span><br><span class="line"> <span class="built_in">memset</span>(head2, <span class="number">-1</span>, <span class="keyword">sizeof</span>(head2));</span><br><span class="line"> <span class="built_in">memset</span>(ans1, <span class="number">0x3F</span>, <span class="keyword">sizeof</span>(ans1));</span><br><span class="line"> <span class="built_in">memset</span>(ans2, <span class="number">0x3F</span>, <span class="keyword">sizeof</span>(ans2));</span><br><span class="line"> <span class="built_in">cin</span> >> n >> m;</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i < m; i++) {</span><br><span class="line"> <span class="keyword">int</span> a, b, c;</span><br><span class="line"> <span class="built_in">cin</span> >> a >> b >> c;</span><br><span class="line"> edg1[i].to = b;</span><br><span class="line"> edg1[i].val = c;</span><br><span class="line"> edg1[i].next = head1[a];</span><br><span class="line"> head1[a] = i;</span><br><span class="line"> edg2[i].to = a;</span><br><span class="line"> edg2[i].val = c;</span><br><span class="line"> edg2[i].next = head2[b];</span><br><span class="line"> head2[b] = i;</span><br><span class="line"> }</span><br><span class="line"> ans1[<span class="number">1</span>] = <span class="number">0</span>;</span><br><span class="line"> <span class="built_in">queue</span><<span class="keyword">int</span>> que;</span><br><span class="line"> que.push(<span class="number">1</span>);</span><br><span class="line"> que_mark[<span class="number">1</span>] = <span class="number">1</span>;</span><br><span class="line"> <span class="keyword">while</span> (!que.empty()) {</span><br><span class="line"> <span class="keyword">int</span> t = que.front();</span><br><span class="line"> que.pop();</span><br><span class="line"> que_mark[t] = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> cnt = head1[t]; cnt != <span class="number">-1</span>; cnt = edg1[cnt].next) {</span><br><span class="line"> <span class="keyword">if</span> (ans1[edg1[cnt].to] > ans1[t] + edg1[cnt].val) {</span><br><span class="line"> ans1[edg1[cnt].to] = ans1[t] + edg1[cnt].val;</span><br><span class="line"> <span class="keyword">if</span> (que_mark[edg1[cnt].to] == <span class="number">0</span>) {</span><br><span class="line"> que_mark[edg1[cnt].to] = <span class="number">1</span>;</span><br><span class="line"> que.push(edg1[cnt].to);</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> ans2[<span class="number">1</span>] = <span class="number">0</span>;</span><br><span class="line"> que.push(<span class="number">1</span>);</span><br><span class="line"> que_mark[<span class="number">1</span>] = <span class="number">1</span>;</span><br><span class="line"> <span class="keyword">while</span> (!que.empty()) {</span><br><span class="line"> <span class="keyword">int</span> t = que.front();</span><br><span class="line"> que.pop();</span><br><span class="line"> que_mark[t] = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> cnt = head2[t]; cnt != <span class="number">-1</span>; cnt = edg2[cnt].next) {</span><br><span class="line"> <span class="keyword">if</span> (ans2[edg2[cnt].to] > ans2[t] + edg2[cnt].val) {</span><br><span class="line"> ans2[edg2[cnt].to] = ans2[t] + edg2[cnt].val;</span><br><span class="line"> <span class="keyword">if</span> (que_mark[edg2[cnt].to] == <span class="number">0</span>) {</span><br><span class="line"> que_mark[edg2[cnt].to] = <span class="number">1</span>;</span><br><span class="line"> que.push(edg2[cnt].to);</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">int</span> ans = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">2</span>; i <= n; i++) {</span><br><span class="line"> ans += ans1[i] + ans2[i];</span><br><span class="line"> }</span><br><span class="line"> <span class="built_in">cout</span> << ans << <span class="built_in">endl</span>;</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h1 id="P1144-最短路计数"><a href="#P1144-最短路计数" class="headerlink" title="P1144 最短路计数"></a><a href="https://www.luogu.com.cn/problem/P1144" target="_blank" rel="noopener">P1144 最短路计数</a></h1><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><iostream></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><queue></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><cstring></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">edge</span> {</span></span><br><span class="line"> <span class="keyword">int</span> to, val, next;</span><br><span class="line">};</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">node</span> {</span></span><br><span class="line"> <span class="keyword">int</span> now, val;</span><br><span class="line"> <span class="keyword">bool</span> <span class="keyword">operator</span>< (<span class="keyword">const</span> node &b) <span class="keyword">const</span> {</span><br><span class="line"> <span class="keyword">return</span> <span class="keyword">this</span>->val > b.val;</span><br><span class="line"> }</span><br><span class="line">};</span><br><span class="line"></span><br><span class="line">edge edg[<span class="number">4000005</span>];<span class="comment">//边集合</span></span><br><span class="line"><span class="keyword">int</span> n, m, edg_cnt, head[<span class="number">1000005</span>], ans[<span class="number">1000005</span>], ans_cnt[<span class="number">1000005</span>];</span><br><span class="line"><span class="comment">//edg_cnt第几条边了, ans最短路径是多少, ans_cnt到这位置最短路方案</span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="built_in">memset</span>(head, <span class="number">-1</span>, <span class="keyword">sizeof</span>(head));</span><br><span class="line"> <span class="built_in">memset</span>(ans, <span class="number">0x3F</span>, <span class="keyword">sizeof</span>(ans));</span><br><span class="line"> <span class="built_in">memset</span>(ans_cnt, <span class="number">0</span>, <span class="keyword">sizeof</span>(ans_cnt));</span><br><span class="line"> <span class="built_in">cin</span> >> n >> m;</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i < m; i++) {<span class="comment">//m条边</span></span><br><span class="line"> <span class="keyword">int</span> a, b;</span><br><span class="line"> <span class="built_in">cin</span> >> a >> b;<span class="comment">//a-b之间有边</span></span><br><span class="line"> edg[edg_cnt].to = b;<span class="comment">//终点b</span></span><br><span class="line"> edg[edg_cnt].val = <span class="number">1</span>;<span class="comment">//权值为1</span></span><br><span class="line"> edg[edg_cnt].next = head[a];<span class="comment">//这条边的下个一是起点的头</span></span><br><span class="line"> head[a] = edg_cnt;<span class="comment">//起点的头等于此时</span></span><br><span class="line"> edg_cnt++;<span class="comment">//往后走一个</span></span><br><span class="line"> edg[edg_cnt].to = a;</span><br><span class="line"> edg[edg_cnt].val = <span class="number">1</span>;</span><br><span class="line"> edg[edg_cnt].next = head[b];</span><br><span class="line"> head[b] = edg_cnt;</span><br><span class="line"> edg_cnt++;</span><br><span class="line"> }</span><br><span class="line"> priority_queue<node> que;<span class="comment">//优先队列</span></span><br><span class="line"> que.push({<span class="number">1</span>, <span class="number">0</span>});<span class="comment">//从1开始</span></span><br><span class="line"> ans[<span class="number">1</span>] = <span class="number">0</span>;<span class="comment">//最短路为0</span></span><br><span class="line"> ans_cnt[<span class="number">1</span>] = <span class="number">1</span>;<span class="comment">//方案数为1</span></span><br><span class="line"> <span class="keyword">while</span> (!que.empty()) {</span><br><span class="line"> node temp = que.top();</span><br><span class="line"> que.pop();</span><br><span class="line"> <span class="keyword">if</span> (temp.val > ans[temp.now]) {<span class="comment">//答案比他还小</span></span><br><span class="line"> <span class="keyword">continue</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> cnt = head[temp.now]; cnt != <span class="number">-1</span>; cnt = edg[cnt].next) {</span><br><span class="line"> </span><br><span class="line"> <span class="keyword">if</span> (ans[edg[cnt].to] > temp.val + edg[cnt].val) {</span><br><span class="line"> ans[edg[cnt].to] = temp.val + edg[cnt].val;</span><br><span class="line"> ans_cnt[edg[cnt].to] = ans_cnt[temp.now];<span class="comment">//方案答案也得更新</span></span><br><span class="line"> que.push({edg[cnt].to, ans[edg[cnt].to]});<span class="comment">//终点,权值</span></span><br><span class="line"> } <span class="keyword">else</span> <span class="keyword">if</span> (ans[edg[cnt].to] == temp.val + edg[cnt].val) {</span><br><span class="line"> <span class="comment">//相等的时候,之前已经更新过,此事是第二种方法</span></span><br><span class="line"> ans_cnt[edg[cnt].to] += ans_cnt[temp.now];<span class="comment">//方法累加</span></span><br><span class="line"> ans_cnt[edg[cnt].to] %= <span class="number">100003</span>;<span class="comment">//答案%</span></span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i <= n; i++) {<span class="comment">//1能到的所有边的方法数</span></span><br><span class="line"> <span class="built_in">cout</span> << ans_cnt[i] << <span class="built_in">endl</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h1 id="P1342-请柬"><a href="#P1342-请柬" class="headerlink" title="P1342 请柬"></a><a href="https://www.luogu.com.cn/problem/P1342" target="_blank" rel="noopener">P1342 请柬</a></h1><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><iostream></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><cstring></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><queue></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><cstdio></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">edge</span> {</span></span><br><span class="line"> <span class="keyword">int</span> to, next, val;<span class="comment">//终点,下一条边,权值</span></span><br><span class="line">};</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">node</span> {</span></span><br><span class="line"> <span class="keyword">int</span> now, val;<span class="comment">////现在在哪,到这位置路径最小多少</span></span><br><span class="line"> <span class="keyword">bool</span> <span class="keyword">operator</span>< (<span class="keyword">const</span> node &b) <span class="keyword">const</span> {</span><br><span class="line"> <span class="keyword">return</span> <span class="keyword">this</span>->now > b.now;</span><br><span class="line"> }</span><br><span class="line">};</span><br><span class="line"></span><br><span class="line">edge edg[<span class="number">1000005</span>], edg2[<span class="number">1000005</span>];<span class="comment">//去回</span></span><br><span class="line"><span class="keyword">int</span> n, m, head[<span class="number">1000005</span>], head2[<span class="number">1000005</span>], ans[<span class="number">1000005</span>], ans2[<span class="number">1000005</span>];</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="built_in">memset</span>(ans, <span class="number">0x3F</span>, <span class="keyword">sizeof</span>(ans));</span><br><span class="line"> <span class="built_in">memset</span>(ans2, <span class="number">0x3F</span>, <span class="keyword">sizeof</span>(ans2));</span><br><span class="line"> <span class="built_in">memset</span>(head, <span class="number">-1</span>, <span class="keyword">sizeof</span>(head));</span><br><span class="line"> <span class="built_in">memset</span>(head2, <span class="number">-1</span>, <span class="keyword">sizeof</span>(head2));</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d%d"</span>, &n, &m);</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i <= m; i++) {</span><br><span class="line"> <span class="keyword">int</span> a, b, c;</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d%d%d"</span>, &a, &b, &c);</span><br><span class="line"> edg[i].to = b;</span><br><span class="line"> edg[i].val = c;</span><br><span class="line"> edg[i].next = head[a];</span><br><span class="line"> head[a] = i;</span><br><span class="line"> edg2[i].to = a;</span><br><span class="line"> edg2[i].val = c;</span><br><span class="line"> edg2[i].next = head2[b];</span><br><span class="line"> head2[b] = i;</span><br><span class="line"> }</span><br><span class="line"> priority_queue<node> que;</span><br><span class="line"> que.push((node){<span class="number">1</span>, <span class="number">0</span>});</span><br><span class="line"> ans[<span class="number">1</span>] = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">while</span> (!que.empty()) {</span><br><span class="line"> node temp = que.top();</span><br><span class="line"> que.pop();</span><br><span class="line"> <span class="keyword">if</span> (temp.val > ans[temp.now]) {</span><br><span class="line"> <span class="keyword">continue</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> cnt = head[temp.now]; cnt != <span class="number">-1</span>; cnt = edg[cnt].next) {</span><br><span class="line"> <span class="keyword">if</span> (ans[edg[cnt].to] > temp.val + edg[cnt].val) {</span><br><span class="line"> ans[edg[cnt].to] = temp.val + edg[cnt].val;</span><br><span class="line"> que.push((node){edg[cnt].to, ans[edg[cnt].to]});</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> que.push((node){<span class="number">1</span>, <span class="number">0</span>});</span><br><span class="line"> ans2[<span class="number">1</span>] = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">while</span> (!que.empty()) {</span><br><span class="line"> node temp = que.top();</span><br><span class="line"> que.pop();</span><br><span class="line"> <span class="keyword">if</span> (temp.val > ans2[temp.now]) {</span><br><span class="line"> <span class="keyword">continue</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> cnt = head2[temp.now]; cnt != <span class="number">-1</span>; cnt = edg2[cnt].next) {</span><br><span class="line"> <span class="keyword">if</span> (ans2[edg2[cnt].to] > temp.val + edg2[cnt].val) {</span><br><span class="line"> ans2[edg2[cnt].to] = temp.val + edg2[cnt].val;</span><br><span class="line"> que.push((node){edg2[cnt].to, ans2[edg2[cnt].to]});</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">long</span> <span class="keyword">long</span> fin = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">2</span>; i <= n; i++) {</span><br><span class="line"> fin += ans[i] + ans2[i];</span><br><span class="line"> }</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%ld\n"</span>, fin);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h1 id="P1608-路径统计"><a href="#P1608-路径统计" class="headerlink" title="P1608 路径统计"></a><a href="https://www.luogu.com.cn/problem/P1608" target="_blank" rel="noopener">P1608 路径统计</a></h1><p>最少花费和花费最少的路径的总数.</p><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><iostream></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><queue></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><cstring></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">edge</span> {</span></span><br><span class="line"> <span class="keyword">int</span> to, val, next;</span><br><span class="line">};</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">node</span> {</span></span><br><span class="line"> <span class="keyword">int</span> now, val;</span><br><span class="line"> <span class="keyword">bool</span> <span class="keyword">operator</span>< (<span class="keyword">const</span> node &b) <span class="keyword">const</span> {</span><br><span class="line"> <span class="keyword">return</span> <span class="keyword">this</span>->val > b.val;</span><br><span class="line"> }</span><br><span class="line">};</span><br><span class="line"></span><br><span class="line">edge edg[<span class="number">4000005</span>];</span><br><span class="line"><span class="keyword">char</span> mark[<span class="number">2001</span>][<span class="number">2001</span>];</span><br><span class="line"><span class="keyword">int</span> n, m, edg_cnt, head[<span class="number">2004</span>], ans[<span class="number">2005</span>], ans_cnt[<span class="number">2005</span>];</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="built_in">memset</span>(head, <span class="number">-1</span>, <span class="keyword">sizeof</span>(head));</span><br><span class="line"> <span class="built_in">memset</span>(ans, <span class="number">0x3F</span>, <span class="keyword">sizeof</span>(ans));</span><br><span class="line"> <span class="built_in">memset</span>(ans_cnt, <span class="number">0</span>, <span class="keyword">sizeof</span>(ans_cnt));</span><br><span class="line"> <span class="built_in">cin</span> >> n >> m;</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i < m; i++) {</span><br><span class="line"> <span class="keyword">int</span> a, b, c;</span><br><span class="line"> <span class="built_in">cin</span> >> a >> b >> c;</span><br><span class="line"> <span class="keyword">if</span> (mark[a][b] == <span class="number">0</span>) {</span><br><span class="line"> mark[a][b] = <span class="number">1</span>;</span><br><span class="line"> edg[edg_cnt].to = b;</span><br><span class="line"> edg[edg_cnt].val = c;</span><br><span class="line"> edg[edg_cnt].next = head[a];</span><br><span class="line"> head[a] = edg_cnt;</span><br><span class="line"> edg_cnt++;</span><br><span class="line"> } <span class="keyword">else</span> {</span><br><span class="line"> <span class="keyword">int</span> cnt = head[a];</span><br><span class="line"> <span class="keyword">while</span> (edg[cnt].to != b) {</span><br><span class="line"> cnt = edg[cnt].next;</span><br><span class="line"> }</span><br><span class="line"> edg[cnt].val = min(edg[cnt].val, c);</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> priority_queue<node> que;</span><br><span class="line"> que.push({<span class="number">1</span>, <span class="number">0</span>});</span><br><span class="line"> ans[<span class="number">1</span>] = <span class="number">0</span>;</span><br><span class="line"> ans_cnt[<span class="number">1</span>] = <span class="number">1</span>;</span><br><span class="line"> <span class="keyword">while</span> (!que.empty()) {</span><br><span class="line"> node temp = que.top();</span><br><span class="line"> que.pop();</span><br><span class="line"> <span class="keyword">if</span> (temp.val > ans[temp.now]) {</span><br><span class="line"> <span class="keyword">continue</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> cnt = head[temp.now]; cnt != <span class="number">-1</span>; cnt = edg[cnt].next) {</span><br><span class="line"> <span class="keyword">if</span> (ans[edg[cnt].to] > temp.val + edg[cnt].val) {</span><br><span class="line"> ans[edg[cnt].to] = temp.val + edg[cnt].val;</span><br><span class="line"> ans_cnt[edg[cnt].to] = ans_cnt[temp.now];</span><br><span class="line"> que.push({edg[cnt].to, ans[edg[cnt].to]});</span><br><span class="line"> } <span class="keyword">else</span> <span class="keyword">if</span> (ans[edg[cnt].to] == temp.val + edg[cnt].val) {</span><br><span class="line"> ans_cnt[edg[cnt].to] += ans_cnt[temp.now];</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span> (ans[n] != <span class="number">0x3F3F3F3F</span>) {</span><br><span class="line"> <span class="built_in">cout</span> << ans[n] << <span class="string">" "</span> << ans_cnt[n] << <span class="built_in">endl</span>;</span><br><span class="line"> } <span class="keyword">else</span> {</span><br><span class="line"> <span class="built_in">cout</span> << <span class="string">"No answer"</span> << <span class="built_in">endl</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h1 id="P4667-BalticOI-2011-Day1-Switch-the-Lamp-On"><a href="#P4667-BalticOI-2011-Day1-Switch-the-Lamp-On" class="headerlink" title="P4667 [BalticOI 2011 Day1]Switch the Lamp On"></a><a href="https://www.luogu.com.cn/problem/P4667" target="_blank" rel="noopener">P4667 [BalticOI 2011 Day1]Switch the Lamp On</a></h1><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><iostream></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><cstring></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><queue></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">edge</span> {</span></span><br><span class="line"> <span class="keyword">int</span> to, val, next;<span class="comment">//终点权值下一条边</span></span><br><span class="line">};</span><br><span class="line"><span class="comment">//djst</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">node</span> {</span></span><br><span class="line"> <span class="keyword">int</span> now, val;<span class="comment">//现在在哪,到这权值是多少</span></span><br><span class="line"> <span class="keyword">bool</span> <span class="keyword">operator</span>< (<span class="keyword">const</span> node &b) <span class="keyword">const</span> {</span><br><span class="line"> <span class="keyword">return</span> <span class="keyword">this</span>->val > b.val;</span><br><span class="line"> }</span><br><span class="line">};</span><br><span class="line"></span><br><span class="line">edge edg[<span class="number">1100005</span>];<span class="comment">//边</span></span><br><span class="line"><span class="keyword">int</span> n, m, edg_cnt, head[<span class="number">260005</span>], ans[<span class="number">260005</span>];</span><br><span class="line"><span class="comment">//第几条边,</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">add_edg</span><span class="params">(<span class="keyword">int</span> a, <span class="keyword">int</span> b, <span class="keyword">int</span> v)</span> </span>{</span><br><span class="line"> edg[edg_cnt].to = b;</span><br><span class="line"> edg[edg_cnt].val = v;</span><br><span class="line"> edg[edg_cnt].next = head[a];</span><br><span class="line"> head[a] = edg_cnt;</span><br><span class="line"> edg_cnt++;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="built_in">memset</span>(head, <span class="number">-1</span>, <span class="keyword">sizeof</span>(head));<span class="comment">//head = -1</span></span><br><span class="line"> <span class="built_in">memset</span>(ans, <span class="number">0x3F</span>, <span class="keyword">sizeof</span>(ans));<span class="comment">//ans = 0x3f</span></span><br><span class="line"> <span class="built_in">cin</span> >> n >> m;<span class="comment">//n点m边</span></span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i < n; i++) {<span class="comment">//点</span></span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">0</span>; j < m; j++) {<span class="comment">//边</span></span><br><span class="line"> <span class="comment">//i * (m + 1) + j i * (m + 1) + j + 1</span></span><br><span class="line"> <span class="comment">//i * (m + 1) + j + m + 1i * (m + 1) + j + m + 2</span></span><br><span class="line"> <span class="keyword">char</span> t;</span><br><span class="line"> <span class="built_in">cin</span> >> t;</span><br><span class="line"> <span class="keyword">int</span> a, b, c, d;</span><br><span class="line"> <span class="keyword">if</span> (t == <span class="string">'/'</span>) {<span class="comment">//a-b 0 c-d 1</span></span><br><span class="line"> a = i * (m + <span class="number">1</span>) + j + <span class="number">1</span>;</span><br><span class="line"> b = a + m;</span><br><span class="line"> c = i * (m + <span class="number">1</span>) + j;</span><br><span class="line"> d = c + m + <span class="number">2</span>;</span><br><span class="line"> } <span class="keyword">else</span> {</span><br><span class="line"> c = i * (m + <span class="number">1</span>) + j + <span class="number">1</span>;</span><br><span class="line"> d = c + m;</span><br><span class="line"> a = i * (m + <span class="number">1</span>) + j;</span><br><span class="line"> b = a + m + <span class="number">2</span>;</span><br><span class="line"> }</span><br><span class="line"> add_edg(a, b, <span class="number">0</span>);<span class="comment">//a-b 0</span></span><br><span class="line"> add_edg(b, a, <span class="number">0</span>);</span><br><span class="line"> add_edg(c, d, <span class="number">1</span>);<span class="comment">//c-d 1</span></span><br><span class="line"> add_edg(d, c, <span class="number">1</span>);</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> priority_queue<node> que;</span><br><span class="line"> que.push((node){<span class="number">0</span>, <span class="number">0</span>});</span><br><span class="line"> ans[<span class="number">0</span>] = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">while</span> (!que.empty()) {</span><br><span class="line"> node temp = que.top();</span><br><span class="line"> que.pop();</span><br><span class="line"> <span class="keyword">if</span> (temp.val != ans[temp.now]) {</span><br><span class="line"> <span class="keyword">continue</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> cnt = head[temp.now]; cnt != <span class="number">-1</span>; cnt = edg[cnt].next) {</span><br><span class="line"> <span class="keyword">if</span> (ans[edg[cnt].to] > ans[temp.now] + edg[cnt].val) {</span><br><span class="line"> ans[edg[cnt].to] = ans[temp.now] + edg[cnt].val;</span><br><span class="line"> que.push((node){edg[cnt].to, ans[edg[cnt].to]});</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">int</span> x = ans[(n + <span class="number">1</span>) * (m + <span class="number">1</span>) - <span class="number">1</span>];</span><br><span class="line"> <span class="keyword">if</span> (x == <span class="number">0x3F3F3F3F</span>) {<span class="comment">//答案没被更新时</span></span><br><span class="line"> <span class="built_in">cout</span> << <span class="string">"NO SOLUTION"</span> << <span class="built_in">endl</span>;</span><br><span class="line"> } <span class="keyword">else</span> {</span><br><span class="line"> <span class="built_in">cout</span> << x << <span class="built_in">endl</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>]]></content>
<summary type="html">
<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" cla
</summary>
</entry>
<entry>
<title>Mynotes/操作系统与系统编程/IO模型大总结</title>
<link href="http://yoursite.com/2020/06/15/Mynotes/%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F%E4%B8%8E%E7%B3%BB%E7%BB%9F%E7%BC%96%E7%A8%8B/IO%E6%A8%A1%E5%9E%8B%E5%A4%A7%E6%80%BB%E7%BB%93/"/>
<id>http://yoursite.com/2020/06/15/Mynotes/%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F%E4%B8%8E%E7%B3%BB%E7%BB%9F%E7%BC%96%E7%A8%8B/IO%E6%A8%A1%E5%9E%8B%E5%A4%A7%E6%80%BB%E7%BB%93/</id>
<published>2020-06-15T07:14:25.987Z</published>
<updated>2020-06-15T07:14:25.987Z</updated>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><h1 id="IO模型大总结"><a href="#IO模型大总结" class="headerlink" title="IO模型大总结"></a>IO模型大总结</h1><h2 id="直接循环"><a href="#直接循环" class="headerlink" title="直接循环"></a>直接循环</h2><p><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1gfjovmg2jmj30vv0h3dkv.jpg" alt="image.png"></p><p>基于fork对进程</p><h2 id="reactor-dispatch-反应堆调度"><a href="#reactor-dispatch-反应堆调度" class="headerlink" title="reactor dispatch(反应堆调度)"></a><strong>reactor dispatch(反应堆调度)</strong></h2><p><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1gfjp3ap54rj30sy0i2dlb.jpg" alt="image.png"></p><h2 id="带线程池的反应堆"><a href="#带线程池的反应堆" class="headerlink" title="带线程池的反应堆"></a><strong>带线程池的反应堆</strong></h2><p><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1gfjp7papk3j30lj0ikwjs.jpg" alt="image.png"></p><h2 id="主从模式的反应堆"><a href="#主从模式的反应堆" class="headerlink" title="主从模式的反应堆"></a><strong>主从模式的反应堆</strong></h2><p><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1gfjpc7aewlj30k00ietg2.jpg" alt="image.png"></p><p>reactor dispatch反应堆调度</p><p>Acceptor受体</p><p>Main Accpet thread主接受线程</p><p>sub-reactor thread子反应器线程</p><p>worker threads工作线程</p><p>Thread Pool线程池</p><h2 id="主从线程池的反应堆"><a href="#主从线程池的反应堆" class="headerlink" title="主从线程池的反应堆"></a><strong>主从线程池的反应堆</strong></h2><p><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1gfjpgdcgcfj30cq0ik0z5.jpg" alt="image.png"></p><p>reactor dispatch反应堆调度</p><p>Acceptor受体</p><p>Main Accpet thread主接受线程</p><p>sub-reactor thread子反应器线程</p><p>worker threads工作线程</p><p>Thread Pool线程池</p>]]></content>
<summary type="html">
<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" cla
</summary>
</entry>
<entry>
<title>Mynotes/linux学习笔记/Linux操作系统结课考试(使用篇)</title>
<link href="http://yoursite.com/2020/06/15/Mynotes/linux%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/Linux%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F%E7%BB%93%E8%AF%BE%E8%80%83%E8%AF%95%EF%BC%88%E4%BD%BF%E7%94%A8%E7%AF%87%EF%BC%89/"/>
<id>http://yoursite.com/2020/06/15/Mynotes/linux%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/Linux%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F%E7%BB%93%E8%AF%BE%E8%80%83%E8%AF%95%EF%BC%88%E4%BD%BF%E7%94%A8%E7%AF%87%EF%BC%89/</id>
<published>2020-06-15T07:14:25.986Z</published>
<updated>2020-06-15T07:14:25.986Z</updated>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><h1 id="Linux操作系统结课考试(使用篇)"><a href="#Linux操作系统结课考试(使用篇)" class="headerlink" title="Linux操作系统结课考试(使用篇)"></a>Linux操作系统结课考试(使用篇)</h1><p><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1gfj0hepc84j30iv0khaaz.jpg" alt="image.png"><br><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1gfj0i7f3cgj30iu0l8dhl.jpg" alt="image.png"><br><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1gfj0ioqq8bj30iq0l0myk.jpg" alt="image.png"><br><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1gfj0ji3htwj30iu17wacr.jpg" alt="image.png"><br><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1gfj0k5zrr1j30ij14wdi2.jpg" alt="image.png"><br><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1gfj0lsdc3ej30ik16s41h.jpg" alt="image.png"><br><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1gfj0marqg8j30ik15kwh2.jpg" alt="image.png"><br><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1gfj0mz5ulnj30ii17eada.jpg" alt="image.png"><br><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1gfj0nepmnoj30ij173q5q.jpg" alt="image.png"><br><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1gfj0nytu4fj30is139mzr.jpg" alt="image.png"><br><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1gfj0oczsjbj30iv19qtc1.jpg" alt="image.png"><br><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1gfj0owdqiwj30in17sgp8.jpg" alt="image.png"><br><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1gfj0x5a32aj30im19sad2.jpg" alt="image.png"><br><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1gfj0xjpqobj30in18htc6.jpg" alt="image.png"><br><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1gfj0y4q6l0j30iy1aogpe.jpg" alt="image.png"><br><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1gfj0yihfiuj30io19ngph.jpg" alt="image.png"><br><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1gfj0yywfg7j30is17zq6t.jpg" alt="image.png"><br><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1gfj0zdiqc9j30ii0g1t9p.jpg" alt="image.png"></p>]]></content>
<summary type="html">
<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" cla
</summary>
</entry>
<entry>
<title>Mynotes/算法/欧拉计划</title>
<link href="http://yoursite.com/2020/06/02/Mynotes/%E7%AE%97%E6%B3%95/%E6%AC%A7%E6%8B%89%E8%AE%A1%E5%88%92/"/>
<id>http://yoursite.com/2020/06/02/Mynotes/%E7%AE%97%E6%B3%95/%E6%AC%A7%E6%8B%89%E8%AE%A1%E5%88%92/</id>
<published>2020-06-02T06:27:31.379Z</published>
<updated>2020-06-02T06:27:31.379Z</updated>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><p>[TOC]</p><h2 id="EP01-3和5的倍数(等差数列)"><a href="#EP01-3和5的倍数(等差数列)" class="headerlink" title="EP01 - 3和5的倍数(等差数列)"></a>EP01 - 3和5的倍数(等差数列)</h2><blockquote><p>如果我们列出10以内所有3或5的倍数,我们将得到3、5、6和9,这些数的和是23。</p><p>求1000以内所有3或5的倍数的和。</p></blockquote><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iostream></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstdio></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cmath></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstring></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iomanip></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><algorithm></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><map></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><vector></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">int is_val(int n) {</span></span><br><span class="line"><span class="comment"> if(n % 3 == 0 || n % 5 == 0)return 1;</span></span><br><span class="line"><span class="comment"> return 0;</span></span><br><span class="line"><span class="comment">}</span></span><br><span class="line"><span class="comment">int main() {</span></span><br><span class="line"><span class="comment"> int sum = 0;</span></span><br><span class="line"><span class="comment"> for(int i = 1; i < 1000; i++) {</span></span><br><span class="line"><span class="comment"> if(!is_val(i))continue;</span></span><br><span class="line"><span class="comment"> sum += i;</span></span><br><span class="line"><span class="comment"> </span></span><br><span class="line"><span class="comment"> }</span></span><br><span class="line"><span class="comment"> printf("%d\n", sum);</span></span><br><span class="line"><span class="comment"> return 0;</span></span><br><span class="line"><span class="comment">}</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{ <span class="comment">//等差数列</span></span><br><span class="line"> <span class="keyword">int</span> sum3 = (<span class="number">3</span> + <span class="number">999</span>) * <span class="number">333</span> / <span class="number">2</span>;</span><br><span class="line"> <span class="keyword">int</span> sum5 = (<span class="number">5</span> + <span class="number">995</span>) * (<span class="number">995</span> / <span class="number">5</span>) / <span class="number">2</span>;</span><br><span class="line"> <span class="keyword">int</span> sum15 = (<span class="number">15</span> + (<span class="number">999</span> / <span class="number">15</span>) * <span class="number">15</span>) * (<span class="number">999</span> / <span class="number">15</span>) /<span class="number">2</span>;</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d\n"</span>, sum3 + sum5 - sum15);</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h2 id="EP02-斐波那契数(滚动数组)"><a href="#EP02-斐波那契数(滚动数组)" class="headerlink" title="EP02 - 斐波那契数(滚动数组)"></a>EP02 - 斐波那契数(滚动数组)</h2><blockquote><p>斐波那契数列中的每个新项都是通过将前两个项相加而生成的。从1和2开始,前10个术语将是:<br>1,2,3,5,8,13,21,34,55,89,…<br>通过考虑斐波那契数列中值不超过四百万的项,找到偶值项的总和。</p></blockquote><table><thead><tr><th>0</th><th>1</th><th>2</th></tr></thead><tbody><tr><td>1</td><td>2</td><td>3</td></tr><tr><td>5</td><td>8</td><td>13</td></tr><tr><td>21</td><td>……</td><td></td></tr></tbody></table><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iostream></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstdio></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cmath></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstring></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iomanip></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><algorithm></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><map></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><vector></span></span></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">#define max_n 44</span></span><br><span class="line"><span class="comment">using namespace std;</span></span><br><span class="line"><span class="comment">int fib[max_n + 5];</span></span><br><span class="line"><span class="comment">#define N 4000000</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">int main() {</span></span><br><span class="line"><span class="comment"> fib[1] = 1, fib[2] = 2;</span></span><br><span class="line"><span class="comment"> int sum = 2;</span></span><br><span class="line"><span class="comment"> for (int i = 3; i <= max_n && (fib[i - 1] + fib[i - 2] <= N); i++) {</span></span><br><span class="line"><span class="comment"> fib[i] = fib[i - 1] + fib[i - 2];</span></span><br><span class="line"><span class="comment"> if(fib[i] & 1)continue;</span></span><br><span class="line"><span class="comment"> sum += fib[i];</span></span><br><span class="line"><span class="comment"> }</span></span><br><span class="line"><span class="comment"> cout << sum << endl;</span></span><br><span class="line"><span class="comment"> return 0;</span></span><br><span class="line"><span class="comment">}</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">#define N 4000000</span></span><br><span class="line"><span class="comment">int main() {</span></span><br><span class="line"><span class="comment"> int sum = 0, n=1;</span></span><br><span class="line"><span class="comment"> int a = 0, b = 1, c;</span></span><br><span class="line"><span class="comment"> while(a + b <= N) {</span></span><br><span class="line"><span class="comment"> n += 1;</span></span><br><span class="line"><span class="comment"> b = a + b;</span></span><br><span class="line"><span class="comment"> a = b - a;</span></span><br><span class="line"><span class="comment"> if(b&1) continue;</span></span><br><span class="line"><span class="comment"> sum+=b;</span></span><br><span class="line"><span class="comment"> }</span></span><br><span class="line"><span class="comment"> printf("%d %d\n",n, sum);</span></span><br><span class="line"><span class="comment"> return 0;</span></span><br><span class="line"><span class="comment">}</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> N 4000000</span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">int</span> f[<span class="number">3</span>] = {<span class="number">0</span>, <span class="number">1</span>};</span><br><span class="line"> <span class="keyword">int</span> sum = <span class="number">0</span>, n = <span class="number">2</span>;</span><br><span class="line"> <span class="keyword">while</span>(f[(n - <span class="number">1</span>) % <span class="number">3</span>] + f[(n - <span class="number">2</span>) % <span class="number">3</span>] <= N) {</span><br><span class="line"> f[n % <span class="number">3</span>] = f[(n - <span class="number">1</span>) % <span class="number">3</span>] + f[(n - <span class="number">2</span>) % <span class="number">3</span>];</span><br><span class="line"> <span class="keyword">if</span>(!(f[n % <span class="number">3</span>] & <span class="number">1</span>)) sum += f[n%<span class="number">3</span>];</span><br><span class="line"> n += <span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d\n"</span>, sum);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h2 id="EP03-最大素数-消除合数"><a href="#EP03-最大素数-消除合数" class="headerlink" title="EP03-最大素数 (消除合数)"></a>EP03-最大素数 (消除合数)</h2><blockquote><p>13195的主要因子是5、7、13和29。<br>什么是600851475143的最大素数?</p></blockquote><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iostream></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstdio></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cmath></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstring></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iomanip></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><algorithm></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><map></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><vector></span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> N 600851475143LL</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">long</span> <span class="keyword">long</span> ans = <span class="number">0</span>, M = N;</span><br><span class="line"> <span class="keyword">long</span> <span class="keyword">long</span> i = <span class="number">2</span>;</span><br><span class="line"> <span class="keyword">while</span>(i * i <= M) {</span><br><span class="line"> <span class="keyword">if</span>(M % i == <span class="number">0</span>)ans = i; <span class="comment">//ans一定是素因子</span></span><br><span class="line"> <span class="keyword">while</span>(M % i == <span class="number">0</span>)M /= i;</span><br><span class="line"> i += <span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span>(M > <span class="number">1</span>)ans = M;</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h2 id="EP04-最大的回文数-任意进制"><a href="#EP04-最大的回文数-任意进制" class="headerlink" title="EP04-最大的回文数 (任意进制)"></a>EP04-最大的回文数 (任意进制)</h2><blockquote><p>回文数在两个方向上都相同。由两个两位数的乘积构成的最大回文数为9009 = 91×99。<br>查找由两个3位数字的乘积组成的最大回文。</p></blockquote><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iostream></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstdio></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cmath></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstring></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iomanip></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><algorithm></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><map></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><vector></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">is_val</span><span class="params">(<span class="keyword">int</span> n, <span class="keyword">int</span> base)</span> </span>{ <span class="comment">//base是任意进制数</span></span><br><span class="line"> <span class="keyword">int</span> x = n, temp = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">while</span>(x) {</span><br><span class="line"> temp = temp * base + x % base;</span><br><span class="line"> x /= base;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> temp == n;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">int</span> ans = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">100</span>; i <= <span class="number">999</span>; i++) {</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> j = i; j <= <span class="number">999</span>; j++) {</span><br><span class="line"> <span class="keyword">if</span>(is_val(i * j, <span class="number">10</span>) &&i * j > ans ) ans = i * j;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d\n"</span>, ans);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h2 id="EP06-平方和差(公式推导)"><a href="#EP06-平方和差(公式推导)" class="headerlink" title="EP06-平方和差(公式推导)"></a>EP06-平方和差(公式推导)</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure><p><strong>平方和推导公式</strong>:</p><p><img src="../%E5%9B%BE%E7%89%87/%E6%8E%A8%E5%AF%BC%E5%B9%B3%E6%96%B9%E5%92%8C%E5%85%AC%E5%BC%8F.png" alt=""></p><p>如果推导四次方和,类似;</p><h2 id="EP28-数螺旋对角线(改进平方和公式)"><a href="#EP28-数螺旋对角线(改进平方和公式)" class="headerlink" title="EP28-数螺旋对角线(改进平方和公式)"></a>EP28-数螺旋对角线(改进平方和公式)</h2><blockquote><p>从数字1开始,沿顺时针方向向右移动,形成了5 x 5的螺旋,如下所示:</p><p><strong>21</strong> 22 23 24 <strong>25</strong><br>20 <strong>7</strong> 8 <strong>9</strong> 10<br>19 6 <strong>1</strong> 2 11<br>18 <strong>5</strong> 4 <strong>3</strong> 12<br><strong>17</strong> 16 15 14 <strong>13</strong></p><p>可以验证对角线上的数字之和为101。</p><p>以相同方式形成的1001 x 1001螺旋中的对角线上的数字的总和是多少?</p></blockquote><p><img src="../%E5%9B%BE%E7%89%87/EP28.png" alt=""></p><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iostream></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstdio></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cmath></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstring></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iomanip></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><algorithm></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><map></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><vector></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">int</span> sum = <span class="number">1</span>;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> l = <span class="number">3</span>; l <= <span class="number">1001</span>; l += <span class="number">2</span>) {</span><br><span class="line"> sum += <span class="number">4</span> * l * l - <span class="number">6</span> * l + <span class="number">6</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">// sum = 4(3^2 + 5^2 + 7^2 + .. + 1001^2) - 6(3 + 5 + 7 + ... ) + C</span></span><br><span class="line"> <span class="comment">//平方和与n项和公式</span></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d\n"</span>, sum);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h2 id="EP30-数字五次幂(如何求上限)"><a href="#EP30-数字五次幂(如何求上限)" class="headerlink" title="EP30-数字五次幂(如何求上限)"></a>EP30-数字五次幂(如何求上限)</h2><p>令人惊讶的是,只有三个数字可以写成它们的数字的四次幂之和:</p><blockquote><p>1634 = 1 4 + 6 4 + 3 4 + 4 4<br>8208 = 8 4 + 2 4 + 0 4 + 8 4<br>9474 = 9 4 + 4 4 + 7 4 + 4 4</p></blockquote><p>由于1 = 1 4不是总和,因此不包括在内。</p><p>这些数字的总和为1634 + 8208 + 9474 = 19316。</p><p>查找所有可以写为数字的五次幂之和的数字之和。</p><p><img src="../%E5%9B%BE%E7%89%87/EP30.png" alt=""></p><p><img src="../%E5%9B%BE%E7%89%87/EP30-1.png" alt=""></p><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iostream></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstdio></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cmath></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstring></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iomanip></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><algorithm></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><map></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><vector></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> max_n 354294</span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">is_val</span><span class="params">(<span class="keyword">int</span> n)</span> </span>{</span><br><span class="line"> <span class="keyword">int</span> x = n, tmp = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">while</span>(x) {</span><br><span class="line"> tmp += (<span class="keyword">int</span>)<span class="built_in">pow</span>(x % <span class="number">10</span>, <span class="number">5</span>);</span><br><span class="line"> x/=<span class="number">10</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> tmp == n;</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">int</span> sum = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">2</span>; i <= max_n; i++) {</span><br><span class="line"> <span class="keyword">if</span>(is_val(i))sum += i;</span><br><span class="line"> }</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d\n"</span>,sum);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">#!/usr/bin/env python</span><br><span class="line"><span class="meta"># coding=utf-8</span></span><br><span class="line"><span class="keyword">import</span> numpy as np</span><br><span class="line"><span class="keyword">import</span> matplotlib.pyplot as plt</span><br><span class="line"></span><br><span class="line"> </span><br><span class="line"></span><br><span class="line">plt.figure(<span class="number">1</span>) # 创建图表<span class="number">1</span></span><br><span class="line">x = np.linspace(<span class="number">0</span>, <span class="number">6</span>, <span class="number">300</span>) # 在<span class="number">0</span>-<span class="number">-6</span>范围之间,生成<span class="number">300</span>个点</span><br><span class="line">y1 = [<span class="number">10</span>**z <span class="keyword">for</span> z in x] # 得到 f(n) 函数的 y 值数组</span><br><span class="line">y2 = [(<span class="number">9</span>**<span class="number">5</span>)*z <span class="keyword">for</span> z in x] # 得到 g(n) 函数的 y 值数组</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">plt.figure(<span class="number">1</span>)</span><br><span class="line">plt.title('f(n) vs g(n)')</span><br><span class="line">p1 = plt.plot(x, y1, label=<span class="string">"$f(n)=10^n$"</span>);</span><br><span class="line">p2 = plt.plot(x, y2, label=<span class="string">"$g(n)=9^5 * n$"</span>);</span><br><span class="line">plt.xlabel('n digits')</span><br><span class="line">plt.ylabel('num value')</span><br><span class="line"></span><br><span class="line">plt.legend() # 显示图例</span><br><span class="line"></span><br><span class="line">plt.show() # 显示图表</span><br></pre></td></tr></table></figure><h2 id="EP34-数字阶乘(如何求上限)"><a href="#EP34-数字阶乘(如何求上限)" class="headerlink" title="EP34-数字阶乘(如何求上限)"></a>EP34-数字阶乘(如何求上限)</h2><blockquote><p>145是一个奇数,等于1!+ 4!+ 5!= 1 + 24 + 120 = 145。</p><p>查找所有数字的总和,这些数字等于其数字的阶乘之和。</p></blockquote><p>注意:为1!= 1和2!= 2不是总和,不包括在内。</p><p>求其上限: <code>n * (9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1) =10 ^(n - 1)</code></p><p>八位</p><p>n = 8,所以 8 *(9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1)= 2903040 </p><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iostream></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstdio></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cmath></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstring></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iomanip></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><algorithm></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><map></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><vector></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">f</span><span class="params">(<span class="keyword">int</span> n)</span> </span>{</span><br><span class="line"> <span class="keyword">if</span>(n == <span class="number">0</span>) <span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line"> <span class="keyword">return</span> n * f(n - <span class="number">1</span>);</span><br><span class="line">}</span><br><span class="line"><span class="comment">/*记忆化搜索</span></span><br><span class="line"><span class="comment">int keep[10] = {0};</span></span><br><span class="line"><span class="comment">int f(int n) {</span></span><br><span class="line"><span class="comment"> if(keep[n])return keep[n];</span></span><br><span class="line"><span class="comment"> if(n == 0) return 1;</span></span><br><span class="line"><span class="comment"> return keep[n] = n * f(n - 1);</span></span><br><span class="line"><span class="comment">}</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">w</span><span class="params">(<span class="keyword">int</span> n)</span> </span>{</span><br><span class="line"> <span class="keyword">int</span> x = n, temp = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">while</span>(x) {</span><br><span class="line"> temp += f(x % <span class="number">10</span>);</span><br><span class="line"> x /= <span class="number">10</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> n == temp;</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">int</span> sum = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">3</span>; i <= <span class="number">2903040</span>; i+=<span class="number">2</span>) {</span><br><span class="line"> <span class="keyword">if</span>(w(i)) {</span><br><span class="line"> sum += i;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="built_in">cout</span> << sum << <span class="built_in">endl</span>;</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h2 id="EP05-最小公倍数(欧几里得证明)"><a href="#EP05-最小公倍数(欧几里得证明)" class="headerlink" title="EP05-最小公倍数(欧几里得证明)"></a>EP05-最小公倍数(欧几里得证明)</h2><blockquote><p>2520是可以除以1到10的每个数字而没有任何余数的最小数字。</p><p>能被1到20的所有数均分的最小正数是多少?</p></blockquote><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iostream></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstdio></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cmath></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstring></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iomanip></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><algorithm></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><map></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><vector></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">gcd</span><span class="params">(<span class="keyword">int</span> a, <span class="keyword">int</span> b)</span> </span>{</span><br><span class="line"> <span class="keyword">return</span> (b ? gcd(b, a % b) : a);</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">int</span> ans = <span class="number">1</span>;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">1</span>; i <= <span class="number">20</span>; i++) {</span><br><span class="line"> <span class="keyword">if</span>(ans % i == <span class="number">0</span>) <span class="keyword">continue</span>;</span><br><span class="line"> ans *= i / gcd(ans, i);</span><br><span class="line"> }</span><br><span class="line"> <span class="built_in">cout</span> << ans << <span class="built_in">endl</span>;</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p><img src="../%E5%9B%BE%E7%89%87/%E6%AC%A7%E5%87%A0%E9%87%8C%E5%BE%97%E8%AF%81%E6%98%8E%EF%BC%91.png" alt=""></p><p><img src="../%E5%9B%BE%E7%89%87/%E6%AC%A7%E5%87%A0%E9%87%8C%E5%BE%97%E8%AF%81%E6%98%8E%EF%BC%92.png" alt=""></p><p>## EP07-素数/线性筛(线性筛推理)</p><p>素数筛:</p><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iostream></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstdio></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cmath></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstring></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iomanip></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><algorithm></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><map></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><vector></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><inttypes.h></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> max_n 200000</span></span><br><span class="line"><span class="keyword">int</span> prime[max_n + <span class="number">5</span>] = {<span class="number">0</span>};</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">init</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int64_t</span> i = <span class="number">2</span>; i * i <= max_n; i++) {</span><br><span class="line"> <span class="keyword">if</span>(prime[i])<span class="keyword">continue</span>;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int64_t</span> j = i * i; j <= max_n; j += i) {</span><br><span class="line"> prime[j] = <span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">2</span>; i <= max_n; i++) {</span><br><span class="line"> <span class="keyword">if</span>(!prime[i])prime[++prime[<span class="number">0</span>]] = i;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span>;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{</span><br><span class="line"> init();</span><br><span class="line"> <span class="built_in">cout</span> << prime[<span class="number">10001</span>]<< <span class="built_in">endl</span>;</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p>线性筛:</p><blockquote><p>筛选前30素数:</p><p>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</p><p>先从2开始往后筛,把4筛掉</p><p>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</p><p>然后从3开始往后筛,把6,9筛掉</p><p>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</p><p>然后从4开始往后筛,把8筛掉</p><p>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</p><p>然后从5开始往后筛,把10,15,25筛掉</p><p>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</p><p>……</p><p>一直晒到最后</p><p>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’</p></blockquote><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">define</span> max_n 200000</span></span><br><span class="line"><span class="keyword">int</span> prime[max_n + <span class="number">5</span>] = {<span class="number">0</span>};</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">init</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">2</span>; i <= max_n; i++) {</span><br><span class="line"> <span class="keyword">if</span>(!prime[i]) prime[++prime[<span class="number">0</span>]] = i;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> j = <span class="number">1</span>; j <= prime[<span class="number">0</span>]; j++) {</span><br><span class="line"> <span class="keyword">if</span>(prime[j] * i > max_n) <span class="keyword">break</span>;</span><br><span class="line"> prime[prime[j] * i] = <span class="number">1</span>;</span><br><span class="line"> <span class="keyword">if</span>(i % prime[j] == <span class="number">0</span>) <span class="keyword">break</span>; <span class="comment">// 9 * 3 = 27, 9 * 4就不行了</span></span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>{</span><br><span class="line"> init();</span><br><span class="line"> <span class="built_in">cout</span> << prime[<span class="number">10001</span>] << <span class="built_in">endl</span>;</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h2 id="EP08-系列中最大的产品-滑动窗口"><a href="#EP08-系列中最大的产品-滑动窗口" class="headerlink" title="EP08- 系列中最大的产品(滑动窗口)"></a>EP08- 系列中最大的产品(滑动窗口)</h2><blockquote><p>The four adjacent digits in the 1000-digit number that have the greatest product are 9 × 9 × 8 × 9 = 5832.</p><p>73167176531330624919225119674426574742355349194934<br>96983520312774506326239578318016984801869478851843<br>85861560789112949495459501737958331952853208805511<br>12540698747158523863050715693290963295227443043557<br>66896648950445244523161731856403098711121722383113<br>62229893423380308135336276614282806444486645238749<br>30358907296290491560440772390713810515859307960866<br>70172427121883998797908792274921901699720888093776<br>65727333001053367881220235421809751254540594752243<br>52584907711670556013604839586446706324415722155397<br>53697817977846174064955149290862569321978468622482<br>83972241375657056057490261407972968652414535100474<br>82166370484403199890008895243450658541227588666881<br>16427171479924442928230863465674813919123162824586<br>17866458359124566529476545682848912883142607690042<br>24219022671055626321111109370544217506941658960408<br>07198403850962455444362981230987879927244284909188<br>84580156166097919133875499200524063689912560717606<br>05886116467109405077541002256983155200055935729725<br>71636269561882670428252483600823257530420752963450</p><p>Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product?</p></blockquote><p><img src="../%E5%9B%BE%E7%89%87/%E6%BB%91%E5%8A%A8%E7%AA%97%E5%8F%A3.png" alt=""></p><p>逆运算:”-“是”+”的逆运算,但”+”不是”-“的逆运算</p><p>input_08</p><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">73167176531330624919225119674426574742355349194934</span></span><br><span class="line"><span class="number">96983520312774506326239578318016984801869478851843</span></span><br><span class="line"><span class="number">85861560789112949495459501737958331952853208805511</span></span><br><span class="line"><span class="number">12540698747158523863050715693290963295227443043557</span></span><br><span class="line"><span class="number">66896648950445244523161731856403098711121722383113</span></span><br><span class="line"><span class="number">62229893423380308135336276614282806444486645238749</span></span><br><span class="line"><span class="number">30358907296290491560440772390713810515859307960866</span></span><br><span class="line"><span class="number">70172427121883998797908792274921901699720888093776</span></span><br><span class="line"><span class="number">65727333001053367881220235421809751254540594752243</span></span><br><span class="line"><span class="number">52584907711670556013604839586446706324415722155397</span></span><br><span class="line"><span class="number">53697817977846174064955149290862569321978468622482</span></span><br><span class="line"><span class="number">83972241375657056057490261407972968652414535100474</span></span><br><span class="line"><span class="number">82166370484403199890008895243450658541227588666881</span></span><br><span class="line"><span class="number">16427171479924442928230863465674813919123162824586</span></span><br><span class="line"><span class="number">17866458359124566529476545682848912883142607690042</span></span><br><span class="line"><span class="number">24219022671055626321111109370544217506941658960408</span></span><br><span class="line"><span class="number">07198403850962455444362981230987879927244284909188</span></span><br><span class="line"><span class="number">84580156166097919133875499200524063689912560717606</span></span><br><span class="line"><span class="number">05886116467109405077541002256983155200055935729725</span></span><br><span class="line"><span class="number">71636269561882670428252483600823257530420752963450</span></span><br></pre></td></tr></table></figure><p>cpp:</p><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">define</span> max_n 1000</span></span><br><span class="line"><span class="keyword">char</span> num[max_n + <span class="number">5</span>]; </span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">int</span> len = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">while</span>(~<span class="built_in">scanf</span>(<span class="string">"%s"</span>, num + len))len += <span class="built_in">strlen</span>(num + len); </span><br><span class="line"> <span class="comment">//逐渐读入</span></span><br><span class="line"> <span class="keyword">long</span> <span class="keyword">long</span> <span class="keyword">int</span> ans = <span class="number">0</span>, p = <span class="number">1</span>, zero = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; num[i]; i++) {</span><br><span class="line"> num[i] -= <span class="string">'0'</span>;</span><br><span class="line"> <span class="keyword">if</span>(num[i]) {</span><br><span class="line"> p *= num[i];</span><br><span class="line"> } <span class="keyword">else</span> zero ++;</span><br><span class="line"> <span class="keyword">if</span>(i < <span class="number">13</span>)<span class="keyword">continue</span>;</span><br><span class="line"> <span class="keyword">if</span>(num[i - <span class="number">13</span>]) p /= num[i - <span class="number">13</span>];</span><br><span class="line"> <span class="keyword">else</span> zero -= <span class="number">1</span>;</span><br><span class="line"> <span class="keyword">if</span>(zero == <span class="number">0</span> && p > ans)ans = p;</span><br><span class="line"> }</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%lld\n"</span>,ans);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p>使用:<code>./a.out < input_8</code></p><h2 id="EP10-素数求和(线性筛)"><a href="#EP10-素数求和(线性筛)" class="headerlink" title="EP10 - 素数求和(线性筛)"></a>EP10 - 素数求和(线性筛)</h2><blockquote><p>低于10的素数之和为2 + 3 + 5 + 7 = 17。</p><p>找出200万以下的所有素数之和。</p></blockquote><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iostream></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstdio></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cmath></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstring></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iomanip></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><algorithm></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><map></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><vector></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> max_n 2000000</span></span><br><span class="line"><span class="keyword">int</span> prime[max_n];</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">long</span> <span class="keyword">long</span> <span class="keyword">int</span> sum = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">2</span>; i <= max_n; i++) {</span><br><span class="line"> <span class="keyword">if</span>(!prime[i]) {</span><br><span class="line"> prime[++prime[<span class="number">0</span>]] = i;</span><br><span class="line"> sum += i;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> j = <span class="number">1</span>; j <= prime[<span class="number">0</span>]; j++) {</span><br><span class="line"> <span class="keyword">if</span>(prime[j] * i > max_n)<span class="keyword">break</span>;</span><br><span class="line"> prime[i * prime[j]] = <span class="number">1</span>;</span><br><span class="line"> <span class="keyword">if</span>(i % prime[j] == <span class="number">0</span>)<span class="keyword">break</span>;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="built_in">cout</span> << sum << <span class="built_in">endl</span>;</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h2 id="EP11-网格中最大的产品-方向数组"><a href="#EP11-网格中最大的产品-方向数组" class="headerlink" title="EP11-网格中最大的产品(方向数组)"></a>EP11-网格中最大的产品(方向数组)</h2><blockquote><p>在下面的20×20网格中,沿对角线的四个数字用红色标记。</p><p>08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08<br>49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00<br>81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65<br>52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91<br>22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80<br>24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50<br>32 98 81 28 64 23 67 10 <font color = red>26</font> 38 40 67 59 54 70 66 18 38 64 70<br>67 26 20 68 02 62 12 20 95 <font color = red><em>63</em></font> 94 39 63 08 40 91 66 49 94 21<br>24 55 58 05 66 73 99 26 97 17 <font color = red><em>78</em></font> 78 96 83 14 88 34 89 63 72<br>21 36 23 09 75 00 76 44 20 45 35 <font color = red><em>14</em></font> 00 61 33 97 34 31 33 95<br>78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92<br>16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57<br>86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58<br>19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40<br>04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66<br>88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69<br>04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36<br>20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16<br>20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54<br>01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48</p><p>这些数字的乘积为26×63×78×14 = 1788696。</p><p>在20×20网格中,相同方向(上,下,左,右或对角线)上四个相邻数字的最大乘积是多少?</p></blockquote><p>只要四个方向就能遍历完:</p><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iostream></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> max_n 20</span></span><br><span class="line"><span class="keyword">int</span> grid[max_n + <span class="number">5</span>][max_n + <span class="number">5</span>];</span><br><span class="line"><span class="keyword">int</span> dir[<span class="number">4</span>][<span class="number">2</span>] = {</span><br><span class="line"> {<span class="number">-1</span>, <span class="number">-1</span>},{ <span class="number">-1</span>, <span class="number">0</span>},{ <span class="number">-1</span>, <span class="number">1</span>},{ <span class="number">0</span>, <span class="number">1</span>} <span class="comment">//只要四个方向就能遍历完</span></span><br><span class="line">};</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">calc</span><span class="params">(<span class="keyword">int</span> x, <span class="keyword">int</span> y)</span> </span>{</span><br><span class="line"> <span class="keyword">int</span> ans = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> k = <span class="number">0</span>; k < <span class="number">4</span>; k ++) {</span><br><span class="line"> <span class="keyword">int</span> p = <span class="number">1</span>;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> step = <span class="number">0</span>; step < <span class="number">4</span>; step ++) {</span><br><span class="line"> <span class="keyword">int</span> dx = x + dir[k][<span class="number">0</span>] * step;</span><br><span class="line"> <span class="keyword">int</span> dy = y + dir[k][<span class="number">1</span>] * step;</span><br><span class="line"> <span class="keyword">if</span>(dx < <span class="number">0</span> || dx >= max_n)<span class="keyword">break</span>;</span><br><span class="line"> <span class="keyword">if</span>(dy < <span class="number">0</span> || dy >= max_n)<span class="keyword">break</span>;</span><br><span class="line"> p *= grid[dx][dy];</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span>(p > ans)ans = p;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> ans;</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i < max_n; i++) {</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> j = <span class="number">0</span>; j < max_n; j++) {</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d"</span>, &grid[i][j]);</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">int</span> ans = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>;i < max_n; i++) {</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> j = <span class="number">0</span>; j < max_n; j++) {</span><br><span class="line"> <span class="keyword">int</span> p = calc(i, j);</span><br><span class="line"> <span class="keyword">if</span>(p > ans) ans = p;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d\n"</span>, ans);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h2 id="EP14-最长的Collatz序列-记忆化"><a href="#EP14-最长的Collatz序列-记忆化" class="headerlink" title="EP14-最长的Collatz序列(记忆化)"></a>EP14-最长的Collatz序列(记忆化)</h2><blockquote><p>为正整数的集合定义了以下迭代序列:</p><p>n → n / 2(n是偶数)<br>n →3 n + 1(n是奇数)</p><p>使用上面的规则并从13开始,我们生成以下序列:</p><p>13→40→20→10→5→16→8→4→2→1</p><p>可以看出,该序列(从13开始到1结束)包含10个项。尽管尚未证明(Collatz问题),但可以认为所有起始数字都以1结尾。</p><p>最长的链条, 小于100万的哪个起始数字?</p><p><strong>注意:</strong>链条启动后,期限就可以超过一百万。</p></blockquote><p>朴素做法</p><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iostream></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstdio></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cmath></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstring></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iomanip></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><algorithm></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><map></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><vector></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> max_n 1000000</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">typedef</span> <span class="keyword">long</span> <span class="keyword">long</span> ll;</span><br><span class="line"></span><br><span class="line"><span class="function">ll <span class="title">get_len</span><span class="params">(ll n)</span> </span>{</span><br><span class="line"> <span class="keyword">if</span>(n == <span class="number">1</span>)<span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line"> <span class="comment">//if(n < 0)printf("ERROR\n");</span></span><br><span class="line"> <span class="keyword">if</span>(n % <span class="number">2</span> == <span class="number">0</span>) <span class="keyword">return</span> get_len(n / <span class="number">2</span>) + <span class="number">1</span>;</span><br><span class="line"> <span class="keyword">return</span> get_len(<span class="number">3</span> * n + <span class="number">1</span>) + <span class="number">1</span>;</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{</span><br><span class="line"> ll ans = <span class="number">0</span>, len = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">1</span>; i < max_n; i++) {</span><br><span class="line"> ll tmp = get_len(i);</span><br><span class="line"> <span class="keyword">if</span>(tmp > len) {</span><br><span class="line"> ans = i;</span><br><span class="line"> len = tmp;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"num = %lld, len = %lld\n"</span>, ans, len);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p>记忆化:</p><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iostream></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstdio></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cmath></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstring></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iomanip></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><algorithm></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><map></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><vector></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> max_n 1000000</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> size 1000000</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">typedef</span> <span class="keyword">long</span> <span class="keyword">long</span> ll;</span><br><span class="line">ll keep[max_n + <span class="number">5</span>] = {<span class="number">0</span>};</span><br><span class="line"></span><br><span class="line"><span class="function">ll <span class="title">get_len</span><span class="params">(ll n)</span> </span>{</span><br><span class="line"> <span class="keyword">if</span>(n == <span class="number">1</span>)<span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line"> <span class="comment">//if(n < 0)printf("ERROR\n");</span></span><br><span class="line"> <span class="keyword">if</span>(n <= max_n && keep[n]) <span class="keyword">return</span> keep[n];</span><br><span class="line"> ll ret = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">if</span>(n % <span class="number">2</span> == <span class="number">0</span>) ret = get_len(n / <span class="number">2</span>) + <span class="number">1</span>;</span><br><span class="line"> <span class="keyword">else</span> ret = get_len(<span class="number">3</span> * n + <span class="number">1</span>) + <span class="number">1</span>;</span><br><span class="line"> <span class="keyword">if</span>(n <= max_n)keep[n] = ret;</span><br><span class="line"> <span class="keyword">return</span> ret;</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{</span><br><span class="line"> ll ans = <span class="number">0</span>, len = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">1</span>; i < max_n; i++) {</span><br><span class="line"> ll tmp = get_len(i);</span><br><span class="line"> <span class="keyword">if</span>(tmp > len) {</span><br><span class="line"> ans = i;</span><br><span class="line"> len = tmp;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"num = %lld, len = %lld\n"</span>, ans, len);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h2 id="EP17-数字字母计数"><a href="#EP17-数字字母计数" class="headerlink" title="EP17-数字字母计数"></a>EP17-数字字母计数</h2><p>![](../图片/2019-12-21 15-35-02屏幕截图.png)</p><p>![](../图片/2019-12-21 15-32-33屏幕截图.png)</p><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iostream></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstdio></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cmath></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstring></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iomanip></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><algorithm></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><map></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><vector></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">get_letters</span><span class="params">(<span class="keyword">int</span> n)</span> </span>{</span><br><span class="line"> <span class="keyword">static</span> <span class="keyword">int</span> arr1[<span class="number">20</span>] = {</span><br><span class="line"> <span class="number">0</span>, <span class="number">3</span>, <span class="number">3</span>, <span class="number">5</span>, <span class="number">4</span>, <span class="number">4</span>, <span class="number">3</span>, <span class="number">5</span>, <span class="number">5</span>, <span class="number">4</span>, <span class="number">3</span>,</span><br><span class="line"> <span class="number">6</span>, <span class="number">6</span>, <span class="number">8</span>, <span class="number">8</span>, <span class="number">7</span>, <span class="number">7</span>, <span class="number">9</span>, <span class="number">8</span>, <span class="number">8</span> </span><br><span class="line"> };</span><br><span class="line"> <span class="keyword">static</span> <span class="keyword">int</span> arr2[<span class="number">10</span>] = {</span><br><span class="line"> <span class="number">0</span>, <span class="number">0</span>, <span class="number">6</span>, <span class="number">6</span>, <span class="number">5</span>, <span class="number">5</span>, <span class="number">5</span>, <span class="number">7</span>, <span class="number">6</span>, <span class="number">6</span></span><br><span class="line"> };</span><br><span class="line"> <span class="keyword">if</span>(n < <span class="number">20</span>) {</span><br><span class="line"> <span class="keyword">return</span> arr1[n];</span><br><span class="line"> } <span class="keyword">else</span> <span class="keyword">if</span>(n < <span class="number">100</span>){</span><br><span class="line"> <span class="keyword">return</span> arr2[n / <span class="number">10</span>] + arr1[n % <span class="number">10</span>];</span><br><span class="line"> } <span class="keyword">else</span> <span class="keyword">if</span>(n < <span class="number">1000</span>) {</span><br><span class="line"> <span class="keyword">if</span>(n % <span class="number">100</span> == <span class="number">0</span>) <span class="keyword">return</span> arr1[n / <span class="number">100</span>] + <span class="number">7</span>;</span><br><span class="line"> <span class="keyword">return</span> arr1[n / <span class="number">100</span>] + <span class="number">10</span> + get_letters(n % <span class="number">100</span>);</span><br><span class="line"> } <span class="keyword">else</span> {</span><br><span class="line"> <span class="keyword">return</span> <span class="number">11</span>;</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">int</span> sum = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">1</span>; i <= <span class="number">1000</span>; i++) {</span><br><span class="line"> sum += get_letters(i);</span><br><span class="line"> }</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d\n"</span>, sum);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h2 id="EP13-大整数加法"><a href="#EP13-大整数加法" class="headerlink" title="EP13-大整数加法"></a>EP13-大整数加法</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iostream></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstdio></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cmath></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstring></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iomanip></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><algorithm></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><map></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><vector></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><string.h></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> max_n 52</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">char</span> str[max_n + <span class="number">5</span>] = {<span class="number">0</span>};</span><br><span class="line"><span class="keyword">int</span> ans[max_n + <span class="number">5</span>] = {<span class="number">0</span>};</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{</span><br><span class="line"></span><br><span class="line"> <span class="keyword">while</span>(~<span class="built_in">scanf</span>(<span class="string">"%s"</span>, str)) {</span><br><span class="line"> <span class="keyword">int</span> len = <span class="built_in">strlen</span>(str);</span><br><span class="line"> <span class="keyword">if</span>(len > ans[<span class="number">0</span>]) ans[<span class="number">0</span>] = len;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i < len; i++) {</span><br><span class="line"> ans[len - i] += str[i] - <span class="string">'0'</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">1</span>; i <= ans[<span class="number">0</span>]; i++) {</span><br><span class="line"> <span class="keyword">if</span>(ans[i] < <span class="number">10</span>)<span class="keyword">continue</span>;</span><br><span class="line"> ans[i + <span class="number">1</span>] += ans[i] / <span class="number">10</span>;</span><br><span class="line"> ans[i] %= <span class="number">10</span>;</span><br><span class="line"> ans[<span class="number">0</span>] += (i == ans[<span class="number">0</span>]);</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = ans[<span class="number">0</span>]; i > ans[<span class="number">0</span>] - <span class="number">10</span>; i--) {</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d"</span>, ans[i]);</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h3 id="a-b"><a href="#a-b" class="headerlink" title="a^ b"></a>a^ b</h3><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iostream></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstdio></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cmath></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstring></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iomanip></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><algorithm></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><map></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><vector></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> max_n 500</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">solve</span> <span class="params">(<span class="keyword">int</span> a, <span class="keyword">int</span> b)</span></span>{</span><br><span class="line"> <span class="keyword">int</span> ans[max_n] = {<span class="number">0</span>};</span><br><span class="line"> ans[<span class="number">0</span>] = <span class="number">1</span>, ans[<span class="number">1</span>] = <span class="number">1</span>;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i < b; i++) {</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> j = <span class="number">1</span>; j <= ans[<span class="number">0</span>]; j++) {</span><br><span class="line"> ans[j] *= a;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> k = <span class="number">1</span>; k <= ans[<span class="number">0</span>]; k++) {</span><br><span class="line"> <span class="keyword">if</span>(ans[k] < <span class="number">10</span>)<span class="keyword">continue</span>;</span><br><span class="line"> ans[k + <span class="number">1</span>] += ans[k] / <span class="number">10</span>;</span><br><span class="line"> ans[k] %= <span class="number">10</span>;</span><br><span class="line"> ans[<span class="number">0</span>] += (ans[<span class="number">0</span>] == k);</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = ans[<span class="number">0</span>]; i > <span class="number">0</span>; i--) {</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d"</span>, ans[i]);</span><br><span class="line"> }</span><br><span class="line"> <span class="built_in">cout</span> << <span class="built_in">endl</span>;</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">int</span> a, b;</span><br><span class="line"> <span class="built_in">cin</span> >> a >> b;</span><br><span class="line"> solve(a, b);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h3 id="高精度乘法"><a href="#高精度乘法" class="headerlink" title="高精度乘法"></a>高精度乘法</h3><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iostream></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstdio></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cmath></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstring></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iomanip></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><algorithm></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><map></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><vector></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> max_n 100</span></span><br><span class="line"><span class="keyword">char</span> str_a[max_n + <span class="number">5</span>], str_b[max_n + <span class="number">5</span>];</span><br><span class="line"><span class="keyword">int</span> a[max_n + <span class="number">5</span>], b[max_n + <span class="number">5</span>], c[<span class="number">2</span> * max_n + <span class="number">5</span>];</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="built_in">memset</span>(c, <span class="number">0</span>, <span class="keyword">sizeof</span>(c));</span><br><span class="line"> <span class="built_in">cin</span> >> str_a >> str_b;</span><br><span class="line"> <span class="keyword">int</span> len_a = <span class="built_in">strlen</span>(str_a);</span><br><span class="line"> <span class="keyword">int</span> len_b = <span class="built_in">strlen</span>(str_b);</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i < len_a; i++)a[i] = str_a[len_a - i - <span class="number">1</span>] - <span class="string">'0'</span>;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i < len_b; i++)b[i] = str_b[len_b - i - <span class="number">1</span>] - <span class="string">'0'</span>;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i < len_a; i ++) {</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> j = <span class="number">0</span>; j < len_b; j++) {</span><br><span class="line"> c[i + j] += a[i] * b[j];</span><br><span class="line"> <span class="keyword">if</span>(c[i + j] < <span class="number">10</span>)<span class="keyword">continue</span>;</span><br><span class="line"> c[i + j + <span class="number">1</span>] += c[i + j] / <span class="number">10</span>;</span><br><span class="line"> c[i + j] %= <span class="number">10</span>;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">int</span> len_c = len_a + len_b;</span><br><span class="line"> <span class="keyword">while</span>(!c[len_c])len_c--;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = len_c; i >= <span class="number">0</span>; i--) {</span><br><span class="line"> <span class="built_in">cout</span> << c[i];</span><br><span class="line"> }</span><br><span class="line"> <span class="built_in">cout</span> << <span class="built_in">endl</span>;</span><br><span class="line"> </span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h3 id="高精度阶乘"><a href="#高精度阶乘" class="headerlink" title="高精度阶乘"></a>高精度阶乘</h3><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">define</span> max_n 1500</span></span><br><span class="line"><span class="keyword">int</span> ans[max_n + <span class="number">5</span>];</span><br><span class="line"><span class="keyword">int</span> res[max_n + <span class="number">5</span>];</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">solve</span><span class="params">(<span class="keyword">int</span> x)</span> </span>{</span><br><span class="line"> <span class="built_in">memset</span>(ans, <span class="number">0</span>, <span class="keyword">sizeof</span>(ans));</span><br><span class="line"> ans[<span class="number">0</span>] = <span class="number">1</span>, ans[<span class="number">1</span>] = <span class="number">1</span>;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">1</span>; i <= x; i++) {</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> j = <span class="number">1</span>; j <= ans[<span class="number">0</span>]; j++) {</span><br><span class="line"> ans[j] *= i;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> k = <span class="number">1</span>; k <= ans[<span class="number">0</span>]; k++) {</span><br><span class="line"> <span class="keyword">if</span>(ans[k] < <span class="number">10</span>)<span class="keyword">continue</span>;</span><br><span class="line"> ans[k + <span class="number">1</span>] += ans[k] / <span class="number">10</span>;</span><br><span class="line"> ans[k] %= <span class="number">10</span>;</span><br><span class="line"> ans[<span class="number">0</span>] += (ans[<span class="number">0</span>] == k);</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="built_in">memset</span>(res, <span class="number">0</span>, <span class="keyword">sizeof</span>(res));</span><br><span class="line"> res[<span class="number">0</span>] = <span class="number">1</span>, res[<span class="number">1</span>] = <span class="number">1</span>;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">1</span>; i <= ans[<span class="number">0</span>]; i++) {</span><br><span class="line"> <span class="keyword">if</span>(!ans[i])<span class="keyword">continue</span>;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> j = <span class="number">1</span>; j <= res[<span class="number">0</span>]; j++) {</span><br><span class="line"> res[j] *= ans[i];</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> k = <span class="number">1</span>; k <= res[<span class="number">0</span>]; k++) {</span><br><span class="line"> <span class="keyword">if</span>(res[k] < <span class="number">10</span>)<span class="keyword">continue</span>;</span><br><span class="line"> res[k + <span class="number">1</span>] += res[k] / <span class="number">10</span>;</span><br><span class="line"> res[k] %= <span class="number">10</span>;</span><br><span class="line"> res[<span class="number">0</span>] += (res[<span class="number">0</span>] == k);</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = res[<span class="number">0</span>]; i > <span class="number">0</span>; i--) {</span><br><span class="line"> <span class="built_in">cout</span> << res[i];</span><br><span class="line"> }</span><br><span class="line"> <span class="built_in">cout</span> << <span class="built_in">endl</span>;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">int</span> n;</span><br><span class="line"> <span class="keyword">while</span>(<span class="built_in">scanf</span>(<span class="string">"%d"</span>, &n)!=EOF) {</span><br><span class="line"> solve(n);</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h2 id="EP25-1000位斐波纳契数-高精度&滚动数组"><a href="#EP25-1000位斐波纳契数-高精度&滚动数组" class="headerlink" title="EP25-1000位斐波纳契数(高精度&滚动数组)"></a>EP25-1000位斐波纳契数(高精度&滚动数组)</h2><p>斐波那契数列由递归关系定义:</p><blockquote><p>F <em>n</em> = F <em>n</em> -1 + F <em>n</em> -2,其中F 1 = 1和F 2 = 1。</p></blockquote><p>因此,前12个术语将是:</p><blockquote><p>F 1 = 1<br>F 2 = 1<br>F 3 = 2<br>F 4 = 3<br>F 5 = 5<br>F 6 = 8<br>F 7 = 13<br>F 8 = 21<br>F 9 = 34<br>F 10 = 55<br>F 11 = 89<br>F 12 = 144</p></blockquote><p>第12个项F 12是包含三个数字的第一个项。</p><p>斐波纳契数列中包含1000个数字的第一项的索引是什么?</p><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><stdio.h></span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">int</span> f[<span class="number">3</span>][<span class="number">1005</span>]; <span class="comment">//开1500防止底下数炸了b[0] 有可能大于</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">int</span> n = <span class="number">2</span>;</span><br><span class="line"> f[<span class="number">1</span>][<span class="number">0</span>] = <span class="number">1</span>; <span class="comment">// [0]存的是几位的数</span></span><br><span class="line"> f[<span class="number">1</span>][<span class="number">1</span>] = <span class="number">1</span>; </span><br><span class="line"> f[<span class="number">2</span>][<span class="number">0</span>] = <span class="number">1</span>;</span><br><span class="line"> f[<span class="number">2</span>][<span class="number">1</span>] = <span class="number">1</span>;</span><br><span class="line"> <span class="keyword">while</span> (f[n%<span class="number">3</span>][<span class="number">0</span>] < <span class="number">1000</span>) { </span><br><span class="line"> n += <span class="number">1</span>; </span><br><span class="line"> <span class="keyword">int</span> *a = f[n % <span class="number">3</span>], *b = f[(n<span class="number">-1</span>)%<span class="number">3</span>], *c = f[(n- <span class="number">2</span>) % <span class="number">3</span>]; <span class="comment">//存的每一位上数的大小</span></span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">1</span>; i <= b[<span class="number">0</span>]; i++) {</span><br><span class="line"> a[i] = b[i] + c[i]; <span class="comment">// 对应的位相加得到的数存在a[]中</span></span><br><span class="line"> }</span><br><span class="line"> a[<span class="number">0</span>] = b[<span class="number">0</span>];</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">1</span>; i <= a[<span class="number">0</span>]; i++) { <span class="comment">// 判断a[]的进位, 进位后,进行一轮判断a[0]是否等于1000</span></span><br><span class="line"> <span class="keyword">if</span>(a[i] < <span class="number">10</span>)<span class="keyword">continue</span>;</span><br><span class="line"> a[i + <span class="number">1</span>] += a[i] / <span class="number">10</span>;</span><br><span class="line"> a[i] %= <span class="number">10</span>;</span><br><span class="line"> a[<span class="number">0</span>] += (i == a[<span class="number">0</span>]);</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d\n"</span>, n);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">f[0], f[1], f[2]</span></span><br><span class="line"><span class="comment"> a c b</span></span><br><span class="line"><span class="comment"> 2 1 1</span></span><br><span class="line"><span class="comment"> c a b</span></span><br><span class="line"><span class="comment"> 3 2 1</span></span><br><span class="line"><span class="comment"> c b a</span></span><br><span class="line"><span class="comment"> 2 1 3</span></span><br><span class="line"><span class="comment">*/</span></span><br></pre></td></tr></table></figure><h2 id="EP31-硬币总和-递推"><a href="#EP31-硬币总和-递推" class="headerlink" title="EP31-硬币总和(递推)"></a>EP31-硬币总和(递推)</h2><p>在英国,货币由英镑(£)和便士(p)组成。一般发行的硬币有八种:</p><blockquote><p>1p,2p,5p,10p,20p,50p,£1(100p)和£2(200p)。</p></blockquote><p>可以通过以下方式赚取£2:</p><blockquote><p>1×£1 + 1×50p + 2×20p + 1×5p + 1×2p + 3×1p</p></blockquote><p>可以使用任意数量的硬币来制作2英镑几种不同的方式?</p><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iostream></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstdio></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cmath></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstring></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iomanip></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><algorithm></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><map></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><vector></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> max_n 8</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> max_m 200</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">int</span> f[max_n + <span class="number">5</span>][max_m + <span class="number">5</span>];</span><br><span class="line"><span class="keyword">int</span> w[max_n + <span class="number">5</span>] = {</span><br><span class="line"> <span class="number">1</span>, <span class="number">2</span>, <span class="number">5</span>, <span class="number">10</span>, <span class="number">20</span>, <span class="number">50</span>, <span class="number">100</span>, <span class="number">200</span></span><br><span class="line">};</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i < max_n; i++) { <span class="comment">// 前i中</span></span><br><span class="line"> f[i][<span class="number">0</span>] = <span class="number">1</span>; <span class="comment">// 初始化前i种组成0元所需要的种数初始化为1,以便后面链接使用</span></span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> j = <span class="number">1</span>; j <= max_m; j++) { <span class="comment">// 控制j块钱</span></span><br><span class="line"> f[i][j] = <span class="number">0</span>; <span class="comment">// 初始化前 i 种组成 j 块钱种数为0 </span></span><br><span class="line"> <span class="keyword">if</span>(i >= <span class="number">1</span>)f[i][j] += f[i - <span class="number">1</span>][j]; <span class="comment">//前i - 1种硬币组成j块钱所种数 </span></span><br><span class="line"> <span class="keyword">if</span>(j >= w[i]) f[i][j] += f[i][j - w[i]]; <span class="comment">// 前i- 1种硬币加上前 i 种组成 j - w[i]块钱所得的种数</span></span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">// 滚动数组法</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> max_n 8</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> max_m 200</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">int</span> f[<span class="number">2</span>][max_m + <span class="number">5</span>];</span><br><span class="line"><span class="keyword">int</span> w[max_n + <span class="number">5</span>] = {</span><br><span class="line"> <span class="number">1</span>, <span class="number">2</span>, <span class="number">5</span>, <span class="number">10</span>, <span class="number">20</span>, <span class="number">50</span>, <span class="number">100</span>, <span class="number">200</span></span><br><span class="line">};</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> k = <span class="number">0</span>; k < max_n; k++) {</span><br><span class="line"> <span class="keyword">int</span> i = k % <span class="number">2</span>; <span class="comment">//表示所存的哪一位 </span></span><br><span class="line"> f[i][<span class="number">0</span>] = <span class="number">1</span>; <span class="comment">// 初始化 i 种所要组成的钱数0,所为1</span></span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> j = <span class="number">1</span>; j <= max_m; j++) { <span class="comment">// j为所要组成的钱数</span></span><br><span class="line"> f[i][j] = <span class="number">0</span>; <span class="comment">// 初始化 前i种所组成的j的个数为0</span></span><br><span class="line"> f[i][j] += f[i ^ <span class="number">1</span>][j]; <span class="comment">// 前i - 1项所组成j元钱所需要的种数</span></span><br><span class="line"> <span class="keyword">if</span>(j >= w[k]) f[i][j] += f[i][j - w[k]]; <span class="comment">// 前 i - 1项所加上前i项所组成的总种类</span></span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d\n"</span>, f[(max_n - <span class="number">1</span>) % <span class="number">2</span>][max_m]);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">// 二维降一维:前面所有的i可以省略掉</span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{</span><br><span class="line"> f[<span class="number">0</span>] = <span class="number">1</span>; <span class="comment">// 组成0元时所种类</span></span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i < max_n; i++) { <span class="comment">//遍历一遍前i项</span></span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> j = <span class="number">1</span>; j <= max_n; j++) { <span class="comment">// 遍历一遍没组成的每一元钱</span></span><br><span class="line"> <span class="keyword">if</span>(j >= w[i])f[j] +=f[j - w[i]];</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d\n"</span>, f[max_n]);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">int main() {</span></span><br><span class="line"><span class="comment"> for(int i = 0; i < max_n; i++) {</span></span><br><span class="line"><span class="comment"> f[i][0] = 1;</span></span><br><span class="line"><span class="comment"> for(int j = 1; j <= max_m; j++) {</span></span><br><span class="line"><span class="comment"> f[i][j] = 0;</span></span><br><span class="line"><span class="comment"> if(i >= 1) f[i][j] = f[i][j] + f[i - 1][j];</span></span><br><span class="line"><span class="comment"> if(j >= w[i])f[i][j] = f[i - 1][j] + f[i][j - w[i]];</span></span><br><span class="line"><span class="comment"> }</span></span><br><span class="line"><span class="comment"> }</span></span><br><span class="line"><span class="comment">}</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">int main() {</span></span><br><span class="line"><span class="comment"> f[0] = 1;</span></span><br><span class="line"><span class="comment"> for(int i = 0; i < max_n; i++) {</span></span><br><span class="line"><span class="comment"> for(int j = 1; j <= max_n; j++) {</span></span><br><span class="line"><span class="comment"> if(j >= w[i])f[j] += f[j - w[i]];</span></span><br><span class="line"><span class="comment"> }</span></span><br><span class="line"><span class="comment"> }</span></span><br><span class="line"><span class="comment"> return 0;</span></span><br><span class="line"><span class="comment">}*/</span></span><br></pre></td></tr></table></figure><h2 id="EP32-Pandigital产品"><a href="#EP32-Pandigital产品" class="headerlink" title="EP32-Pandigital产品"></a>EP32-Pandigital产品</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">我们可以说,如果n位数字恰好一次使用了<span class="number">1</span>到n的所有数字,那么它就是pandigital 。例如,<span class="number">5</span>位数字<span class="number">15234</span>是<span class="number">1</span>到<span class="number">5</span>个泛数字。</span><br><span class="line"></span><br><span class="line">乘积<span class="number">7254</span>是不寻常的,因为其标识为<span class="number">39</span>×<span class="number">186</span> = <span class="number">7254</span>,包含被乘数,乘数,乘积是<span class="number">1</span>到<span class="number">9</span>泛数。</span><br><span class="line"></span><br><span class="line">找出所有被乘数/乘数/产品标识可以写成<span class="number">1</span>到<span class="number">9</span>泛数字的所有产品的总和。</span><br><span class="line"></span><br><span class="line">提示:某些产品可以通过多种方式获得,因此请确保只在其总和中包含一次。</span><br></pre></td></tr></table></figure><p>![](../图片/2019-12-22 17-42-53屏幕截图.png)</p><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iostream></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstdio></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cmath></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstring></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iomanip></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><algorithm></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><map></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><vector></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> max_n 10000</span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">add_to_num</span><span class="params">(<span class="keyword">int</span> *num, <span class="keyword">int</span> n)</span> </span>{ <span class="comment">// 表示在num数组中对num[0~9]做标记,如果有重的,就返回0</span></span><br><span class="line"> <span class="keyword">while</span>(n) {</span><br><span class="line"> <span class="keyword">if</span>(num[n % <span class="number">10</span>])<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line"> num[n%<span class="number">10</span>] += <span class="number">1</span>;</span><br><span class="line"> n/=<span class="number">10</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">dights</span><span class="params">(<span class="keyword">int</span> a)</span> </span>{</span><br><span class="line"> <span class="keyword">if</span>(a == <span class="number">0</span>)<span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line"> <span class="keyword">else</span> {</span><br><span class="line"> <span class="keyword">return</span> <span class="built_in">floor</span>(<span class="built_in">log10</span>(a)) + <span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">val</span><span class="params">(<span class="keyword">int</span> a, <span class="keyword">int</span> b)</span> </span>{</span><br><span class="line"> <span class="keyword">if</span>(dights(a) + dights(b) + dights(a * b) - <span class="number">9</span>)<span class="keyword">return</span> <span class="number">0</span>; <span class="comment">// a, b, c 三个数的位数和为9</span></span><br><span class="line"> <span class="keyword">int</span> num[<span class="number">10</span>] = {<span class="number">0</span>}; </span><br><span class="line"> <span class="keyword">int</span> flag = <span class="number">1</span>;</span><br><span class="line"> num[<span class="number">0</span>] = <span class="number">1</span>;</span><br><span class="line"> flag = flag && add_to_num(num, a); <span class="comment">// 如果有一个为0,那么最后返回0</span></span><br><span class="line"> flag = flag && add_to_num(num, b);</span><br><span class="line"> flag = flag && add_to_num(num, a * b);</span><br><span class="line"> <span class="keyword">return</span> flag;</span><br><span class="line">}</span><br><span class="line"><span class="keyword">int</span> keep[max_n + <span class="number">5</span>] = {<span class="number">0</span>};</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">int</span> sum = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> a = <span class="number">1</span>; a < <span class="number">100</span>; a ++ ) { <span class="comment">// a 1... 100 </span></span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> b = a + <span class="number">1</span>; b < <span class="number">10000</span>; b ++) { <span class="comment">// b > a</span></span><br><span class="line"> <span class="keyword">if</span>(!val(a , b))<span class="keyword">continue</span>; <span class="comment">// 如果a,b满足条件</span></span><br><span class="line"> <span class="keyword">if</span>(keep[a * b])<span class="keyword">continue</span>; <span class="comment">// 标记只有一次</span></span><br><span class="line"> sum += a * b;</span><br><span class="line"> keep[a * b] = <span class="number">1</span>;</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d * %d = %d\n"</span>, a , b, a * b);</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d\n"</span>, sum);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h2 id="EP38-全数字的倍数"><a href="#EP38-全数字的倍数" class="headerlink" title="EP38-全数字的倍数"></a>EP38-全数字的倍数</h2><p>将192分别与1、2、3相乘:</p><blockquote><p>192 × 1 = 192<br>192 × 2 = 384<br>192 × 3 = 576</p></blockquote><p>连接这些乘积,我们得到一个1至9全数字的数192384576。我们称192384576为192和(1,2,3)的连接乘积。</p><p>同样地,将9分别与1、2、3、4、5相乘,得到1至9全数字的数918273645,即是9和(1,2,3,4,5)的连接乘积。</p><p>对于n > 1,所有某个整数和(1,2, … ,n)的连接乘积所构成的数中,最大的1至9全数字的数是多少?</p><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><stdio.h></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><math.h></span></span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">dights</span><span class="params">(<span class="keyword">long</span> <span class="keyword">long</span> <span class="keyword">int</span> n)</span> </span>{</span><br><span class="line"> <span class="keyword">if</span>(n == <span class="number">0</span>)<span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line"> <span class="keyword">else</span> <span class="keyword">return</span> <span class="built_in">floor</span>(<span class="built_in">log10</span>(n)) + <span class="number">1</span>;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">calc</span><span class="params">(<span class="keyword">int</span> x)</span> </span>{</span><br><span class="line"> <span class="keyword">long</span> <span class="keyword">long</span> n = <span class="number">1</span>, ans = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">while</span>(dights(ans) < <span class="number">9</span>) { <span class="comment">// 不足9位继续</span></span><br><span class="line"> ans *= <span class="built_in">pow</span>(<span class="number">10</span>, dights(x * n)); <span class="comment">// 几位</span></span><br><span class="line"> ans += n * x; <span class="comment">// 每一次乘上n</span></span><br><span class="line"> n += <span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span>(dights(ans) - <span class="number">9</span>) <span class="keyword">return</span> <span class="number">0</span>; <span class="comment">// 判断是否为9位数</span></span><br><span class="line"> <span class="keyword">int</span> num[<span class="number">10</span>] = {<span class="number">1</span>, <span class="number">0</span>}; <span class="comment">// 因为是1...9,不包含0,去掉0</span></span><br><span class="line"> <span class="keyword">int</span> tmp = ans;</span><br><span class="line"> <span class="keyword">while</span>(tmp) { <span class="comment">// 判断ans是否包含所有的1....9</span></span><br><span class="line"> <span class="keyword">if</span>(num[tmp % <span class="number">10</span>])<span class="keyword">return</span> <span class="number">0</span>; </span><br><span class="line"> num[tmp % <span class="number">10</span>] += <span class="number">1</span>;</span><br><span class="line"> tmp /= <span class="number">10</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> ans;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">long</span> <span class="keyword">long</span> ans = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">1</span>; i < <span class="number">10000</span>; i++) {</span><br><span class="line"> <span class="keyword">int</span> tmp = calc(i);</span><br><span class="line"> <span class="keyword">if</span>(tmp > ans) ans = tmp;</span><br><span class="line"> }</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%lld\n"</span>, ans);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h2 id="EP44-五边形数"><a href="#EP44-五边形数" class="headerlink" title="EP44-五边形数"></a>EP44-五边形数</h2><p>五边形数由公式Pn=n(3n−1)/2生成。前十个五边形数是:</p><p>1, 5, 12, 22, 35, 51, 70, 92, 117, 145, …</p><p>可以看出P4 + P7 = 22 + 70 = 92 = P8。然而,它们的差70 − 22 = 48并不是五边形数。</p><p>在所有和差均为五边形数的五边形数对Pj和Pk中,找出使D = |Pk − Pj|最小的一对;此时D的值是多少?</p><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><stdio.h></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><inttypes.h></span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">typedef</span> <span class="keyword">long</span> <span class="keyword">long</span> int1;</span><br><span class="line"><span class="function">int1 <span class="title">Pentagonal</span><span class="params">(int1 n)</span> </span>{</span><br><span class="line"> <span class="keyword">return</span> n * (<span class="number">3</span> * n - <span class="number">1</span>) / <span class="number">2</span>;</span><br><span class="line">}</span><br><span class="line"><span class="function">int1 <span class="title">binary_search</span><span class="params">(int1 (*func)(int1), int1 n, int1 x)</span> </span>{</span><br><span class="line"> int1 head = <span class="number">1</span>, tail = n, mid;</span><br><span class="line"> <span class="keyword">while</span>(head <= tail) {</span><br><span class="line"> mid = (head + tail) >> <span class="number">1</span>;</span><br><span class="line"> <span class="keyword">if</span>(func(mid) == x) <span class="keyword">return</span> mid;</span><br><span class="line"> <span class="keyword">if</span>(func(mid) < x) head = mid + <span class="number">1</span>;</span><br><span class="line"> <span class="keyword">else</span> tail = mid - <span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{</span><br><span class="line"> int1 n = <span class="number">2</span>, D = INT32_MAX, pk, pj; </span><br><span class="line"> <span class="keyword">while</span>(Pentagonal(n) - Pentagonal(n - <span class="number">1</span>) < D) {</span><br><span class="line"> pk = Pentagonal(n);</span><br><span class="line"> <span class="keyword">for</span>(int1 j = n - <span class="number">1</span>; j >= <span class="number">1</span>; j--) { <span class="comment">// 枚举n - 1之前</span></span><br><span class="line"> pj = Pentagonal(j); <span class="comment">// 取出</span></span><br><span class="line"> <span class="keyword">if</span>(pk - pj >= D) <span class="keyword">break</span>; <span class="comment">// 如果差值 >= D跳出</span></span><br><span class="line"> int1 ind1 = binary_search(Pentagonal, <span class="number">2</span> * n, pk + pj); <span class="comment">// 进行二分</span></span><br><span class="line"> int1 ind2 = binary_search(Pentagonal, n, pk - pj); <span class="comment">// 进行二分</span></span><br><span class="line"> <span class="keyword">if</span>(ind2 && ind1) {</span><br><span class="line"> D = pk - pj;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> n ++;</span><br><span class="line"> }</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%lld\n"</span>, D);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h2 id="EP09-特殊毕达哥拉斯三元组-素勾股数"><a href="#EP09-特殊毕达哥拉斯三元组-素勾股数" class="headerlink" title="EP09-特殊毕达哥拉斯三元组(素勾股数)"></a>EP09-<strong>特殊毕达哥拉斯三元组</strong>(素勾股数)</h2><p>毕达哥拉斯三元组是三个自然数a <b <c组成的集合,并满足</p><p>a 2 + b 2 = c 2</p><p>例如,3 2 + 4 2 = 9 + 16 = 25 = 5 2。</p><p>有且只有一个毕达哥拉斯三元组满足a + b + c =1000。求这个三元组的乘积abc。</p><p><img src="../%E5%9B%BE%E7%89%87/EP09-1.png" alt=""></p><p><img src="../%E5%9B%BE%E7%89%87/EP09.png" alt="性质3"></p><p><img src="../%E5%9B%BE%E7%89%87/EP09-2.png" alt="性质4"></p><p><img src="../%E5%9B%BE%E7%89%87/EP09-4.png" alt="性质4"></p><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iostream></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstdio></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cmath></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstring></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iomanip></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><algorithm></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><map></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><vector></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">gcd</span><span class="params">(<span class="keyword">int</span> a, <span class="keyword">int</span> b)</span> </span>{</span><br><span class="line"> <span class="keyword">return</span> (b ? gcd(b, a % b) : a );</span><br><span class="line">}</span><br><span class="line"><span class="keyword">int</span> ans;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> n = <span class="number">1</span>; n <= <span class="number">33</span>; n++) {</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> m = n + <span class="number">1</span>; m * m + n * n < <span class="number">1000</span>; m++) {</span><br><span class="line"> <span class="keyword">if</span>(gcd(m, n) - <span class="number">1</span>)<span class="keyword">continue</span>;</span><br><span class="line"> <span class="keyword">int</span> a = <span class="number">2</span> * m * n;</span><br><span class="line"> <span class="keyword">int</span> b = m * m - n * n;</span><br><span class="line"> <span class="keyword">int</span> c = m * m + n * n;</span><br><span class="line"> <span class="keyword">if</span>(<span class="number">1000</span> % (a + b + c) == <span class="number">0</span>) {</span><br><span class="line"> <span class="keyword">int</span> k = <span class="number">1000</span> / (a + b + c);</span><br><span class="line"> ans = a * b * c * <span class="built_in">pow</span>(k , <span class="number">3</span>);</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span>(ans)<span class="keyword">break</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span>(ans)<span class="keyword">break</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="built_in">cout</span> << ans << <span class="built_in">endl</span>;</span><br><span class="line"></span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h2 id="EP12-高度可约的三角形数(求整数因子个数)"><a href="#EP12-高度可约的三角形数(求整数因子个数)" class="headerlink" title="EP12-高度可约的三角形数(求整数因子个数)"></a>EP12-高度可约的三角形数(求整数因子个数)</h2><blockquote><p>例如,第7个三角形数是1 + 2 + 3 + 4 + 5 + 6 + 7 =28。三角形数列的前十项分别是:</p><p>1,3,6,10,15,21,28,36,45,55,…</p><p>让我们同轴出前七个三角形数的所有约数:</p><p> <strong>1</strong>:1<br> <strong>3</strong>:1,3<br> <strong>6</strong>:1,2,3,6<br><strong>10</strong>:1,2,5,10<br><strong>15</strong>:1,3,5,15<br><strong>21</strong>:1,3,7,21<br><strong>28</strong>:1,2, 4,7,14,28</p><p>我们可以抛光,28是第一个拥有超过5个约数的三角形数。</p><p>第一个拥有超过500个约数的三角形数是多少?</p></blockquote><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iostream></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstdio></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cmath></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstring></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iomanip></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><algorithm></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><map></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><vector></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> max_n 1000</span></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">int f[max_n + 5] = {0};</span></span><br><span class="line"><span class="comment">int prime[max_n + 5] = {0};</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">void init() {</span></span><br><span class="line"><span class="comment"> for(int i = 2; i <= max_n; i++) {</span></span><br><span class="line"><span class="comment"> if(!prime[i]){ </span></span><br><span class="line"><span class="comment"> prime[++prime[0]] = i;</span></span><br><span class="line"><span class="comment"> f[i] = 2;</span></span><br><span class="line"><span class="comment"> }</span></span><br><span class="line"><span class="comment"> for(int j = 1; j <= prime[0]; j++) {</span></span><br><span class="line"><span class="comment"> if(i * prime[j] > max_n)break;</span></span><br><span class="line"><span class="comment"> prime[i * prime[j]] = 1;</span></span><br><span class="line"><span class="comment"> if(i % prime[j] == 0) {//bu hu su</span></span><br><span class="line"><span class="comment"> int a = i, cnt = 0;</span></span><br><span class="line"><span class="comment"> while(a % prime[j] == 0)a /= prime[j], cnt ++;</span></span><br><span class="line"><span class="comment"> f[i * prime[j]] = f[i] / (cnt + 1) * (cnt + 2);</span></span><br><span class="line"><span class="comment"> break;</span></span><br><span class="line"><span class="comment"> } else { //hu su</span></span><br><span class="line"><span class="comment"> f[i * prime[j]] = f[prime[j]] * f[i];</span></span><br><span class="line"><span class="comment"> }</span></span><br><span class="line"><span class="comment"> }</span></span><br><span class="line"><span class="comment"> </span></span><br><span class="line"><span class="comment"> }</span></span><br><span class="line"><span class="comment"> return;</span></span><br><span class="line"><span class="comment">}</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">int main() {</span></span><br><span class="line"><span class="comment"> init();</span></span><br><span class="line"><span class="comment"> int n = 1, fac = 0;</span></span><br><span class="line"><span class="comment"> while(fac < 500) {</span></span><br><span class="line"><span class="comment"> if(n & 1) {</span></span><br><span class="line"><span class="comment"> fac = </span></span><br><span class="line"><span class="comment"> }</span></span><br><span class="line"><span class="comment"> }</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment"> return 0;</span></span><br><span class="line"><span class="comment">}</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">#include<iostream></span></span><br><span class="line"><span class="comment">#include<set></span></span><br><span class="line"><span class="comment">#include<map></span></span><br><span class="line"><span class="comment">#include<vector></span></span><br><span class="line"><span class="comment">#include<algorithm></span></span><br><span class="line"><span class="comment">using namespace std;</span></span><br><span class="line"><span class="comment">#define max_n 1000</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">int prime[max_n + 5] = {0};</span></span><br><span class="line"><span class="comment">int f[max_n + 5] = {0};</span></span><br><span class="line"><span class="comment">int cnt[max_n + 5] = {0};</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">void init() {</span></span><br><span class="line"><span class="comment"> for (int i = 2; i <= max_n; i++) {</span></span><br><span class="line"><span class="comment"> if (!prime[i]) {</span></span><br><span class="line"><span class="comment"> prime[++prime[0]] = i;</span></span><br><span class="line"><span class="comment"> f[i] = 2;</span></span><br><span class="line"><span class="comment"> cnt[i] = 1;</span></span><br><span class="line"><span class="comment"> }</span></span><br><span class="line"><span class="comment"> for (int j = 1; j <= prime[0]; j++) {</span></span><br><span class="line"><span class="comment"> if (prime[j] * i > max_n) break;</span></span><br><span class="line"><span class="comment"> prime[prime[j] * i] = 1;</span></span><br><span class="line"><span class="comment"> if (i % prime[j] == 0) {</span></span><br><span class="line"><span class="comment"> f[i * prime[j]] = f[i] / (cnt[i] + 1) * (cnt[i] + 2);</span></span><br><span class="line"><span class="comment"> cnt[i * prime[j]] = cnt[i] + 1;</span></span><br><span class="line"><span class="comment"> break;</span></span><br><span class="line"><span class="comment"> } else {</span></span><br><span class="line"><span class="comment"> f[prime[j] * i] = f[prime[j]] * f[i];</span></span><br><span class="line"><span class="comment"> cnt[prime[j] * i] = 1 ;</span></span><br><span class="line"><span class="comment"> }</span></span><br><span class="line"><span class="comment"> }</span></span><br><span class="line"><span class="comment"> }</span></span><br><span class="line"><span class="comment"> return ;</span></span><br><span class="line"><span class="comment">}</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">int main() {</span></span><br><span class="line"><span class="comment"> init();</span></span><br><span class="line"><span class="comment"> for (int i = 2; i <= 100; i++) {</span></span><br><span class="line"><span class="comment"> cout << i << "=" << f[i] << endl;</span></span><br><span class="line"><span class="comment"> }</span></span><br><span class="line"><span class="comment"> return 0;</span></span><br><span class="line"><span class="comment">}</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">init</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">2</span>; i <= max_n; i++) {</span><br><span class="line"> <span class="keyword">if</span>(!prime[i]) { </span><br><span class="line"> prime[++prime[<span class="number">0</span>]] = i;</span><br><span class="line"> f[i] = <span class="number">2</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> j = <span class="number">1</span>; j <= prime[<span class="number">0</span>]; j++) {</span><br><span class="line"> <span class="keyword">if</span>(i * prime[j] > max_n) <span class="keyword">break</span>;</span><br><span class="line"> <span class="keyword">if</span>(i % prime[j] == <span class="number">0</span>) {</span><br><span class="line"> <span class="keyword">int</span> a = i, cnt = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">while</span>(i % prime[j] == <span class="number">0</span>) {</span><br><span class="line"> a /= prime[j];</span><br><span class="line"> cnt ++;</span><br><span class="line"> }</span><br><span class="line"> f[i * prime[j]] = f[i] / (cnt + <span class="number">1</span>) * (cnt + <span class="number">2</span>);</span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> } <span class="keyword">else</span> {</span><br><span class="line"> f[i * prime[j]] = f[i] * f[prime[j]];</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{</span><br><span class="line"> </span><br><span class="line">}</span><br></pre></td></tr></table></figure><h2 id="EP-15-晶格路径(求组合数,一乘一除)"><a href="#EP-15-晶格路径(求组合数,一乘一除)" class="headerlink" title="EP-15 晶格路径(求组合数,一乘一除)"></a>EP-15 晶格路径(求组合数,一乘一除)</h2><p>从2×2网格的左上角开始,并且只能向右和向下移动,到右下角有6条路线。</p><p><img src="https://projecteuler.net/project/images/p015.png" alt="img"></p><p>通过20×20网格有多少条这样的路线?</p><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iostream></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstdio></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cmath></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstring></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iomanip></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><algorithm></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><map></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><vector></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">long</span> <span class="keyword">long</span> m = <span class="number">40</span>, n = <span class="number">20</span>, ans = <span class="number">1</span>; <span class="comment">// 求</span></span><br><span class="line"> <span class="keyword">while</span>(m != <span class="number">20</span>|| n) {</span><br><span class="line"> <span class="keyword">if</span>(m != <span class="number">20</span> )ans *= (m --);</span><br><span class="line"> <span class="keyword">if</span>(n != <span class="number">0</span> && ans % n == <span class="number">0</span>) ans /= (n--);</span><br><span class="line"> }</span><br><span class="line"> <span class="built_in">cout</span> << ans << <span class="built_in">endl</span>;</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h2 id="EP-16功率数字总和(高精度幂)"><a href="#EP-16功率数字总和(高精度幂)" class="headerlink" title="EP-16功率数字总和(高精度幂)"></a>EP-16功率数字总和(高精度幂)</h2><p>2 ^15 = 32768,其位数之和为3 + 2 + 7 + 6 + 8 = 26。</p><p>2 ^1000的位数总和是多少?</p><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iostream></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstdio></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cmath></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstring></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iomanip></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><algorithm></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><map></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><vector></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> max_n 400</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">int</span> ans[max_n + <span class="number">5</span>] = {<span class="number">1</span>, <span class="number">1</span>, <span class="number">0</span>};</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">1</span>; i <= <span class="number">100</span>; i++) {</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> j = <span class="number">1</span>; j <= ans[<span class="number">0</span>]; j++) {</span><br><span class="line"> ans[j] *= <span class="number">1024</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> k = <span class="number">1</span>; k <= ans[<span class="number">0</span>]; k++) {</span><br><span class="line"> <span class="keyword">if</span>(ans[k] < <span class="number">10</span>)<span class="keyword">continue</span>;</span><br><span class="line"> ans[k + <span class="number">1</span>] += ans[k] / <span class="number">10</span>;</span><br><span class="line"> ans[k] %= <span class="number">10</span>;</span><br><span class="line"> ans[<span class="number">0</span>] += (k == ans[<span class="number">0</span>]);</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">int</span> sum = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">1</span>; i <= ans[<span class="number">0</span>]; i++) {</span><br><span class="line"> sum += ans[i];</span><br><span class="line"> }</span><br><span class="line"> <span class="built_in">cout</span> << sum << <span class="built_in">endl</span>;</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h2 id="EP-18-最大路径和-(动态规划)"><a href="#EP-18-最大路径和-(动态规划)" class="headerlink" title="EP-18 最大路径和 (动态规划)"></a>EP-18 最大路径和 (动态规划)</h2><blockquote><p>从下面展示的三角形的顶端出发,不断移动到在下一行与其相邻的元素,能够得到的最大路径和是23。</p><p><strong>3</strong><br><strong>7</strong> 4<br>2 <strong>4</strong> 6<br>8 5 <strong>9</strong> 3</p><p>如上图,最大路径和为 3 + 7 + 4 + 9 = 23。</p><p>求从下面展示的三角形顶端出发到达底部,所能够得到的最大路径和:</p><p>75<br>95 64<br>17 47 82<br>18 35 87 10<br>20 04 82 47 65<br>19 01 23 75 03 34<br>88 02 77 73 07 63 67<br>99 65 04 28 06 16 70 92<br>41 41 26 56 83 40 80 70 33<br>41 48 72 33 47 32 37 16 94 29<br>53 71 44 65 25 43 91 52 97 51 14<br>70 11 33 28 77 73 17 78 39 68 17 57<br>91 71 52 38 17 14 91 43 58 50 27 29 48<br>63 66 04 68 89 53 67 30 73 16 69 87 40 31<br>04 62 98 27 23 09 70 98 73 93 38 53 60 04 23</p><p><strong>注意:</strong> 在这个问题中,由于只有16384条路径,通过尝试所有的路径来解决问题是可行的。但是,对于<a href="http://pe-cn.github.io/67" target="_blank" rel="noopener">第67题</a>,虽然是一道相同类型的题目,但是三角形将拥有一百行,此时暴力破解将不能解决,而需要一个更加聪明的办法!;o)</p></blockquote><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iostream></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstdio></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cmath></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstring></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iomanip></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><algorithm></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><map></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><vector></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> max_n 20</span></span><br><span class="line"><span class="keyword">int</span> val[max_n][max_n]={<span class="number">0</span>};</span><br><span class="line"><span class="keyword">int</span> ans[max_n][max_n] = {<span class="number">0</span>}; <span class="comment">//记忆化dfs</span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">dfs</span><span class="params">(<span class="keyword">int</span> i, <span class="keyword">int</span> j, <span class="keyword">int</span> n)</span> </span>{</span><br><span class="line"> <span class="keyword">if</span>(i + <span class="number">1</span> == n)<span class="keyword">return</span> val[i][j]; <span class="comment">// 如果是最后一行</span></span><br><span class="line"> <span class="keyword">if</span>(ans[i][j])<span class="keyword">return</span> ans[i][j]; <span class="comment">// 记忆化,如果有使用的值,则可以直接拿来用</span></span><br><span class="line"> <span class="keyword">int</span> val1 = dfs(i + <span class="number">1</span>, j, n); </span><br><span class="line"> <span class="keyword">int</span> val2 = dfs(i + <span class="number">1</span>, j + <span class="number">1</span>, n);</span><br><span class="line"> <span class="keyword">return</span> ans[i][j] = (val1 > val2 ? val1 :val2) + val[i][j];</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i < <span class="number">15</span>; i++) {</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> j = <span class="number">0</span>; j <= i; j++) {</span><br><span class="line"> <span class="built_in">cin</span> >> val[i][j];</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="built_in">cout</span> << dfs(<span class="number">0</span>, <span class="number">0</span>, max_n);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><iostream></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> max_n 20</span></span><br><span class="line"><span class="keyword">int</span> vak[max_n + <span class="number">5</span>][max_n + <span class="number">5</span>] = {<span class="number">0</span>};</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i < max_n; i ++) {</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> j = <span class="number">0</span>; j <= i; j++) {</span><br><span class="line"> <span class="built_in">cin</span> >> val[i][j];</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> max_n - <span class="number">2</span>; i >= <span class="number">0</span>; i--) {</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> j = <span class="number">0</span>; j <= i; j++) {</span><br><span class="line"> f[i][j] = max(val[i + <span class="number">1</span>][j], val[i + <span class="number">1</span>][j + <span class="number">1</span>]) + val[i][j]; <span class="comment">// 状态转移公式,从下往上推</span></span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="built_in">cout</span> << f[<span class="number">0</span>][<span class="number">0</span>] << <span class="built_in">endl</span>;</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h2 id="EP-24字典序排列(全排列)"><a href="#EP-24字典序排列(全排列)" class="headerlink" title="EP-24字典序排列(全排列)"></a>EP-24<strong>字典序排列</strong>(全排列)</h2><blockquote><p>例如,3124是数字1、2、3、4的一个分开。如果把所有排列按照数字大小或字母先后进行排序,我们称为字典序排列。0、1、2的字典序排列是:</p><p>012 021 102 120 201 210</p><p>数字0、1、2、3、4、5、6、7、8、9的字典序分开中第一百万位的排列是什么?</p></blockquote><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iostream></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstdio></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cmath></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstring></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iomanip></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><algorithm></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><map></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><vector></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> max_n 10</span></span><br><span class="line"><span class="keyword">int</span> fac[max_n]; <span class="comment">// 存谁的阶乘</span></span><br><span class="line"><span class="keyword">int</span> num[max_n]; <span class="comment">// 存一个数是否使用过,如果没使用为1,否则为0</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">init</span><span class="params">(<span class="keyword">int</span> n)</span> </span>{</span><br><span class="line"> fac[<span class="number">0</span>] = <span class="number">1</span>; <span class="comment">// 0的阶乘为1</span></span><br><span class="line"> num[<span class="number">0</span>] = <span class="number">1</span>; <span class="comment">// 将num[0]初始化为1,因为在下面循环随着1开始的所以这里要初始化num[0] = 1;</span></span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">1</span>; i <= n; i++) { <span class="comment">//求阶乘</span></span><br><span class="line"> fac[i] = i * fac[i - <span class="number">1</span>]; <span class="comment">// 从0~n的每个数的阶乘存在数组里打表</span></span><br><span class="line"> num[i] = <span class="number">1</span>; <span class="comment">// 将未使用过num[i] 标记为1,</span></span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span>;</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">get_num</span><span class="params">(<span class="keyword">int</span> k, <span class="keyword">int</span> n, <span class="keyword">int</span> &x)</span> </span>{ <span class="comment">// 每次传来k是还需要多少能到所得项,n是每个数的阶乘,x是带回其此位的值</span></span><br><span class="line"> <span class="keyword">int</span> step = k / n; <span class="comment">// 需要上升几步</span></span><br><span class="line"> x = <span class="number">0</span>; <span class="comment">// 初始化x为0</span></span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> t = <span class="number">0</span>; t <= step; x += (t <= step)) { <span class="comment">// 将t走几步, 只要t <=step时,意思是还得走</span></span><br><span class="line"> t += num[x]; <span class="comment">// 每次t所加的是没有走过, + 0 || + 1</span></span><br><span class="line"> }</span><br><span class="line"> k %= n; <span class="comment">// k % n = k还需要多少能到达其第几个</span></span><br><span class="line"> num[x] = <span class="number">0</span>;<span class="comment">//走过的标记为0</span></span><br><span class="line"> <span class="keyword">return</span> k;</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{ </span><br><span class="line"> init(max_n);</span><br><span class="line"> <span class="keyword">int</span> k = <span class="number">1000000</span> - <span class="number">1</span>;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = max_n; i > <span class="number">0</span>; i --) {</span><br><span class="line"> <span class="keyword">int</span> x;</span><br><span class="line"> k = get_num(k, fac[i - <span class="number">1</span>], x);</span><br><span class="line"> <span class="built_in">cout</span> << x;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><img src="../图片/EP24.png" style="zoom: 200%;" /><h2 id="EP-26倒数的循环节-最大的循环节"><a href="#EP-26倒数的循环节-最大的循环节" class="headerlink" title="EP-26倒数的循环节(最大的循环节)"></a>EP-26倒数的循环节(最大的循环节)</h2><blockquote><p>单位分数指分子为1的分数。分母为2至10的单位分数的十进制表示如下所示:</p><p>1 / 2 = 0.5<br>1 / 3 = 0(3)<br>1 / 4 = 0.25<br>1 / 5 = 0.2<br>1 / 6 = 0.1(6)<br>1 / 7 = 0。(142857)<br>1 / 8 = 0.125<br>1 / 9 = 0(1)<br>1 / 10 = 0.1</p><p>这里0.1(6)表示0.166666 …,括号内表示有一个循环节。可以研磨,1 / 7有六位循环节。</p><p>找出正整数d <1000,其倒数的十进制表示小数部分有最大的循环节。</p></blockquote><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iostream></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstdio></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cmath></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstring></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iomanip></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><algorithm></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><map></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><vector></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> max_n 1000</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">int</span> r_len[max_n + <span class="number">5</span>] = {<span class="number">0</span>};</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">get_len</span><span class="params">(<span class="keyword">int</span> d)</span> </span>{</span><br><span class="line"> <span class="built_in">memset</span>(r_len, <span class="number">0</span>, <span class="keyword">sizeof</span>(r_len));</span><br><span class="line"> <span class="keyword">int</span> r = <span class="number">1</span>, t = <span class="number">1</span>;</span><br><span class="line"> r_len[r] = <span class="number">1</span>;</span><br><span class="line"> <span class="keyword">while</span> (d) {</span><br><span class="line"> r *= <span class="number">10</span>;</span><br><span class="line"> r %= d;</span><br><span class="line"> t += <span class="number">1</span>;</span><br><span class="line"> <span class="keyword">if</span>(r_len[r]) <span class="keyword">return</span> t - r_len[r];</span><br><span class="line"> r_len[r] = t;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">int</span> d = <span class="number">0</span>, len = <span class="number">-1</span>;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">2</span>; i <= max_n; i++) {</span><br><span class="line"> <span class="keyword">int</span> tmp = get_len(i);</span><br><span class="line"> <span class="keyword">if</span>(len < tmp) {</span><br><span class="line"> len = tmp;</span><br><span class="line"> d = i;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="built_in">cout</span> << d << <span class="built_in">endl</span>;</span><br><span class="line"> <span class="built_in">cout</span> << len <<<span class="built_in">endl</span>;</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h2 id="EP29-不同的幂-大整数幂&比较大小"><a href="#EP29-不同的幂-大整数幂&比较大小" class="headerlink" title="EP29-不同的幂(大整数幂&比较大小)"></a>EP29-<strong>不同的幂</strong>(大整数幂&比较大小)</h2><p>考虑所有满足2≤a≤5和2≤b≤5的整体组合生成的幂a b:</p><blockquote><p>2 2 = 4,2 3 = 8,2 4 = 16,2 5 = 32<br>3 2 = 9,3 3 = 27,3 4 = 81,3 5 = 243<br>4 2 = 16,4 3 = 64,4 4 = 256、4 5 = 1024<br>5 2 = 25、5 3 = 125、5 4 = 625、5 5 = 3125</p></blockquote><p>如果把这些幂按照大小排列并去重,我们得到以下由15个不同的项组成的序列:</p><p>4,8,9,16,25,27,32,64,81,125,243,256,625,1024,3125</p><p>在所有满足2≤a≤100和2≤b≤100的整体组合生成的幂a b分布并去重所得到的序列中,有多少个不同的项?</p><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iostream></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstdio></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cmath></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstring></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iomanip></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><algorithm></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><map></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><vector></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> max_n 10000</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> max_m 210</span></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">int result[max_n + 5][max_m];</span></span><br><span class="line"><span class="comment">int result_len = 0;</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">int find_result(int *num) {</span></span><br><span class="line"><span class="comment"> for(int i = 0; i < result_len; i++) {</span></span><br><span class="line"><span class="comment"> if(memcmp(result + i, num,sizeof(int)*max_m) == 0) {</span></span><br><span class="line"><span class="comment"> return i + 1;</span></span><br><span class="line"><span class="comment"> }</span></span><br><span class="line"><span class="comment"> }</span></span><br><span class="line"><span class="comment"> return 0;</span></span><br><span class="line"><span class="comment">}</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">int main() {</span></span><br><span class="line"><span class="comment"> for(int a = 2; a <= 100; a++) {</span></span><br><span class="line"><span class="comment"> for(int b = 2; b <= 100; b++) {</span></span><br><span class="line"><span class="comment"> int temp[max_m] = {1, 1, 0};</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment"> for(int i = 0; i < b; i++) {</span></span><br><span class="line"><span class="comment"> for(int j = 1; j <= temp[0]; j++) temp[j] *= a; </span></span><br><span class="line"><span class="comment"> for(int j = 1; j <= temp[0]; j++) { </span></span><br><span class="line"><span class="comment"> if(temp[j] < 10) continue;</span></span><br><span class="line"><span class="comment"> temp[j + 1] += temp[j] / 10;</span></span><br><span class="line"><span class="comment"> temp[j] %= 10;</span></span><br><span class="line"><span class="comment"> temp[0] += (temp[0] == j);}</span></span><br><span class="line"><span class="comment"> }</span></span><br><span class="line"><span class="comment"> </span></span><br><span class="line"><span class="comment"> if (find_result(temp) == 0) {</span></span><br><span class="line"><span class="comment"> memcpy(result + result_len, temp, sizeof(int) * max_m);</span></span><br><span class="line"><span class="comment"> result_len += 1;</span></span><br><span class="line"><span class="comment"> }</span></span><br><span class="line"><span class="comment"> }</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment"> }</span></span><br><span class="line"><span class="comment"> cout << result_len<<endl;</span></span><br><span class="line"><span class="comment"> return 0;</span></span><br><span class="line"><span class="comment">}</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="keyword">int</span> *result[max_n + <span class="number">5</span>];</span><br><span class="line"><span class="keyword">int</span> result_len = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">find_result</span><span class="params">(<span class="keyword">int</span> *num)</span> </span>{</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i < result_len; i++) {</span><br><span class="line"> <span class="keyword">if</span>(<span class="built_in">memcmp</span>(result[i], num,<span class="keyword">sizeof</span>(<span class="keyword">int</span>)*max_m) == <span class="number">0</span>) {</span><br><span class="line"> <span class="keyword">return</span> i + <span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> *<span class="title">calc</span><span class="params">(<span class="keyword">int</span> a, <span class="keyword">int</span> b)</span> </span>{</span><br><span class="line"> <span class="keyword">int</span> *temp = (<span class="keyword">int</span> *)<span class="built_in">calloc</span>(<span class="keyword">sizeof</span>(<span class="keyword">int</span>), max_m);</span><br><span class="line"> temp[<span class="number">0</span>] = <span class="number">1</span>, temp[<span class="number">1</span>] = <span class="number">1</span>;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i < b; i++) {</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> j = <span class="number">1</span>; j <= temp[<span class="number">0</span>]; j++) temp[j] *= a; </span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> j = <span class="number">1</span>; j <= temp[<span class="number">0</span>]; j++) { </span><br><span class="line"> <span class="keyword">if</span>(temp[j] < <span class="number">10</span>) <span class="keyword">continue</span>;</span><br><span class="line"> temp[j + <span class="number">1</span>] += temp[j] / <span class="number">10</span>;</span><br><span class="line"> temp[j] %= <span class="number">10</span>;</span><br><span class="line"> temp[<span class="number">0</span>] += (temp[<span class="number">0</span>] == j);</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> temp;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> a = <span class="number">2</span>; a <= <span class="number">100</span>; a++) {</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> b = <span class="number">2</span>; b <= <span class="number">100</span>; b++) {</span><br><span class="line"> <span class="keyword">int</span> *temp = calc(a , b);</span><br><span class="line"> <span class="keyword">if</span> (find_result(temp) == <span class="number">0</span>) {</span><br><span class="line"> result[result_len++] = temp;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> }</span><br><span class="line"> <span class="built_in">cout</span> << result_len<<<span class="built_in">endl</span>;</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h2 id="EP-39整个边长直角三角形-素勾股数"><a href="#EP-39整个边长直角三角形-素勾股数" class="headerlink" title="EP-39整个边长直角三角形(素勾股数)"></a>EP-39整个边长直角三角形(素勾股数)</h2><p>若三边长{a,b,c}插入整数的直角三角形周长为p,当p = 120时,恰好存在三个不同的解:</p><p>{20,48,52},{24,45,51},{30,40,50}</p><p>在所有的p≤1000中,p取何值时有解的数目最多?</p><p>一个素勾股数的倍数是勾股数</p><blockquote><p>a = m * m - n * n;</p><p>b = 2 *n * m;</p><p>c = m * m + n * n;</p></blockquote><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iostream></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstdio></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cmath></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstring></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iomanip></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><algorithm></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><map></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><vector></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> max_n 1000</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">int</span> cnt[max_n +<span class="number">5</span>] = {<span class="number">0</span>};</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">gcd</span><span class="params">(<span class="keyword">int</span> a, <span class="keyword">int</span> b)</span> </span>{</span><br><span class="line"> <span class="keyword">return</span> (b ? gcd(b, a % b) : a);</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> n = <span class="number">1</span>; n <= <span class="number">32</span>; n++) {</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> m = n + <span class="number">1</span>; m <= <span class="number">32</span>; m++) {</span><br><span class="line"> <span class="keyword">if</span>(gcd(n, m) - <span class="number">1</span>)<span class="keyword">continue</span>;</span><br><span class="line"> <span class="keyword">int</span> a = m * m - n * n;</span><br><span class="line"> <span class="keyword">int</span> b = <span class="number">2</span> * m * n;</span><br><span class="line"> <span class="keyword">int</span> c = m * m + n * n;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> p = a + b + c; p <= <span class="number">1000</span>; p+= (a + b + c)) {</span><br><span class="line"> cnt[p] += <span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">int</span> ans = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">1</span>; i <= max_n; i++) {</span><br><span class="line"> <span class="keyword">if</span>(cnt[i] > cnt[ans])ans = i;</span><br><span class="line"> }</span><br><span class="line"> </span><br><span class="line"> <span class="built_in">cout</span> << ans << <span class="built_in">endl</span>;</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h2 id="EP-47不同的素数因子"><a href="#EP-47不同的素数因子" class="headerlink" title="EP-47不同的素数因子"></a>EP-47不同的素数因子</h2><blockquote><p>不同的质因数**</p><p>首次出现连续两个数均有两个不同的质因数是在:</p><p>14 = 2 × 7<br>15 = 3 × 5</p><p>首次出现连续三个数均有三个不同的质因数是在:</p><p>644 = 22 × 7 × 23<br>645 = 3 × 5 × 43<br>646 = 2 × 17 × 19</p><p>首次出现连续四个数均有四个不同的质因数时,其中的第一个数是多少?</p></blockquote><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iostream></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstdio></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cmath></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstring></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iomanip></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><algorithm></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><map></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><vector></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> max_n 1000000</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">int</span> prime[max_n + <span class="number">5</span>] = {<span class="number">0</span>};</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">init</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">2</span>; i <= max_n; i++) {</span><br><span class="line"> <span class="keyword">if</span>(prime[i]) <span class="keyword">continue</span>;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> j = i; j <= max_n; j+=i) {</span><br><span class="line"> prime[j] += <span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span>;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{</span><br><span class="line"> init();</span><br><span class="line"> <span class="keyword">int</span> ans = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">1000</span>; i < max_n; i++) {</span><br><span class="line"> <span class="keyword">int</span> flag = <span class="number">1</span>;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> j = <span class="number">0</span>; j < <span class="number">4</span> && flag ; j ++) {</span><br><span class="line"> flag = (prime[i + j] == <span class="number">4</span>);</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span>(!flag) <span class="keyword">continue</span>;</span><br><span class="line"> ans = i;</span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="built_in">cout</span> << ans << <span class="built_in">endl</span>;</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h2 id="EP-21亲和数"><a href="#EP-21亲和数" class="headerlink" title="EP-21亲和数"></a><strong>EP-21亲和数</strong></h2><p>记d(n)为n的所有真因数(小于n且整除n的正整数)之和。<br>如果d(a) = b且d(b) = a,且a ≠ b,那么a和b构成一个亲和数对,a和b被称为亲和数。</p><p>例如,220的真因数包括1、2、4、5、10、11、20、22、44、55和110,因此d(220) = 284;而284的真因数包括1、2、4、71和142,因此d(284) = 220。</p><p>求所有小于10000的亲和数的和。</p><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iostream></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstdio></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cmath></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstring></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iomanip></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><algorithm></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><map></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><vector></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> max_n 10000</span></span><br><span class="line"><span class="number">1.</span>...........................................</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">int f[max_n + 5] = {0};</span></span><br><span class="line"><span class="comment">void init() {</span></span><br><span class="line"><span class="comment"> for(int i = 2; i <= max_n; i++) {</span></span><br><span class="line"><span class="comment"> for(int j = 1; j * j <= i; j++) {</span></span><br><span class="line"><span class="comment"> if(i % j)continue;</span></span><br><span class="line"><span class="comment"> f[i] += j;</span></span><br><span class="line"><span class="comment"> f[i] += i / j;</span></span><br><span class="line"><span class="comment"> }</span></span><br><span class="line"><span class="comment"> f[i] -= i;</span></span><br><span class="line"><span class="comment"> }</span></span><br><span class="line"><span class="comment"> return ;</span></span><br><span class="line"><span class="comment">}</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="number">2.</span>...............................................</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">void init() {</span></span><br><span class="line"><span class="comment"> for(int i = 1; i <= max_n; i++) {</span></span><br><span class="line"><span class="comment"> for(int j = 2; j * j <= max_n;j ++) {</span></span><br><span class="line"><span class="comment"> f[i * j] += i;</span></span><br><span class="line"><span class="comment"> }</span></span><br><span class="line"><span class="comment"> }</span></span><br><span class="line"><span class="comment"> return ;</span></span><br><span class="line"><span class="comment">}</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">int main() {</span></span><br><span class="line"><span class="comment"> init();</span></span><br><span class="line"><span class="comment"> long long sum = 0;</span></span><br><span class="line"><span class="comment"> for(int i = 2; i < max_n; i++) {</span></span><br><span class="line"><span class="comment"> if(f[i] != i && f[i] < max_n &&i == f[f[i]])sum += i;</span></span><br><span class="line"><span class="comment"> }</span></span><br><span class="line"><span class="comment"> cout << sum << endl;</span></span><br><span class="line"><span class="comment"> return 0;</span></span><br><span class="line"><span class="comment">}</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="number">3.</span>.....................................................</span><br><span class="line"><span class="keyword">int</span> prime[max_n + <span class="number">5</span>] = {<span class="number">0</span>};</span><br><span class="line"><span class="keyword">int</span> f[max_n + <span class="number">5</span>] = {<span class="number">0</span>};</span><br><span class="line"><span class="keyword">int</span> cnt[max_n + <span class="number">5</span>] = {<span class="number">0</span>};</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">init</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">2</span>; i <= max_n; i++) {</span><br><span class="line"> <span class="keyword">if</span>(!prime[i]){</span><br><span class="line"> prime[++prime[<span class="number">0</span>]] = i;</span><br><span class="line"> f[i] = i + <span class="number">1</span>; <span class="comment">// f[i]存素因子的和</span></span><br><span class="line"> cnt[i] = <span class="number">1</span>; <span class="comment">// 最小素因子的指数</span></span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> j = <span class="number">1</span>; j <= prime[<span class="number">0</span>]; j++) {</span><br><span class="line"> <span class="keyword">if</span>(prime[j] * i > max_n)<span class="keyword">break</span>;</span><br><span class="line"> prime[prime[j] * i] = <span class="number">1</span>;</span><br><span class="line"> <span class="keyword">if</span>(i % prime[j] == <span class="number">0</span>) {</span><br><span class="line"> f[i * prime[j]] = f[i] / (<span class="built_in">pow</span>(prime[j], cnt[i] + <span class="number">1</span>) - <span class="number">1</span>) * (<span class="built_in">pow</span>(prime[j], cnt[i] + <span class="number">2</span>) - <span class="number">1</span>);</span><br><span class="line"> cnt[i * prime[j]] = cnt[i] + <span class="number">1</span>;</span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> }<span class="keyword">else</span> {<span class="comment">//fei hu su</span></span><br><span class="line"> f[i * prime[j]] = f[i] * f[prime[j]];</span><br><span class="line"> cnt[i * prime[j]] = <span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> } </span><br><span class="line">}</span><br><span class="line"><span class="number">4.</span>.........................................................</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">void init() {</span></span><br><span class="line"><span class="comment"> for(int i = 2; i <= max_n; i++) {</span></span><br><span class="line"><span class="comment"> if(!prime[i]){</span></span><br><span class="line"><span class="comment"> prime[++prime[0]] = i;</span></span><br><span class="line"><span class="comment"> f[i] = i + 1;</span></span><br><span class="line"><span class="comment"> cnt[i] = i * i;</span></span><br><span class="line"><span class="comment"> }</span></span><br><span class="line"><span class="comment"> for(int j = 1; j <= prime[0]; j++) {</span></span><br><span class="line"><span class="comment"> if(prime[j] * i > max_n)break;</span></span><br><span class="line"><span class="comment"> prime[prime[j] * i] = 1;</span></span><br><span class="line"><span class="comment"> if(i % prime[j] == 0) {</span></span><br><span class="line"><span class="comment"> f[i * prime[j]] = f[i] * (cnt[i] * prime[j] - 1) / (cnt[i] - 1);</span></span><br><span class="line"><span class="comment"> cnt[i * prime[j]] = cnt[i] + prime[j];</span></span><br><span class="line"><span class="comment"> break;</span></span><br><span class="line"><span class="comment"> }else {//fei hu su</span></span><br><span class="line"><span class="comment"> f[i * prime[j]] = f[i] * f[prime[j]];</span></span><br><span class="line"><span class="comment"> cnt[i * prime[j]] = prime[j] * prime[j];</span></span><br><span class="line"><span class="comment"> }</span></span><br><span class="line"><span class="comment"> }</span></span><br><span class="line"><span class="comment"> } </span></span><br><span class="line"><span class="comment">}</span></span><br><span class="line"><span class="comment">*/</span></span><br></pre></td></tr></table></figure><p>![](../图片/2019-12-29 19-13-02屏幕截图.png)</p><p>![](../图片/2019-12-29 19-18-59屏幕截图.png)</p><p><img src="../%E5%9B%BE%E7%89%87/%E7%BA%A6%E6%9D%9F%E5%92%8C.png" alt=""></p><h1 id="小项-练习"><a href="#小项-练习" class="headerlink" title="小项(练习)"></a>小项(练习)</h1><h2 id="阶乘分解素数"><a href="#阶乘分解素数" class="headerlink" title="阶乘分解素数"></a>阶乘分解素数</h2><blockquote><h4 id="题目描述"><a href="#题目描述" class="headerlink" title="题目描述"></a>题目描述</h4><p>给定整数 N(1≤N≤106)N(1≤N≤106),试把阶乘 N!N! 分解质因数,按照算术基本定理的形式输出分解结果中的 pi 和 ci 即可。</p><hr><h4 id="输入"><a href="#输入" class="headerlink" title="输入"></a>输入</h4><p>一个整数 N。</p><h4 id="输出"><a href="#输出" class="headerlink" title="输出"></a>输出</h4><p>N!N! 分解质因数后的结果,共若干行,每行一对 pi,ci,表示含有 pciipici 项。按照pi 从小到大的顺序输出。</p><hr><h4 id="样例输入"><a href="#样例输入" class="headerlink" title="样例输入"></a>样例输入</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">>5</span><br></pre></td></tr></table></figure><h4 id="样例输出"><a href="#样例输出" class="headerlink" title="样例输出"></a>样例输出</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">>2 3</span><br><span class="line">>3 1</span><br><span class="line">>5 1</span><br></pre></td></tr></table></figure><h4 id="样例说明"><a href="#样例说明" class="headerlink" title="样例说明"></a>样例说明</h4><p>5!=120=23∗31∗51</p></blockquote><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iostream></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstdio></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cmath></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstring></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iomanip></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><algorithm></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><map></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><vector></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> max_n 150000</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">int</span> cnt[max_n];</span><br><span class="line"></span><br><span class="line"><span class="keyword">int</span> prime[max_n];</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">init</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">2</span>; i <= max_n; i++) {</span><br><span class="line"> <span class="keyword">if</span>(!prime[i]) {</span><br><span class="line"> prime[++prime[<span class="number">0</span>]] = i;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> j = <span class="number">1</span>; j <= prime[<span class="number">0</span>]; j++) {</span><br><span class="line"> <span class="keyword">if</span>(i * prime[j] > max_n)<span class="keyword">break</span>;</span><br><span class="line"> prime[i * prime[j]] = <span class="number">1</span>;</span><br><span class="line"> <span class="keyword">if</span>(i % prime[j] == <span class="number">0</span>)<span class="keyword">break</span>;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">// <1></span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">int</span> n;</span><br><span class="line"> <span class="built_in">cin</span> >> n;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">2</span>; i <= n; i++) {</span><br><span class="line"> <span class="keyword">int</span> num = i;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> j = <span class="number">2</span>; j * j <= num; j++) {</span><br><span class="line"> <span class="keyword">if</span>(num % j)<span class="keyword">continue</span>; <span class="comment">// j为素数往下走...若9%2 != 0不往下走9 % 3往下走</span></span><br><span class="line"> <span class="keyword">while</span>(num % j == <span class="number">0</span>) { <span class="comment">// 素数j</span></span><br><span class="line"> cnt[j]++; <span class="comment">// cnt[j] 存素数j的个数</span></span><br><span class="line"> num /= j; </span><br><span class="line"> }</span><br><span class="line"> cnt[num] ++; <span class="comment">// 2 * 2 * 3得把最后一个3存上</span></span><br><span class="line"> }</span><br><span class="line"> cnt[i]++; <span class="comment">// 不进循环</span></span><br><span class="line"> }</span><br><span class="line"> init();</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">1</span>; prime[i] <= n; i++) {</span><br><span class="line"> <span class="built_in">cout</span> << prime[i] <<<span class="string">' '</span> << cnt[prime[i]] << <span class="built_in">endl</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">// <2></span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">init</span><span class="params">(<span class="keyword">int</span> n)</span> </span>{</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">2</span>; i <= n; i++) {</span><br><span class="line"> <span class="keyword">if</span>(!prime[i]) {</span><br><span class="line"> prime[++prime[<span class="number">0</span>]] = i;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> j = <span class="number">1</span>; j <= prime[<span class="number">0</span>]; j++) {</span><br><span class="line"> <span class="keyword">if</span>(i * prime[j] > n)<span class="keyword">break</span>;</span><br><span class="line"> prime[i * prime[j]] = <span class="number">1</span>;</span><br><span class="line"> <span class="keyword">if</span>(i % prime[j] == <span class="number">0</span>)<span class="keyword">break</span>;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">int</span> n;;</span><br><span class="line"> <span class="built_in">cin</span> >> n;</span><br><span class="line"> init(n);</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">2</span>; i <= n; i++) { <span class="comment">// 遍历2 . 3 . 4 . 5 //正好 5!= 2 * 3 * 4 *5</span></span><br><span class="line"> <span class="keyword">int</span> temp = i;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> j = <span class="number">1</span>; j <= prime[<span class="number">0</span>] && temp >= prime[j]; j++) { <span class="comment">// 遍历比temp小的素数</span></span><br><span class="line"> <span class="keyword">while</span>(temp % prime[j] == <span class="number">0</span>) { <span class="comment">// 得知temp每个数中有几个素数</span></span><br><span class="line"> temp /= prime[j]; </span><br><span class="line"> cnt[prime[j]]++; <span class="comment">// 将素数存起来</span></span><br><span class="line"> } </span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">1</span>; i <= prime[<span class="number">0</span>]; i++) {</span><br><span class="line"> <span class="keyword">if</span>(!cnt[prime[i]])<span class="keyword">continue</span>;</span><br><span class="line"> <span class="built_in">cout</span> << prime[i] << <span class="string">' '</span><< cnt[prime[i]] << <span class="built_in">endl</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">5 ! = 1 * 2 * 3 * 4 * 5</span></span><br><span class="line"><span class="comment">2中含1个2</span></span><br><span class="line"><span class="comment">3中含1个3</span></span><br><span class="line"><span class="comment">4中含2个2</span></span><br><span class="line"><span class="comment">5中含1个5</span></span><br><span class="line"><span class="comment">所以遍历每个数中的素数,因为一个数是由若干个素数乘积组成的;</span></span><br><span class="line"><span class="comment">所用的算法是:</span></span><br><span class="line"><span class="comment">while(temp % prime[j] == 0) {</span></span><br><span class="line"><span class="comment">temp /= prime[j];</span></span><br><span class="line"><span class="comment">cnt[prime[j]] ++; //计数</span></span><br><span class="line"><span class="comment">}</span></span><br><span class="line"><span class="comment">然后外层循环一下从2~5就ok了</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"></span><br><span class="line"> <span class="comment">// <3></span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">init</span><span class="params">(<span class="keyword">int</span> n)</span> </span>{</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">2</span>; i <= n; i++) {</span><br><span class="line"> <span class="keyword">if</span>(!prime[i]) {</span><br><span class="line"> prime[++prime[<span class="number">0</span>]] = i;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> j = <span class="number">1</span>; j <= prime[<span class="number">0</span>]; j++) {</span><br><span class="line"> <span class="keyword">if</span>(i * prime[j] > n)<span class="keyword">break</span>;</span><br><span class="line"> prime[i * prime[j]] = <span class="number">1</span>;</span><br><span class="line"> <span class="keyword">if</span>(i % prime[j] == <span class="number">0</span>)<span class="keyword">break</span>;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">long</span> <span class="keyword">long</span> n;</span><br><span class="line"> <span class="built_in">cin</span> >> n;</span><br><span class="line"> init(n);</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> j = <span class="number">1</span>; j <= prime[<span class="number">0</span>]; j++) {</span><br><span class="line"> <span class="keyword">long</span> <span class="keyword">long</span> temp = prime[i], cnt = <span class="number">0</span>; <span class="comment">//先将temp等于一个素数</span></span><br><span class="line"> <span class="keyword">while</span>(temp <= n) {</span><br><span class="line"> cnt += n / temp;</span><br><span class="line"> temp *= prime[j];</span><br><span class="line"> }</span><br><span class="line"> <span class="built_in">cout</span> << prime[j] <<<span class="string">' '</span><< cnt << <span class="built_in">endl</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">例如 :5 4 3 2 1</span></span><br><span class="line"><span class="comment">先将素数2的出掉 cnt += n /temp ==> cnt += 5 / 2 , 将2出掉,temp *= prime[j] ==> temp *= 2 </span></span><br><span class="line"><span class="comment">cnt = 2, temp = 2 * 2 = 4;</span></span><br><span class="line"><span class="comment">下一次将4筛掉 cnt += n / temp ==> 5 / 4 则cnt = 2 + 1 = 3, temp *= 2 >>> 4 * 2 = 8 > 5 </span></span><br><span class="line"><span class="comment">此时将2出尽</span></span><br><span class="line"><span class="comment">然后从下一个素数开始3</span></span><br><span class="line"><span class="comment">.....</span></span><br><span class="line"><span class="comment">...</span></span><br><span class="line"><span class="comment">..</span></span><br><span class="line"><span class="comment">.</span></span><br><span class="line"><span class="comment">*/</span></span><br></pre></td></tr></table></figure>]]></content>
<summary type="html">
<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" cla
</summary>
</entry>
<entry>
<title>Mynotes/acwing基础/5.动态规划/1.动态规划(背包)</title>
<link href="http://yoursite.com/2020/06/02/Mynotes/acwing%E5%9F%BA%E7%A1%80/5.%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%92/1.%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%92%EF%BC%88%E8%83%8C%E5%8C%85%EF%BC%89/"/>
<id>http://yoursite.com/2020/06/02/Mynotes/acwing%E5%9F%BA%E7%A1%80/5.%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%92/1.%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%92%EF%BC%88%E8%83%8C%E5%8C%85%EF%BC%89/</id>
<published>2020-06-02T06:27:31.321Z</published>
<updated>2020-06-02T06:27:31.321Z</updated>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><h2 id="动态规划"><a href="#动态规划" class="headerlink" title="动态规划"></a>动态规划</h2><p> dp 优化(代码计算方程的变换)</p><p> </p><p> 所有选法 </p><p> 集合: 1.只从前i个中选取</p><p> 状态表示 f(i, j) : 条件 : </p><p> 2.总体积 <= j</p><p> 属性 : max ,min ,数量</p><p>dp:</p><p> 状态计算: ————集合的划分 </p><p> </p><h2 id="1-0-1-背包问题"><a href="#1-0-1-背包问题" class="headerlink" title="1 .0 1 背包问题"></a>1 .0 1 背包问题</h2><p>每个物品只能用一次</p><p>例如 N件物品, V 体积</p><p>输入 :</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">4 5 // N V</span><br><span class="line">1 2 // 体积 价值</span><br><span class="line">2 4</span><br><span class="line">3 4</span><br><span class="line">4 7</span><br></pre></td></tr></table></figure><p>输出:</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">8</span><br></pre></td></tr></table></figure><p> 集合 f(i, j) 化为两大类, 不含i和含i, 划分时不重不漏</p><p>不含i:只从i- 1中选, 总体积不大于j</p><p> 左边最大值f(i - 1, j);</p><p>含i :从1 ~i中选, 不超过j, 含i</p><p> 右边最大值 : 先把第i去掉, 1 ~ i- 1 中选, j - Vi 最大值 f(i - 1, j - Vi) + Wi;</p><p>f(i, j) = max(f[i - 1, j], f(i - 1, j - Vi) + Wi);</p><p>朴素代码:</p><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><iostream></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="keyword">int</span> n, m;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> N = <span class="number">1010</span>;</span><br><span class="line"><span class="keyword">int</span> v[N], w[N];</span><br><span class="line"><span class="keyword">int</span> f[N][N];</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="built_in">cin</span> >> n >> m;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">1</span>; i <= n; i++) {</span><br><span class="line"> <span class="built_in">cin</span> >> v[i] >> w[i];</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">1</span>; i <= n; i++) {</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> j = <span class="number">0</span>; j <= m; j++) {</span><br><span class="line"> f[i][j] = f[i - <span class="number">1</span>][j]; <span class="comment">// f[i - 1][j]一定存在 含i时可能不存在; </span></span><br><span class="line"> <span class="keyword">if</span>(j >= v[i]) f[i][j] = max(f[i][j] , f[i - <span class="number">1</span>][j - v[i]] + w[i]);</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="built_in">cout</span> << f[n][m] << <span class="built_in">endl</span>;</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p>优化:</p><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><iostream></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> N = <span class="number">1010</span>;</span><br><span class="line"><span class="keyword">int</span> n, m;</span><br><span class="line"><span class="keyword">int</span> v[N], w[N];</span><br><span class="line"><span class="keyword">int</span> f[N];</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>{</span><br><span class="line"> <span class="built_in">cin</span> >> n >> m;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">1</span>; i <= n; i++) <span class="built_in">cin</span> >> v[i] >> w[i];</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">1</span>; i <= n ;i++) {</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> j = m; j >= v[i]; j--) {</span><br><span class="line"> f[j] = max(f[j] , f[j - v[i]] + w[i]);</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="built_in">cout</span> << f[m] << <span class="built_in">endl</span>;</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h2 id="2-完全背包"><a href="#2-完全背包" class="headerlink" title="2 .完全背包"></a>2 .完全背包</h2><p>每件物品无限个</p><p>与0 1背包的区别在于时状态计算中</p><p>集合的划分 :</p><p>f[i ,j] </p><p>可以划分很多份 0, 1, 2, 3 …. k - 1, .. k 物品的个数</p><p>f[i - 1, j], </p><p>曲线救国: </p><ol><li><p>去掉k个物品i</p><pre><code>2. 求max, f[i - 1, j - k * v[i]]; 3. 再加回来k个物品i</code></pre></li></ol><p> f[i - 1, j - k * v[i]] + k * w[i];</p><p> f[i, j] = f[i - 1, j - v[i] * k] + w[i] * k</p><h2 id="3-多重背包"><a href="#3-多重背包" class="headerlink" title="3 .多重背包"></a>3 .多重背包</h2><p>每个物品有限制</p><p>每个物品可能有很多个</p><p><strong>未优化:</strong> </p><p><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1gfd321vsryj30ov08ewgi.jpg" alt="image.png"></p><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iostream></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstdio></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cmath></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstring></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iomanip></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><algorithm></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><map></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><vector></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> N = <span class="number">110</span>;</span><br><span class="line"><span class="keyword">int</span> n, m;</span><br><span class="line"><span class="keyword">int</span> f[N];</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="built_in">cin</span> >> n >> m;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i < n; i++) {<span class="comment">//类似01背包</span></span><br><span class="line"> <span class="keyword">int</span> v, w, s;<span class="comment">//v, w, s分别表示第 i 种物品的体积、价值和数量</span></span><br><span class="line"> <span class="built_in">cin</span> >> v >> w >> s;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> j = m; j >= <span class="number">0</span>; j--){</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> k = <span class="number">1</span>; k <= s && k * v <= j; k++) {</span><br><span class="line"> f[j] = max(f[j], f[j - k * v] + k * w);</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="built_in">cout</span> << f[m] << <span class="built_in">endl</span>;</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p><strong>使用二进制优化:</strong></p><p>例如将其变为01背包问题,7=1+1+1+1+1+1+1,7(2)=111</p><p>很明显7的二进制数位比较少,则可以转化为二进制进行计算优化</p><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iostream></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstdio></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cmath></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstring></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iomanip></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><algorithm></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><map></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><vector></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> N = <span class="number">2010</span>;</span><br><span class="line"><span class="keyword">int</span> n, m;</span><br><span class="line"><span class="keyword">int</span> f[N];</span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Good</span>{</span></span><br><span class="line"> <span class="keyword">int</span> v, w;</span><br><span class="line">};</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="built_in">cin</span> >> n >> m;</span><br><span class="line"> <span class="built_in">vector</span><Good> goods;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i < n; i++) {<span class="comment">//优化</span></span><br><span class="line"> <span class="keyword">int</span> v, w, s;</span><br><span class="line"> <span class="built_in">cin</span> >> v >> w >> s;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> k = <span class="number">1</span>; k <= s; k *= <span class="number">2</span>) {</span><br><span class="line"> s -= k;</span><br><span class="line"> goods.push_back({v * k, w * k});</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span>(s > <span class="number">0</span>) goods.push_back({v * s, w * s});</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">auto</span> good : goods) {<span class="comment">//0 1 背包</span></span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> j = m; j >= good.v; j--) {</span><br><span class="line"> f[j] = max(f[j], f[j - good.v] + good.w);</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="built_in">cout</span> << f[m] << <span class="built_in">endl</span>;</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h2 id="4-分组背包"><a href="#4-分组背包" class="headerlink" title="4 .分组背包"></a>4 .分组背包</h2><p>每一组里只能选一个物品 , 有 <code>N</code> 组物品和一个容量是 <code>V</code> 的背包。</p><p>每组物品有若干个,同一组内的物品最多只能选一个。<br>每件物品的体积是 v<sub>ij</sub>,价值是 w<sub>ij</sub>,其中 <code>i</code> 是组号,<code>j</code> 是组内编号。求解将哪些物品装入背包,可使物品总体积不超过背包容量,且总价值最大。</p><p>输出最大价值。</p><ul><li>状态表示<ul><li>集合 <code>只从前i组中选总体积不大于j的所有选法</code></li><li>属性 <code>max</code></li></ul></li><li>状态计算<ul><li>集合划分</li></ul></li></ul><p><img src="http://ww1.sinaimg.cn/large/006Uqzbtly1gfd4oajs76j314c0k6n6f.jpg" alt="image.png"></p><p><code>f[i - 1][j]</code>第i组一个都不选</p><p><code>f[i - 1, j - v[i, k]] + w[i, k]</code>第i组选第k个物品</p><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iostream></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstdio></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cmath></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstring></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iomanip></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><algorithm></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><map></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><vector></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> N = <span class="number">110</span>;</span><br><span class="line"><span class="keyword">int</span> n, m;</span><br><span class="line"><span class="keyword">int</span> v[N][N], w[N][N], s[N];</span><br><span class="line"><span class="keyword">int</span> f[N];</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="built_in">cin</span> >> n >> m;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">1</span>; i <= n; i++) {</span><br><span class="line"> <span class="built_in">cin</span> >> s[i];</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> j = <span class="number">0</span>; j < s[i]; j++) {</span><br><span class="line"> <span class="built_in">cin</span> >> v[i][j] >> w[i][j];</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">1</span>; i <= n; i++) {</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> j = m; j >= <span class="number">0</span>; j--) {</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> k = <span class="number">0</span>; k < s[i]; k++) {</span><br><span class="line"> <span class="keyword">if</span>(v[i][k] <= j) {</span><br><span class="line"> f[j] = max(f[j], f[j - v[i][k]] + w[i][k]);</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="built_in">cout</span> << f[m] << <span class="built_in">endl</span>;</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>]]></content>
<summary type="html">
<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" cla
</summary>
</entry>
<entry>
<title>Mynotes/acwing基础/4.Trie树/(Trie树)最大异或对</title>
<link href="http://yoursite.com/2020/06/02/Mynotes/acwing%E5%9F%BA%E7%A1%80/4.Trie%E6%A0%91/(Trie%E6%A0%91)%E6%9C%80%E5%A4%A7%E5%BC%82%E6%88%96%E5%AF%B9/"/>
<id>http://yoursite.com/2020/06/02/Mynotes/acwing%E5%9F%BA%E7%A1%80/4.Trie%E6%A0%91/(Trie%E6%A0%91)%E6%9C%80%E5%A4%A7%E5%BC%82%E6%88%96%E5%AF%B9/</id>
<published>2020-06-02T06:27:31.320Z</published>
<updated>2020-06-02T06:27:31.320Z</updated>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><p>题目:</p><p>在给定的N个整数A1,A2……ANA1,A2……AN中选出两个进行xor(异或)运算,得到的结果最大是多少?</p><h4 id="输入格式"><a href="#输入格式" class="headerlink" title="输入格式"></a>输入格式</h4><p>第一行输入一个整数N。</p><p>第二行输入N个整数A1A1~ANAN。</p><h4 id="输出格式"><a href="#输出格式" class="headerlink" title="输出格式"></a>输出格式</h4><p>输出一个整数表示答案。</p><h4 id="数据范围"><a href="#数据范围" class="headerlink" title="数据范围"></a>数据范围</h4><p>1≤N≤105 1≤N≤105,<br>0≤Ai<2310≤Ai<231</p><h4 id="输入样例:"><a href="#输入样例:" class="headerlink" title="输入样例:"></a>输入样例:</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">3</span><br><span class="line">1 2 3</span><br></pre></td></tr></table></figure><h4 id="输出样例:"><a href="#输出样例:" class="headerlink" title="输出样例:"></a>输出样例:</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">3</span><br></pre></td></tr></table></figure><p>1.暴力</p><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">int</span> res;</span><br><span class="line"><span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i < n; i++) </span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> j = <span class="number">0</span>; j < i; j++) {</span><br><span class="line"> res = max(a[i] ^ a[j]);</span><br><span class="line"> }</span><br></pre></td></tr></table></figure><p>2.改进(trie树优化第二层)</p><p><img src="/home/fangsong/%E6%96%87%E6%A1%A3/%E5%9B%BE%E7%89%87/trie.png" alt=""></p><p>查询的时候从根节点开始,有分支尽量与不同节点的分支走,没有直接走, 走到叶子尽头时候此时是最大的。</p><p>只要走31位</p><p>100000 * 31 = 3100000则优化很快, n * log(n) 级别</p><p>一个节点代表一个集合,也就是二进制这一位是0或1的值</p><p>例如:</p><p>5 6 3 4 </p><p>101 110 011 100</p><p><font color = red>主要模拟思路先查找后插入-></font></p><p><img src="/home/fangsong/%E6%96%87%E6%A1%A3/%E5%9B%BE%E7%89%87/trie2.png" alt=""></p><p>代码:(trie树改进)</p><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iostream></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><algorithm></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> N = <span class="number">100010</span>, M = <span class="number">31</span> * N;</span><br><span class="line"><span class="keyword">int</span> n;</span><br><span class="line"><span class="keyword">int</span> a[N];</span><br><span class="line"><span class="keyword">int</span> son[M][<span class="number">2</span>], idx;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">insert</span><span class="params">(<span class="keyword">int</span> x)</span> </span>{</span><br><span class="line"> <span class="keyword">int</span> p = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">30</span>; i >= <span class="number">0</span>; i--) {</span><br><span class="line"> <span class="keyword">int</span> u = x >> i & <span class="number">1</span>;</span><br><span class="line"> <span class="keyword">if</span>(!son[p][u])son[p][u] = ++ idx;</span><br><span class="line"> p = son[p][u];</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">query</span><span class="params">(<span class="keyword">int</span> x)</span> </span>{</span><br><span class="line"> <span class="keyword">int</span> p = <span class="number">0</span>, res = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">30</span>; i >= <span class="number">0</span>; i --) {</span><br><span class="line"> <span class="keyword">int</span> u = x >> i & <span class="number">1</span>;</span><br><span class="line"> <span class="keyword">if</span>(son[p][!u]) { <span class="comment">// 如果另外一个方向存在,就走另外一个方向</span></span><br><span class="line"> p = son[p][!u];</span><br><span class="line"> res = res * <span class="number">2</span> + !u; <span class="comment">// res * 2 + !u= res << 1 + !u </span></span><br><span class="line"> } <span class="keyword">else</span> { <span class="comment">// 如果另外一个方向不存在,就直接走</span></span><br><span class="line"> p = son[p][u]; </span><br><span class="line"> res = res * <span class="number">2</span> + u;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> res;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="built_in">cin</span> >> n;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i < n; i++) <span class="built_in">cin</span> >> a[i];</span><br><span class="line"> <span class="keyword">int</span> res = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i < n; i++) {</span><br><span class="line"> insert(a[i]);</span><br><span class="line"> <span class="keyword">int</span> t = query(a[i]);</span><br><span class="line"> res = max(res, a[i] ^ t);</span><br><span class="line"> }</span><br><span class="line"> <span class="built_in">cout</span> << res; </span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>]]></content>
<summary type="html">
<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" cla
</summary>
</entry>
<entry>
<title>Mynotes/acwing基础/4.Trie树/(Trie树)</title>
<link href="http://yoursite.com/2020/06/02/Mynotes/acwing%E5%9F%BA%E7%A1%80/4.Trie%E6%A0%91/(Trie%E6%A0%91)/"/>
<id>http://yoursite.com/2020/06/02/Mynotes/acwing%E5%9F%BA%E7%A1%80/4.Trie%E6%A0%91/(Trie%E6%A0%91)/</id>
<published>2020-06-02T06:27:31.317Z</published>
<updated>2020-06-02T06:27:31.318Z</updated>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><h2 id="Trie-树"><a href="#Trie-树" class="headerlink" title="Trie 树"></a>Trie 树</h2><p>高效的存储和查找字符串的集合的数据结构</p><p>例如如果要是存储:</p><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">abcdef</span><br><span class="line">abdef</span><br><span class="line">aced</span><br><span class="line">bcdf</span><br><span class="line">bcdf</span><br><span class="line">bcff</span><br><span class="line">cdaa</span><br><span class="line">bcdc</span><br></pre></td></tr></table></figure><img src="./图片/trie树.png" style="zoom:50%;" /><p>先做个标记 <code>f , f, d, f, c, f, a,</code></p><p>插入:</p><p>如果要是插入一个<code>abc</code>, 就是以’c’结尾的标记</p><img src="/home/fangsong/文档/图片/trie树1.png" style="zoom:50%;" /><p>查找:(快速查找)</p><p>例如我们想要查询一个存在的字符串<code>aced</code>,从根节点开始查询,aced, d正好未标记的一个点,所以查找到,存在。若查找不存在的<code>acbf</code> , acb ,f不存在,未找到。</p><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iostream></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstdio></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cmath></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstring></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iomanip></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><algorithm></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><map></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><vector></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> N = <span class="number">1000010</span>;</span><br><span class="line"><span class="keyword">char</span> str[N];</span><br><span class="line"><span class="keyword">int</span> son[N][<span class="number">26</span>], cnt[N], idx; <span class="comment">// 下表是0的点也是根节点,空节点, son[N][26]:因为一个节点最多有26个子节点,cnt是以当前这个点结尾的单词有多少个, idx当前用到的是那个下表, 如果一个带你没有子节点, 也让他指向0</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">insert</span><span class="params">(<span class="keyword">char</span> str[])</span> </span>{ <span class="comment">// 插入操作, 插入一个字符串</span></span><br><span class="line"> <span class="keyword">int</span> p = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>;str[i]; i++) { <span class="comment">// p = 0, 从根节点开始, 遍历这个个字符串</span></span><br><span class="line"> <span class="keyword">int</span> u = str[i] - <span class="string">'a'</span>; <span class="comment">// 将小写字母a ~z 映射成0 ~ 25;</span></span><br><span class="line"> <span class="keyword">if</span>(!son[p][u]) son[p][u] = ++idx; <span class="comment">// 如果不存在就创建出来</span></span><br><span class="line"> p = son[p][u];</span><br><span class="line"> } </span><br><span class="line"> cnt[p]++; <span class="comment">// 就是说以这个结尾的单词多了一个</span></span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">query</span><span class="params">(<span class="keyword">char</span> str[])</span> </span>{ <span class="comment">// 查询操作</span></span><br><span class="line"> <span class="keyword">int</span> p = <span class="number">0</span>; </span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; str[i]; i++) {</span><br><span class="line"> <span class="keyword">int</span> u = str[i] - <span class="string">'a'</span>;</span><br><span class="line"> <span class="keyword">if</span>(!son[p][u]) <span class="keyword">return</span> <span class="number">0</span>; <span class="comment">// 如不存在的话结束</span></span><br><span class="line"> p = son[p][u]; <span class="comment">// 继续走</span></span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> cnt[p]; <span class="comment">// 返回以p结尾的个数</span></span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">int</span> n;</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d"</span>, &n);</span><br><span class="line"> <span class="keyword">while</span>(n --) {</span><br><span class="line"> <span class="keyword">char</span> op[<span class="number">2</span>];</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%s%s"</span>, op, str);</span><br><span class="line"> <span class="keyword">if</span>(op[<span class="number">0</span>] == <span class="string">'I'</span>)insert(str);</span><br><span class="line"> <span class="keyword">else</span> <span class="built_in">printf</span>(<span class="string">"%d\n"</span>, query(str));</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>]]></content>
<summary type="html">
<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" cla
</summary>
</entry>
<entry>
<title>Mynotes/acwing基础/3.堆/(堆)(手写堆)</title>
<link href="http://yoursite.com/2020/06/02/Mynotes/acwing%E5%9F%BA%E7%A1%80/3.%E5%A0%86/(%E5%A0%86)%EF%BC%88%E6%89%8B%E5%86%99%E5%A0%86%EF%BC%89/"/>
<id>http://yoursite.com/2020/06/02/Mynotes/acwing%E5%9F%BA%E7%A1%80/3.%E5%A0%86/(%E5%A0%86)%EF%BC%88%E6%89%8B%E5%86%99%E5%A0%86%EF%BC%89/</id>
<published>2020-06-02T06:27:31.315Z</published>
<updated>2020-06-02T06:27:31.317Z</updated>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><p>如何手写一个堆</p><p>1.插入一个数</p><p>2.求集合中最小值</p><p>3.删除最小值</p><p>4.删除任意一个元素</p><p>5.修改任意一个元素</p><p><font color=red>完全二叉树</font></p><p>性质:小跟堆:(小顶堆)根小于等于左右</p><p><img src="/home/fangsong/%E6%96%87%E6%A1%A3/%E5%9B%BE%E7%89%87/%E5%A0%86%EF%BC%91.png" alt=""></p><p>存储:全新的存储方式</p><p><img src="/home/fangsong/%E6%96%87%E6%A1%A3/%E5%9B%BE%E7%89%87/%E5%A0%86%EF%BC%92.png" alt=""></p><p>操作:</p><p>1.down()节点往下移动</p><p>与子节点比较</p><p>2.up()节点往上操作</p><p>与父节点比较往上移动</p><p><font color=red>下表从1开始</font></p><p>down + up</p><p>1.插入一个数 heap[++size] = x; up(size)</p><p>2.求集合中最小值 heap[1];</p><p>3.删除最小值 heap[1] = heap[size]; size–;down(1);</p><p>4.删除任意一个元素 heap[k] = heap[size]; size–;down(k);up(k);</p><p>5.修改任意一个元素 heap[k] = x; down(k); up(k);</p><p>为什么初建堆从n/ 2开始:</p><p><img src="/home/fangsong/%E6%96%87%E6%A1%A3/%E5%9B%BE%E7%89%87/%E5%A0%86%EF%BC%93.png" alt=""></p><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iostream></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><algorithm></span></span></span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> N = <span class="number">100010</span>;</span><br><span class="line"><span class="keyword">int</span> n, m;</span><br><span class="line"><span class="keyword">int</span> p[N], s;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">down</span><span class="params">(<span class="keyword">int</span> u)</span> </span>{</span><br><span class="line"> <span class="keyword">int</span> t = u;</span><br><span class="line"> <span class="keyword">if</span>(u * <span class="number">2</span> <= s && p[u * <span class="number">2</span>] < p[t])t = u * <span class="number">2</span>;</span><br><span class="line"> <span class="keyword">if</span>(u * <span class="number">2</span> + <span class="number">1</span> <= s && p[u * <span class="number">2</span> + <span class="number">1</span>] < p[t]) t = u * <span class="number">2</span> + <span class="number">1</span>;</span><br><span class="line"> <span class="keyword">if</span>(u != t) {</span><br><span class="line"> swap(p[u], p[t]);</span><br><span class="line"> down(t);</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">up</span><span class="params">(<span class="keyword">int</span> u)</span> </span>{</span><br><span class="line"> <span class="keyword">while</span>(u / <span class="number">2</span> && h[u / <span class="number">2</span>] > h[u]) {</span><br><span class="line"> swap(h[u / <span class="number">2</span>], h[u]);</span><br><span class="line"> u /= <span class="number">2</span>;</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d%d"</span>, &n, &m);</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">1</span>; i <= n;i++) <span class="built_in">scanf</span>(<span class="string">"%d"</span>, &p[i]);</span><br><span class="line"> s = n;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = n / <span class="number">2</span>; i ; i --) down(i);</span><br><span class="line"> <span class="keyword">while</span>(m --) {</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d "</span>, p[<span class="number">1</span>]);</span><br><span class="line"> p[<span class="number">1</span>] = p[s];</span><br><span class="line"> s --;</span><br><span class="line"> down(<span class="number">1</span>);</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>]]></content>
<summary type="html">
<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" cla
</summary>
</entry>
<entry>
<title>Mynotes/acwing基础/2.并查集/(并查集)连通块中点的数量</title>
<link href="http://yoursite.com/2020/06/02/Mynotes/acwing%E5%9F%BA%E7%A1%80/2.%E5%B9%B6%E6%9F%A5%E9%9B%86/%EF%BC%88%E5%B9%B6%E6%9F%A5%E9%9B%86%EF%BC%89%E8%BF%9E%E9%80%9A%E5%9D%97%E4%B8%AD%E7%82%B9%E7%9A%84%E6%95%B0%E9%87%8F/"/>
<id>http://yoursite.com/2020/06/02/Mynotes/acwing%E5%9F%BA%E7%A1%80/2.%E5%B9%B6%E6%9F%A5%E9%9B%86/%EF%BC%88%E5%B9%B6%E6%9F%A5%E9%9B%86%EF%BC%89%E8%BF%9E%E9%80%9A%E5%9D%97%E4%B8%AD%E7%82%B9%E7%9A%84%E6%95%B0%E9%87%8F/</id>
<published>2020-06-02T06:27:31.304Z</published>
<updated>2020-06-02T06:27:31.304Z</updated>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><p>给定一个包含n个点(编号为1~n)的无向图,初始时图中没有边。</p><p>现在要进行m个操作,操作共有三种:</p><ol><li>“C a b”,在点a和点b之间连一条边,a和b可能相等;</li><li>“Q1 a b”,询问点a和点b是否在同一个连通块中,a和b可能相等;</li><li>“Q2 a”,询问点a所在连通块中点的数量;</li></ol><h4 id="输入格式"><a href="#输入格式" class="headerlink" title="输入格式"></a>输入格式</h4><p>第一行输入整数n和m。</p><p>接下来m行,每行包含一个操作指令,指令为“C a b”,“Q1 a b”或“Q2 a”中的一种。</p><h4 id="输出格式"><a href="#输出格式" class="headerlink" title="输出格式"></a>输出格式</h4><p>对于每个询问指令”Q1 a b”,如果a和b在同一个连通块中,则输出“Yes”,否则输出“No”。</p><p>对于每个询问指令“Q2 a”,输出一个整数表示点a所在连通块中点的数量</p><p>每个结果占一行。</p><h4 id="数据范围"><a href="#数据范围" class="headerlink" title="数据范围"></a>数据范围</h4><p>1≤n,m≤1051≤n,m≤105</p><h4 id="输入样例:"><a href="#输入样例:" class="headerlink" title="输入样例:"></a>输入样例:</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">5 5</span><br><span class="line">C 1 2</span><br><span class="line">Q1 1 2</span><br><span class="line">Q2 1</span><br><span class="line">C 2 5</span><br><span class="line">Q2 5</span><br></pre></td></tr></table></figure><h4 id="输出样例:"><a href="#输出样例:" class="headerlink" title="输出样例:"></a>输出样例:</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Yes</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td></tr></table></figure><p><img src="https://github.com/fangsong0517/Mynotes/blob/master/%E5%9B%BE%E7%89%87/2020-02-05%2021-32-11%E5%B1%8F%E5%B9%95%E6%88%AA%E5%9B%BE.png" alt=""></p><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iostream></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> N = <span class="number">1e5</span> + <span class="number">5</span>;</span><br><span class="line"><span class="keyword">int</span> n, m;</span><br><span class="line"><span class="keyword">int</span> p[N], sum[N];</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">find</span><span class="params">(<span class="keyword">int</span> x)</span> </span>{</span><br><span class="line"> <span class="keyword">if</span>(p[x] != x) p[x] = find(p[x]);</span><br><span class="line"> <span class="keyword">return</span> p[x];</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d%d"</span>, &n, &m);</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">1</span>; i <= n; i++)p[i] = i, sum[i] = <span class="number">1</span>;</span><br><span class="line"> <span class="keyword">while</span>(m--) {</span><br><span class="line"> <span class="keyword">char</span> op[<span class="number">5</span>];</span><br><span class="line"> <span class="keyword">int</span> a, b;</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%s"</span>, op);</span><br><span class="line"> <span class="keyword">if</span>(op[<span class="number">0</span>] ==<span class="string">'C'</span>) {</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d%d"</span>, &a, &b);</span><br><span class="line"> <span class="keyword">if</span>(find(a) == find(b)) <span class="keyword">continue</span>;</span><br><span class="line"> sum[find(b)] += sum[find(a)];</span><br><span class="line"> p[find(a)] = find(b);</span><br><span class="line"> } <span class="keyword">else</span> <span class="keyword">if</span>(op[<span class="number">1</span>] ==<span class="string">'1'</span>) {</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d%d"</span>, &a, &b);</span><br><span class="line"> <span class="keyword">if</span>(find(a) == find(b))<span class="built_in">puts</span>(<span class="string">"Yes"</span>);</span><br><span class="line"> <span class="keyword">else</span> <span class="built_in">puts</span>(<span class="string">"No"</span>);</span><br><span class="line"> } <span class="keyword">else</span> {</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d"</span>, &a);</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d\n"</span>, sum[find(a)]);</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>]]></content>
<summary type="html">
<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" cla
</summary>
</entry>
<entry>
<title>Mynotes/acwing基础/2.并查集/(并查集)食物链</title>
<link href="http://yoursite.com/2020/06/02/Mynotes/acwing%E5%9F%BA%E7%A1%80/2.%E5%B9%B6%E6%9F%A5%E9%9B%86/(%E5%B9%B6%E6%9F%A5%E9%9B%86)%E9%A3%9F%E7%89%A9%E9%93%BE/"/>
<id>http://yoursite.com/2020/06/02/Mynotes/acwing%E5%9F%BA%E7%A1%80/2.%E5%B9%B6%E6%9F%A5%E9%9B%86/(%E5%B9%B6%E6%9F%A5%E9%9B%86)%E9%A3%9F%E7%89%A9%E9%93%BE/</id>
<published>2020-06-02T06:27:31.303Z</published>
<updated>2020-06-02T06:27:31.303Z</updated>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><p>动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形。</p><p>A吃B, B吃C,C吃A。</p><p>现有N个动物,以1-N编号。</p><p>每个动物都是A,B,C中的一种,但是我们并不知道它到底是哪一种。</p><p>有人用两种说法对这N个动物所构成的食物链关系进行描述:</p><p>第一种说法是”1 X Y”,表示X和Y是同类。</p><p>第二种说法是”2 X Y”,表示X吃Y。</p><p>此人对N个动物,用上述两种说法,一句接一句地说出K句话,这K句话有的是真的,有的是假的。</p><p>当一句话满足下列三条之一时,这句话就是假话,否则就是真话。</p><p>1) 当前的话与前面的某些真的话冲突,就是假话;<br>2) 当前的话中X或Y比N大,就是假话;<br>3) 当前的话表示X吃X,就是假话。</p><p>你的任务是根据给定的N和K句话,输出假话的总数。</p><h4 id="输入格式"><a href="#输入格式" class="headerlink" title="输入格式"></a>输入格式</h4><p>第一行是两个整数N和K,以一个空格分隔。</p><p>以下K行每行是三个正整数 D,X,Y,两数之间用一个空格隔开,其中D表示说法的种类。</p><p>若D=1,则表示X和Y是同类。</p><p>若D=2,则表示X吃Y。</p><h4 id="输出格式"><a href="#输出格式" class="headerlink" title="输出格式"></a>输出格式</h4><p>只有一个整数,表示假话的数目。</p><h4 id="数据范围"><a href="#数据范围" class="headerlink" title="数据范围"></a>数据范围</h4><p>1≤N≤500001≤N≤50000,<br>0≤K≤1000000≤K≤100000</p><h4 id="输入样例:"><a href="#输入样例:" class="headerlink" title="输入样例:"></a>输入样例:</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">100 7</span><br><span class="line">1 101 1 </span><br><span class="line">2 1 2</span><br><span class="line">2 2 3 </span><br><span class="line">2 3 3 </span><br><span class="line">1 1 3 </span><br><span class="line">2 3 1 </span><br><span class="line">1 5 5</span><br></pre></td></tr></table></figure><h4 id="输出样例:"><a href="#输出样例:" class="headerlink" title="输出样例:"></a>输出样例:</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">3</span><br></pre></td></tr></table></figure><p><img src="/home/fangsong/%E6%96%87%E6%A1%A3/%E5%9B%BE%E7%89%87/%E5%B9%B6%E6%9F%A5%E9%9B%86%EF%BC%91.png" alt=""></p><p><img src="/home/fangsong/%E6%96%87%E6%A1%A3/%E5%9B%BE%E7%89%87/%E5%B9%B6%E6%9F%A5%E9%9B%86%EF%BC%92.png" alt=""></p><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iostream></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstdio></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cmath></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstring></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iomanip></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><algorithm></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><map></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><vector></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> N = <span class="number">50010</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">int</span> n, m;</span><br><span class="line"><span class="keyword">int</span> p[N], d[N];</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">find</span><span class="params">(<span class="keyword">int</span> x)</span> </span>{</span><br><span class="line"> <span class="keyword">if</span>(p[x] != x) {</span><br><span class="line"> <span class="keyword">int</span> t = find(p[x]);</span><br><span class="line"> d[x] += d[p[x]];</span><br><span class="line"> p[x] = t;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> p[x];</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d%d"</span>, &n, &m);</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">1</span>; i <= n; i++) p[i] = i;</span><br><span class="line"> <span class="keyword">int</span> res = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">while</span>(m --) {</span><br><span class="line"> <span class="keyword">int</span> t, x, y;</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d%d%d"</span>, &t, &x, &y);</span><br><span class="line"></span><br><span class="line"> <span class="keyword">if</span>(x > n|| y > n) res ++;</span><br><span class="line"> <span class="keyword">else</span> {</span><br><span class="line"> <span class="keyword">int</span> px = find(x), py = find(y); <span class="comment">// 找出两个根节点</span></span><br><span class="line"> <span class="keyword">if</span>(t == <span class="number">1</span>) { <span class="comment">// 同类时</span></span><br><span class="line"> <span class="keyword">if</span>(px == py && (d[x] - d[y]) % <span class="number">3</span>)res++; <span class="comment">// x, y到根节点的距离摸3是不同的,因为余数是哪一类它就是哪一类</span></span><br><span class="line"> <span class="keyword">else</span> <span class="keyword">if</span>(px != py) { <span class="comment">// x, y不在一个集合里</span></span><br><span class="line"> p[px] = py;</span><br><span class="line"> d[px] = d[y] - d[x];</span><br><span class="line"> }</span><br><span class="line"> } <span class="keyword">else</span> {</span><br><span class="line"> <span class="keyword">if</span>(p[x] == p[y]&&(d[x] - d[y] - <span class="number">1</span>)%<span class="number">3</span>) res++; <span class="comment">//x比y多1</span></span><br><span class="line"> <span class="keyword">else</span> <span class="keyword">if</span>(px != py){ <span class="comment">// x, y不在一个集合</span></span><br><span class="line"> p[px] = py;</span><br><span class="line"> d[px] = d[y] + <span class="number">1</span> - d[x];</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d\n"</span>, res);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p><img src="/home/fangsong/%E6%96%87%E6%A1%A3/%E5%9B%BE%E7%89%87/%E5%B9%B6%E6%9F%A5%E9%9B%86%EF%BC%93.png" alt=""></p>]]></content>
<summary type="html">
<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" cla
</summary>
</entry>
<entry>
<title>Mynotes/acwing基础/2.并查集/(并查集)合并集合</title>
<link href="http://yoursite.com/2020/06/02/Mynotes/acwing%E5%9F%BA%E7%A1%80/2.%E5%B9%B6%E6%9F%A5%E9%9B%86/(%E5%B9%B6%E6%9F%A5%E9%9B%86)%E5%90%88%E5%B9%B6%E9%9B%86%E5%90%88/"/>
<id>http://yoursite.com/2020/06/02/Mynotes/acwing%E5%9F%BA%E7%A1%80/2.%E5%B9%B6%E6%9F%A5%E9%9B%86/(%E5%B9%B6%E6%9F%A5%E9%9B%86)%E5%90%88%E5%B9%B6%E9%9B%86%E5%90%88/</id>
<published>2020-06-02T06:27:31.303Z</published>
<updated>2020-06-02T06:27:31.303Z</updated>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><p>并查集:</p><p>1.将两个集合合并</p><p>2.询问两个元素是否在一个集合中</p><p>基本原理:每个集合用一棵树来表示,树根的编号就是整个集合的编号。每个节点存储它的父节点,p[x]表示x的父节点</p><p>问题1:如何判断树根:if(p[x] == x)</p><p>问题2:如何求x的集合编号:while(p[x]!= x) x = p[x];</p><p>问题3:如何合并两个集合:px 是x 的集合编号,py 是y的集合编号。p[x] = y; </p><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iostream></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstdio></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cmath></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstring></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iomanip></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><map></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><vector></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> N = <span class="number">100010</span>;</span><br><span class="line"><span class="keyword">int</span> p[N];</span><br><span class="line"><span class="keyword">int</span> n, m;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">find</span><span class="params">(<span class="keyword">int</span> x)</span> </span>{ <span class="comment">// 查找x的父节点并返回,p[x] == x 时</span></span><br><span class="line"> <span class="keyword">if</span>(p[x] != x) p[x] = find(p[x]); </span><br><span class="line"> <span class="keyword">return</span> p[x];</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="built_in">cin</span> >> n >> m;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">1</span>; i <= n; i++) p[i] = i;</span><br><span class="line"> <span class="keyword">int</span> a ,b;</span><br><span class="line"> <span class="keyword">char</span> op[<span class="number">2</span>];</span><br><span class="line"> <span class="keyword">while</span>(m --){</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%s%d%d"</span>, op, &a, &b);</span><br><span class="line"> <span class="keyword">if</span>(op[<span class="number">0</span>] == <span class="string">'M'</span>)p[find(a)] = find(b); <span class="comment">// 合并两个集合</span></span><br><span class="line"> <span class="keyword">else</span> {</span><br><span class="line"> <span class="keyword">if</span>(find(a) == find(b)) <span class="built_in">puts</span>(<span class="string">"Yes"</span>); <span class="comment">// 查询是否在同一个集合</span></span><br><span class="line"> <span class="keyword">else</span> <span class="built_in">puts</span>(<span class="string">"No"</span>);</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"><span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>]]></content>
<summary type="html">
<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" cla
</summary>
</entry>
<entry>
<title>Mynotes/acwing基础/1.KMP/KMP</title>
<link href="http://yoursite.com/2020/06/02/Mynotes/acwing%E5%9F%BA%E7%A1%80/1.KMP/KMP/"/>
<id>http://yoursite.com/2020/06/02/Mynotes/acwing%E5%9F%BA%E7%A1%80/1.KMP/KMP/</id>
<published>2020-06-02T06:27:31.302Z</published>
<updated>2020-06-02T06:27:31.303Z</updated>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><h2 id="KMP"><a href="#KMP" class="headerlink" title="KMP"></a>KMP</h2><ol><li>暴力算法怎么做?</li><li>如何去优化?</li></ol><p> </p><p>暴力:</p><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">s[N] = {hellow,world}</span><br><span class="line">p[N] = {world}</span><br><span class="line"> </span><br><span class="line"> h e l l o w , w o r l d</span><br><span class="line"> w o r l d</span><br><span class="line"> w o r l d</span><br><span class="line"> w o r l d</span><br><span class="line"> w o r l d</span><br><span class="line"> w o r l d</span><br><span class="line"> w o r l d</span><br><span class="line"> w o r l d</span><br></pre></td></tr></table></figure><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">s[N], p[N];</span><br><span class="line"><span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">1</span>; i <= n; i ++) {</span><br><span class="line"> <span class="keyword">bool</span> flag = <span class="literal">true</span>;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> j = <span class="number">1</span>; j <= m; j++) {</span><br><span class="line"> <span class="keyword">if</span>(s[i + j - <span class="number">1</span>] != p[j]) {</span><br><span class="line"> flag = <span class="literal">false</span>;</span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> } </span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iostream></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> N = <span class="number">10010</span>, M = <span class="number">100010</span>;</span><br><span class="line"><span class="keyword">char</span> s[M], p[N];</span><br><span class="line"><span class="keyword">int</span> ne[N];</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">int</span> n, m;</span><br><span class="line"> <span class="built_in">cin</span> >> n >> p + <span class="number">1</span> >> m >> s + <span class="number">1</span>;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">2</span>, j = <span class="number">0</span>; i <= n; i++) {</span><br><span class="line"> <span class="keyword">while</span>(j && p[i] != p[j + <span class="number">1</span>]) j = ne[j];</span><br><span class="line"> <span class="keyword">if</span>(p[i] == p[j + <span class="number">1</span>])j++;</span><br><span class="line"> ne[i] = j;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">1</span>, j = <span class="number">0</span>; i <= m; i++) {</span><br><span class="line"> <span class="keyword">while</span>(j && s[i] != p[j + <span class="number">1</span>]) j = ne[j];</span><br><span class="line"> <span class="keyword">if</span>(s[i] == p[j + <span class="number">1</span>])j ++;</span><br><span class="line"> <span class="keyword">if</span>(j == n) {</span><br><span class="line"> <span class="built_in">cout</span> << i - n << <span class="string">' '</span>;</span><br><span class="line"> j = ne[j];</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>]]></content>
<summary type="html">
<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" cla
</summary>
</entry>
<entry>
<title>Mynotes/README</title>
<link href="http://yoursite.com/2020/06/02/Mynotes/README/"/>
<id>http://yoursite.com/2020/06/02/Mynotes/README/</id>
<published>2020-06-02T06:27:31.286Z</published>
<updated>2020-06-02T06:27:31.287Z</updated>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><h1 id="Mynotes"><a href="#Mynotes" class="headerlink" title="Mynotes"></a>Mynotes</h1><p>笔记记录<br>平常学习时的笔记整理,包括数据结构,C语言,linux学习,系统编程,计算机网络等</p><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br><span class="line">233</span><br><span class="line">234</span><br><span class="line">235</span><br><span class="line">236</span><br><span class="line">237</span><br><span class="line">238</span><br><span class="line">239</span><br><span class="line">240</span><br><span class="line">241</span><br><span class="line">242</span><br><span class="line">243</span><br><span class="line">244</span><br><span class="line">245</span><br><span class="line">246</span><br><span class="line">247</span><br><span class="line">248</span><br><span class="line">249</span><br><span class="line">250</span><br><span class="line">251</span><br><span class="line">252</span><br><span class="line">253</span><br><span class="line">254</span><br><span class="line">255</span><br><span class="line">256</span><br><span class="line">257</span><br><span class="line">258</span><br><span class="line">259</span><br><span class="line">260</span><br><span class="line">261</span><br><span class="line">262</span><br><span class="line">263</span><br><span class="line">264</span><br><span class="line">265</span><br><span class="line">266</span><br><span class="line">267</span><br><span class="line">268</span><br><span class="line">269</span><br><span class="line">270</span><br><span class="line">271</span><br><span class="line">272</span><br><span class="line">273</span><br><span class="line">274</span><br><span class="line">275</span><br><span class="line">276</span><br><span class="line">277</span><br><span class="line">278</span><br><span class="line">279</span><br><span class="line">280</span><br><span class="line">281</span><br><span class="line">282</span><br><span class="line">283</span><br><span class="line">284</span><br><span class="line">285</span><br><span class="line">286</span><br><span class="line">287</span><br><span class="line">288</span><br><span class="line">289</span><br><span class="line">290</span><br><span class="line">291</span><br><span class="line">292</span><br><span class="line">293</span><br><span class="line">294</span><br><span class="line">295</span><br><span class="line">296</span><br><span class="line">297</span><br><span class="line">298</span><br><span class="line">299</span><br><span class="line">300</span><br><span class="line">301</span><br><span class="line">302</span><br><span class="line">303</span><br><span class="line">304</span><br><span class="line">305</span><br><span class="line">306</span><br><span class="line">307</span><br><span class="line">308</span><br><span class="line">309</span><br><span class="line">310</span><br><span class="line">311</span><br><span class="line">312</span><br><span class="line">313</span><br><span class="line">314</span><br><span class="line">315</span><br><span class="line">316</span><br><span class="line">317</span><br><span class="line">318</span><br><span class="line">319</span><br><span class="line">320</span><br><span class="line">321</span><br><span class="line">322</span><br><span class="line">323</span><br><span class="line">324</span><br><span class="line">325</span><br><span class="line">326</span><br><span class="line">327</span><br><span class="line">328</span><br><span class="line">329</span><br><span class="line">330</span><br><span class="line">331</span><br><span class="line">332</span><br><span class="line">333</span><br><span class="line">334</span><br><span class="line">335</span><br><span class="line">336</span><br><span class="line">337</span><br><span class="line">338</span><br><span class="line">339</span><br><span class="line">340</span><br><span class="line">341</span><br><span class="line">342</span><br><span class="line">343</span><br><span class="line">344</span><br><span class="line">345</span><br><span class="line">346</span><br><span class="line">347</span><br><span class="line">348</span><br><span class="line">349</span><br><span class="line">350</span><br><span class="line">351</span><br><span class="line">352</span><br><span class="line">353</span><br><span class="line">354</span><br><span class="line">355</span><br><span class="line">356</span><br><span class="line">357</span><br><span class="line">358</span><br><span class="line">359</span><br><span class="line">360</span><br><span class="line">361</span><br><span class="line">362</span><br></pre></td><td class="code"><pre><span class="line">./</span><br><span class="line">├── 操作系统与系统编程</span><br><span class="line">│ ├── 多进程</span><br><span class="line">│ │ ├── <span class="number">1.f</span>ork.c</span><br><span class="line">│ │ ├── <span class="number">2.f</span>ork.c</span><br><span class="line">│ │ └── exec.c</span><br><span class="line">│ ├── 多线程</span><br><span class="line">│ │ └── pthread.c</span><br><span class="line">│ ├── 理解select、poll和epoll区别</span><br><span class="line">│ │ ├── 理解select、poll和epoll区别.md</span><br><span class="line">│ │ └── README.md</span><br><span class="line">│ ├── 文件锁</span><br><span class="line">│ │ ├── <span class="number">1.</span>数字累加</span><br><span class="line">│ │ │ ├── add.c</span><br><span class="line">│ │ │ └── a.out</span><br><span class="line">│ │ └── 文件锁.md</span><br><span class="line">│ ├── 线程池</span><br><span class="line">│ │ ├── common</span><br><span class="line">│ │ │ ├── chatroom.h</span><br><span class="line">│ │ │ ├── color.h</span><br><span class="line">│ │ │ ├── common.c</span><br><span class="line">│ │ │ ├── common.h</span><br><span class="line">│ │ │ ├── head.h</span><br><span class="line">│ │ │ ├── tcp_client.c</span><br><span class="line">│ │ │ ├── tcp_client.h</span><br><span class="line">│ │ │ ├── tcp_server.c</span><br><span class="line">│ │ │ └── tcp_server.h</span><br><span class="line">│ │ ├── README.assets</span><br><span class="line">│ │ │ └── <span class="number">2020</span><span class="number">-04</span><span class="number">-08</span> <span class="number">15</span><span class="number">-34</span><span class="number">-46</span>屏幕截图.png</span><br><span class="line">│ │ ├── README.md</span><br><span class="line">│ │ └── server</span><br><span class="line">│ │ ├── a.out</span><br><span class="line">│ │ ├── G</span><br><span class="line">│ │ └── server.c</span><br><span class="line">│ ├── 消息队列</span><br><span class="line">│ │ ├── msg.c</span><br><span class="line">│ │ └── README.md</span><br><span class="line">│ ├── 信号量</span><br><span class="line">│ │ ├── a.out</span><br><span class="line">│ │ ├── G</span><br><span class="line">│ │ ├── README.md</span><br><span class="line">│ │ └── server.c</span><br><span class="line">│ ├── common</span><br><span class="line">│ │ ├── a.out</span><br><span class="line">│ │ ├── chatroom.h</span><br><span class="line">│ │ ├── color.h</span><br><span class="line">│ │ ├── common.c</span><br><span class="line">│ │ ├── common.h</span><br><span class="line">│ │ ├── head.h</span><br><span class="line">│ │ ├── tcp_client.c</span><br><span class="line">│ │ ├── tcp_client.h</span><br><span class="line">│ │ ├── tcp_server.c</span><br><span class="line">│ │ ├── tcp_server.h</span><br><span class="line">│ │ └── test.c</span><br><span class="line">│ ├── cp</span><br><span class="line">│ │ ├── 实现cp所用到的函数.md</span><br><span class="line">│ │ └── cp.c</span><br><span class="line">│ ├── IPC</span><br><span class="line">│ │ ├── <span class="number">1.</span>数字累加</span><br><span class="line">│ │ │ ├── add.c</span><br><span class="line">│ │ │ ├── a.out</span><br><span class="line">│ │ │ └── README.md</span><br><span class="line">│ │ ├── <span class="number">2.</span>SHM</span><br><span class="line">│ │ │ ├── a.out</span><br><span class="line">│ │ │ ├── README.md</span><br><span class="line">│ │ │ └── shm1.c</span><br><span class="line">│ │ ├── <span class="number">3.</span>非亲缘进程间的内存共享</span><br><span class="line">│ │ │ ├── client</span><br><span class="line">│ │ │ ├── client.c</span><br><span class="line">│ │ │ ├── README.md</span><br><span class="line">│ │ │ ├── server</span><br><span class="line">│ │ │ └── server.c</span><br><span class="line">│ │ ├── <span class="number">4.</span>共享内存+互斥锁</span><br><span class="line">│ │ │ └── add2.c</span><br><span class="line">│ │ └── <span class="number">5.</span>共享内存+互斥锁+条件变量</span><br><span class="line">│ │ ├── add.c</span><br><span class="line">│ │ ├── client</span><br><span class="line">│ │ ├── client.c</span><br><span class="line">│ │ ├── server</span><br><span class="line">│ │ └── server.c</span><br><span class="line">│ ├── ls</span><br><span class="line">│ │ ├── 实现ls需要用到的函数.md</span><br><span class="line">│ │ └── LS1.c</span><br><span class="line">│ ├── more</span><br><span class="line">│ │ └── More.c</span><br><span class="line">│ ├── PIPE</span><br><span class="line">│ │ ├── a.out</span><br><span class="line">│ │ ├── README.assets</span><br><span class="line">│ │ │ └── <span class="number">2020</span><span class="number">-04</span><span class="number">-08</span> <span class="number">16</span><span class="number">-12</span><span class="number">-38</span>屏幕截图.png</span><br><span class="line">│ │ ├── README.md</span><br><span class="line">│ │ └── shm2.c</span><br><span class="line">│ ├── popen</span><br><span class="line">│ │ ├── a.out</span><br><span class="line">│ │ ├── popentask.c</span><br><span class="line">│ │ └── README.md</span><br><span class="line">│ ├── server_epoll</span><br><span class="line">│ │ ├── a.out</span><br><span class="line">│ │ ├── epoll.c</span><br><span class="line">│ │ ├── G</span><br><span class="line">│ │ ├── README.assets</span><br><span class="line">│ │ └── README.md</span><br><span class="line">│ ├── server_poll</span><br><span class="line">│ │ ├── a.out</span><br><span class="line">│ │ ├── G</span><br><span class="line">│ │ └── server.c</span><br><span class="line">│ ├── server_select</span><br><span class="line">│ │ ├── a.out</span><br><span class="line">│ │ ├── G1</span><br><span class="line">│ │ ├── G2</span><br><span class="line">│ │ ├── README.assets</span><br><span class="line">│ │ ├── README.md</span><br><span class="line">│ │ ├── select1.c</span><br><span class="line">│ │ └── select.c</span><br><span class="line">│ └── socket</span><br><span class="line">│ ├── a.out</span><br><span class="line">│ ├── client.c</span><br><span class="line">│ ├── Linux Socket编程(不限Linux).assets</span><br><span class="line">│ ├── Linux Socket编程(不限Linux).md</span><br><span class="line">│ └── server.c</span><br><span class="line">├── 高级数据结构</span><br><span class="line">│ ├── <span class="number">1.</span>AVL.cpp</span><br><span class="line">│ ├── <span class="number">2.</span>RBT.cpp</span><br><span class="line">│ ├── <span class="number">3.</span>RBT.cpp</span><br><span class="line">│ ├── 红黑树.md</span><br><span class="line">│ ├── a.out</span><br><span class="line">│ ├── binary_search_tree.cpp</span><br><span class="line">│ └── README.md</span><br><span class="line">├── 计算机网络</span><br><span class="line">│ ├── 封装成帧&差错检测.md</span><br><span class="line">│ ├── 计算机网络概要.assets</span><br><span class="line">│ ├── 计算机网络概要.md</span><br><span class="line">│ ├── 计算机网络习题.md</span><br><span class="line">│ ├── 可靠传输的实现机制.md</span><br><span class="line">│ ├── 可靠传输概念.md</span><br><span class="line">│ ├── 流量控制.md</span><br><span class="line">│ ├── 数据链路层概述.md</span><br><span class="line">│ ├── 网络层协议.md</span><br><span class="line">│ ├── 网络基础.md</span><br><span class="line">│ ├── 应用层协议.md</span><br><span class="line">│ ├── 运输层协议.md</span><br><span class="line">│ ├── DNS协议.md</span><br><span class="line">│ ├── FTP协议.md</span><br><span class="line">│ ├── HTTP协议与HTTPS协议.md</span><br><span class="line">│ ├── IP地址、MAC地址、ARP地址.md</span><br><span class="line">│ ├── SMTP、POP3、IMAP协议.md</span><br><span class="line">│ ├── TCP的流量控制.md</span><br><span class="line">│ ├── TCP的拥塞控制.md</span><br><span class="line">│ ├── UDP和TCP的对比.md</span><br><span class="line">│ └── WEB与HTTP协议.md</span><br><span class="line">├── 数据结构</span><br><span class="line">│ ├── 数据结构.assets</span><br><span class="line">│ ├── 数据结构code</span><br><span class="line">│ │ ├── <span class="number">10.</span>heap_sort.cpp</span><br><span class="line">│ │ ├── <span class="number">11.</span>stable_sort.cpp</span><br><span class="line">│ │ ├── <span class="number">12.u</span>nstable_sort.cpp</span><br><span class="line">│ │ ├── <span class="number">13.b</span>inary_search.cpp</span><br><span class="line">│ │ ├── <span class="number">14.</span>hashtable.cpp</span><br><span class="line">│ │ ├── <span class="number">15.b</span>inary_search_tree.cpp</span><br><span class="line">│ │ ├── <span class="number">16.U</span>nionSet.cpp</span><br><span class="line">│ │ ├── <span class="number">17.U</span>nionSet<span class="number">-1.</span>cpp</span><br><span class="line">│ │ ├── <span class="number">18.U</span>nionSet<span class="number">-2.</span>cpp</span><br><span class="line">│ │ ├── <span class="number">1.</span>Vector.cpp</span><br><span class="line">│ │ ├── <span class="number">2.1l</span>ist.cpp</span><br><span class="line">│ │ ├── <span class="number">2.l</span>ist.cpp</span><br><span class="line">│ │ ├── <span class="number">3.</span><span class="built_in">queue</span>.cpp</span><br><span class="line">│ │ ├── <span class="number">4.</span><span class="built_in">stack</span>.cpp</span><br><span class="line">│ │ ├── <span class="number">5.b</span>inary_tree.cpp</span><br><span class="line">│ │ ├── <span class="number">6.</span>thread_tree.cpp</span><br><span class="line">│ │ ├── <span class="number">7.</span>table_to_tree.cpp</span><br><span class="line">│ │ ├── <span class="number">8.</span>expre_calc.cpp</span><br><span class="line">│ │ ├── <span class="number">9.</span>priority_queue.cpp</span><br><span class="line">│ │ ├── a.out</span><br><span class="line">│ │ ├── KMP.cpp</span><br><span class="line">│ │ └── <span class="built_in">unordered_map</span>.cpp</span><br><span class="line">│ └── 数据结构.md</span><br><span class="line">├── 算法</span><br><span class="line">│ ├── 欧拉计划.md</span><br><span class="line">│ └── 线性筛的变形.md</span><br><span class="line">├── 图片</span><br><span class="line">│ ├── 变量到底储.png</span><br><span class="line">│ ├── 变量及类型转换.png</span><br><span class="line">│ ├── 并查集1.png</span><br><span class="line">│ ├── 并查集2.png</span><br><span class="line">│ ├── 并查集3.png</span><br><span class="line">│ ├── 出栈.png</span><br><span class="line">│ ├── 堆1.png</span><br><span class="line">│ ├── 堆2.png</span><br><span class="line">│ ├── 堆3.png</span><br><span class="line">│ ├── 访问数组.png</span><br><span class="line">│ ├── 共用体内存.png</span><br><span class="line">│ ├── 宏-条件式编译.png</span><br><span class="line">│ ├── 滑动窗口.png</span><br><span class="line">│ ├── 基本数据类型2.png</span><br><span class="line">│ ├── 基本数据类型.jpg</span><br><span class="line">│ ├── 解决gitclone慢1.png</span><br><span class="line">│ ├── 解决gitclone慢.png</span><br><span class="line">│ ├── 扩展欧几里得过程.jpg</span><br><span class="line">│ ├── 欧几里得证明1.png</span><br><span class="line">│ ├── 欧几里得证明2.png</span><br><span class="line">│ ├── 入栈.png</span><br><span class="line">│ ├── 上传github.png</span><br><span class="line">│ ├── 推导平方和公式.png</span><br><span class="line">│ ├── 小端机.png</span><br><span class="line">│ ├── 循环队列.png</span><br><span class="line">│ ├── 预处理命令.png</span><br><span class="line">│ ├── 约束和.png</span><br><span class="line">│ ├── 指针.png</span><br><span class="line">│ ├── 字符串.png</span><br><span class="line">│ ├── 作用域规则<span class="number">2.</span>png</span><br><span class="line">│ ├── 作用域规则.png</span><br><span class="line">│ ├── build_thread.png</span><br><span class="line">│ ├── C语言预编译编辑汇编链接.png</span><br><span class="line">│ ├── EP09<span class="number">-1.</span>png</span><br><span class="line">│ ├── EP09<span class="number">-2.</span>png</span><br><span class="line">│ ├── EP09<span class="number">-3.</span>png</span><br><span class="line">│ ├── EP09<span class="number">-4.</span>png</span><br><span class="line">│ ├── EP09.png</span><br><span class="line">│ ├── EP12<span class="number">-1.</span>png</span><br><span class="line">│ ├── EP12<span class="number">-2.</span>png</span><br><span class="line">│ ├── EP12<span class="number">-3.</span>png</span><br><span class="line">│ ├── EP24.png</span><br><span class="line">│ ├── EP26.png</span><br><span class="line">│ ├── EP28.png</span><br><span class="line">│ ├── EP30<span class="number">-1.</span>png</span><br><span class="line">│ ├── EP30.png</span><br><span class="line">│ ├── g++ -E.png</span><br><span class="line">│ ├── <span class="built_in">sscanf</span>&<span class="built_in">sprintf</span>.png</span><br><span class="line">│ ├── trie2.png</span><br><span class="line">│ ├── trie树1.png</span><br><span class="line">│ ├── trie树.png</span><br><span class="line">│ ├── trie.png</span><br><span class="line">│ └── xiaoduanji.png</span><br><span class="line">├── acwing基础</span><br><span class="line">│ ├── (并查集)合并集合.md</span><br><span class="line">│ ├── (并查集)连通块中点的数量.md</span><br><span class="line">│ ├── (并查集)食物链.md</span><br><span class="line">│ ├── 动态规划(背包).md</span><br><span class="line">│ ├── (堆)(手写堆).md</span><br><span class="line">│ ├── a.out</span><br><span class="line">│ ├── KMP.cpp</span><br><span class="line">│ ├── KMP.md</span><br><span class="line">│ ├── (Trie树)最大异或对.md</span><br><span class="line">│ └── (Trie树).md</span><br><span class="line">├── C语言</span><br><span class="line">│ ├── c语言项目.md</span><br><span class="line">│ ├── C语言csdn博客总结.md</span><br><span class="line">│ └── C语言.md</span><br><span class="line">├── Hzoj_Code</span><br><span class="line">│ ├── <span class="number">227</span><span class="number">-64</span>位整数乘法.cpp</span><br><span class="line">│ ├── <span class="number">235</span>-递归实现指数型枚举.cpp</span><br><span class="line">│ ├── <span class="number">236</span>-递归实现组合型枚举.cpp</span><br><span class="line">│ ├── <span class="number">237</span>-递归实现排列型枚举.cpp</span><br><span class="line">│ ├── <span class="number">240</span>-图形打印四.cpp</span><br><span class="line">│ ├── <span class="number">241</span>-最近点对.cpp</span><br><span class="line">│ ├── <span class="number">242</span>-最大平均值.cpp</span><br><span class="line">│ ├── <span class="number">243</span>-秦腾与教学评估.cpp</span><br><span class="line">│ ├── <span class="number">245</span>-货仓选址.cpp</span><br><span class="line">│ ├── <span class="number">250</span>-糖果传递.cpp</span><br><span class="line">│ ├── <span class="number">251</span>-士兵.cpp</span><br><span class="line">│ ├── <span class="number">264</span>-最大矩形面积.cpp</span><br><span class="line">│ ├── <span class="number">271</span>-滑动窗口.cpp</span><br><span class="line">│ ├── <span class="number">303</span>-矩阵距离一.cpp</span><br><span class="line">│ ├── <span class="number">304</span>-骑士风度的牛.cpp</span><br><span class="line">│ ├── <span class="number">305</span>-乳草的入侵.cpp</span><br><span class="line">│ ├── <span class="number">325</span>-Parity_game.cpp</span><br><span class="line">│ ├── <span class="number">327</span>-关押罪犯.cpp</span><br><span class="line">│ ├── <span class="number">342</span>-照相的排列数.cpp</span><br><span class="line">│ ├── <span class="number">343</span>-最长公共上升子序列.cpp</span><br><span class="line">│ ├── <span class="number">362</span>-食物链.cpp</span><br><span class="line">│ ├── <span class="number">386</span>-吃瓜群众.cpp</span><br><span class="line">│ ├── <span class="number">387</span>-吃瓜群众<span class="number">2.</span>cpp</span><br><span class="line">│ ├── <span class="number">389</span>-暴躁的程序猿.cpp</span><br><span class="line">│ ├── <span class="number">390</span>-原木切割.cpp</span><br><span class="line">│ ├── <span class="number">391</span>-数列分段.cpp</span><br><span class="line">│ ├── <span class="number">392</span>-丢瓶盖.cpp</span><br><span class="line">│ ├── <span class="number">393</span>-切绳子.cpp</span><br><span class="line">│ ├── <span class="number">396</span>-填涂颜色.cpp</span><br><span class="line">│ ├── <span class="number">397</span>-僵尸来袭.cpp</span><br><span class="line">│ ├── <span class="number">398</span>-马的遍历.cpp</span><br><span class="line">│ ├── <span class="number">404</span><span class="number">-01</span>迷宫简易版.cpp</span><br><span class="line">│ ├── <span class="number">405</span><span class="number">-01</span>迷宫.cpp</span><br><span class="line">│ ├── <span class="number">406</span>-水坑数量.cpp</span><br><span class="line">│ ├── <span class="number">41</span><span class="number">-2</span>-墙壁涂色.cpp</span><br><span class="line">│ ├── <span class="number">41</span>-墙壁涂色.cpp</span><br><span class="line">│ ├── <span class="number">42</span>-钱币问题.cpp</span><br><span class="line">│ ├── <span class="number">43</span>-数字三角形.cpp</span><br><span class="line">│ ├── <span class="number">44</span>-最长上升子序列.cpp</span><br><span class="line">│ ├── <span class="number">45</span>-最长公共子序列.cpp</span><br><span class="line">│ ├── <span class="number">47</span><span class="number">-01</span>背包.cpp</span><br><span class="line">│ ├── <span class="number">48</span>-完全背包.cpp</span><br><span class="line">│ ├── <span class="number">490</span>-新约瑟夫游戏.cpp</span><br><span class="line">│ ├── <span class="number">49</span><span class="number">-1</span>-多重背包.cpp</span><br><span class="line">│ ├── <span class="number">49</span>-多重背包.cpp</span><br><span class="line">│ ├── <span class="number">503</span>-独木舟.cpp</span><br><span class="line">│ ├── <span class="number">504</span>-删数.cpp</span><br><span class="line">│ ├── <span class="number">505</span>-最大整数.cpp</span><br><span class="line">│ ├── <span class="number">506</span>-打热水.cpp</span><br><span class="line">│ ├── <span class="number">507</span>-大神排队.cpp</span><br><span class="line">│ ├── <span class="number">508</span>-两人过河.cpp</span><br><span class="line">│ ├── <span class="number">509</span>-智力大冲浪.cpp</span><br><span class="line">│ ├── <span class="number">50</span>-扔鸡蛋.cpp</span><br><span class="line">│ ├── <span class="number">51</span>-矩形.cpp</span><br><span class="line">│ ├── <span class="number">527</span>-飞跃原野.cpp</span><br><span class="line">│ ├── <span class="number">528</span>-关系网络.cpp</span><br><span class="line">│ ├── <span class="number">529</span>-龙与虫.cpp</span><br><span class="line">│ ├── <span class="number">52</span>-古老的打字机.cpp</span><br><span class="line">│ ├── <span class="number">530</span>-警察找车.cpp</span><br><span class="line">│ ├── <span class="number">531</span>-奇怪的电视.cpp</span><br><span class="line">│ ├── <span class="number">534</span>-体积.cpp</span><br><span class="line">│ ├── <span class="number">535</span>-瓷砖.cpp</span><br><span class="line">│ ├── <span class="number">536</span>-最大黑色区域.cpp</span><br><span class="line">│ ├── <span class="number">537</span>-门票信息.cpp</span><br><span class="line">│ ├── <span class="number">538</span>-图的遍历.cpp</span><br><span class="line">│ ├── <span class="number">540</span>-生日购物.cpp</span><br><span class="line">│ ├── <span class="number">541</span>-相遇问题.cpp</span><br><span class="line">│ ├── <span class="number">542</span>-奶酪.cpp</span><br><span class="line">│ ├── <span class="number">551</span>-最长不上升子序列.cpp</span><br><span class="line">│ ├── <span class="number">558</span>-采药.cpp</span><br><span class="line">│ ├── <span class="number">559</span>-小毛的交易.cpp</span><br><span class="line">│ ├── <span class="number">560</span>-开心的金明.cpp</span><br><span class="line">│ ├── <span class="number">561</span>-猪猪存钱罐.cpp</span><br><span class="line">│ ├── <span class="number">565</span>-位的交换.cpp</span><br><span class="line">│ ├── <span class="number">577</span>-讲话模式.cpp</span><br><span class="line">│ ├── <span class="number">587</span>-环.cpp</span><br><span class="line">│ ├── <span class="number">588</span>-数列.cpp</span><br><span class="line">│ ├── <span class="number">589</span>-看电影.cpp</span><br><span class="line">│ ├── <span class="number">590</span>-树塔狂想曲.cpp</span><br><span class="line">│ ├── <span class="number">591</span>-游戏.cpp</span><br><span class="line">│ ├── <span class="number">592</span>-六十四进制.cpp</span><br><span class="line">│ ├── <span class="number">593</span>-魔法项链.cpp</span><br><span class="line">│ ├── <span class="number">71</span>-朋友圈.cpp</span><br><span class="line">│ ├── <span class="number">72</span>-猜拳.cpp</span><br><span class="line">│ ├── <span class="number">80</span>-小明看电影.cpp</span><br><span class="line">│ ├── <span class="number">81</span>-小明回家.cpp</span><br><span class="line">│ ├── <span class="number">83</span>-N分.cpp</span><br><span class="line">│ ├── 递归与DFS.assets</span><br><span class="line">│ │ └── Whiteboard[<span class="number">2</span>]<span class="number">-01.</span>png</span><br><span class="line">│ ├── a.out</span><br><span class="line">│ ├── README.assets</span><br><span class="line">│ └── README.md</span><br><span class="line">├── linux学习笔记</span><br><span class="line">│ ├── 基本系统.md</span><br><span class="line">│ ├── 命令系统.md</span><br><span class="line">│ ├── 认识SHELL并使用相关文件.assets</span><br><span class="line">│ │ └── <span class="number">2020</span><span class="number">-03</span><span class="number">-12</span> <span class="number">12</span><span class="number">-46</span><span class="number">-48</span>屏幕截图.png</span><br><span class="line">│ ├── 认识SHELL并使用相关文件.md</span><br><span class="line">│ ├── 数据提取操作.md</span><br><span class="line">│ ├── 文件与目录操作、实现ls命令.md</span><br><span class="line">│ ├── 文件与目录操作.md</span><br><span class="line">│ ├── 系统信息的获取.md</span><br><span class="line">│ ├── linux3<span class="number">.14</span>笔记.assets</span><br><span class="line">│ ├── linux笔记.assets</span><br><span class="line">│ ├── linux笔记.md</span><br><span class="line">│ ├── linux服务器免密登陆.md</span><br><span class="line">│ ├── linux配置.md</span><br><span class="line">│ ├── linux书籍</span><br><span class="line">│ │ └── Linux+命令行和shell脚本编程宝典.pdf</span><br><span class="line">│ ├── SHELL基础.assets</span><br><span class="line">│ ├── SHELL基础.md</span><br><span class="line">│ ├── SOCKET编程基础.assets</span><br><span class="line">│ └── SOCKET编程基础.md</span><br><span class="line">└── README.md</span><br></pre></td></tr></table></figure>]]></content>
<summary type="html">
<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" cla
</summary>
</entry>
<entry>
<title>Mynotes/C语言/C语言</title>
<link href="http://yoursite.com/2020/06/02/Mynotes/C%E8%AF%AD%E8%A8%80/C%E8%AF%AD%E8%A8%80/"/>
<id>http://yoursite.com/2020/06/02/Mynotes/C%E8%AF%AD%E8%A8%80/C%E8%AF%AD%E8%A8%80/</id>
<published>2020-06-02T06:27:31.264Z</published>
<updated>2020-06-02T06:27:31.264Z</updated>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><h2 id="C语言"><a href="#C语言" class="headerlink" title="C语言"></a>C语言</h2><p>[TOC]</p><h4 id="第一节:语言入门"><a href="#第一节:语言入门" class="headerlink" title="第一节:语言入门"></a>第一节:语言入门</h4><p>1.<code>scanf</code>返回值时变量的个数</p><p>功 能: 执行格式化输入<br>用 法: <code>int scanf(char *format[,argument,...])</code>;</p><p><code>scanf()</code>函数返回成功赋值的数据项数,出错时则返回<code>EOF</code>。</p><p>格式字符 说明<br><code>%a</code> 读入一个浮点值(仅<code>C99</code>有效)<br><code>%A</code> 同上<br><code>%c</code> 读入一个字符<br><code>%d</code> 读入十进制整数<br><code>%i</code> 读入十进制,八进制,十六进制整数<br><code>%o</code> 读入八进制整数<br><code>%x</code> 读入十六进制整数<br><code>%X</code> 同上<br><code>%c</code> 读入一个字符<br><code>%s</code> 读入一个字符串<br><code>%f</code> 读入一个浮点数<br><code>%F</code> 同上<br><code>%e</code> 同上<br><code>%E</code> 同上<br><code>%g</code> 同上<br><code>%G</code> 同上<br><code>%p</code> 读入一个指针<br><code>%u</code> 读入一个无符号十进制整数<br><code>%n</code> 至此已读入值的等价字符数<br><code>%[]</code> 扫描字符集合<br><code>%%</code> 读%符号</p><p>2.<code>printf</code>返回值时变量的长度</p><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><stdio.h></span></span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">int</span> n;</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d\n"</span>,<span class="built_in">scanf</span>(<span class="string">"%d"</span>, &n));</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d\n"</span>, <span class="built_in">printf</span>(<span class="string">"%d"</span>, n));</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d"</span>, <span class="built_in">printf</span>(<span class="string">"hello"</span>));</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">456</span></span><br><span class="line"><span class="comment">1</span></span><br><span class="line"><span class="comment">4563</span></span><br><span class="line"><span class="comment">hello5</span></span><br><span class="line"><span class="comment">*/</span></span><br></pre></td></tr></table></figure><p>3.求带空格字段串中字符的个数</p><p><code>输入字符串中有空格时用scanf("%[^\n]s",str);</code></p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><stdio.h></span></span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">char</span> str[<span class="number">100</span>];</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%[^\n]s"</span>, str); <span class="comment">// 输入字符串中有空格时用scanf("%[^\n]s",str);</span></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d"</span>, <span class="built_in">printf</span>(<span class="string">"%s"</span>, str));</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p>4.<code>"%[^\n]"</code></p><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">while</span>((ret = <span class="built_in">scanf</span>(<span class="string">"%[^\n]s"</span>, str)) != EOF) {</span><br><span class="line"> getchar(); <span class="comment">// 吃掉/n, 如果没有的话,程序无法停止</span></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%s ret = %d\n"</span>, str, ret);</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p>4.<code>sprintf</code>就是把格式化的数据写入到某个字符串中。返回值字符串的长度。</p><p>5.<code>fprintf</code>是用于文件操作。</p><p> 原型:<code>int fprintf(FILE *stream,char *format,[argument]);</code> </p><p> 功能:<code>fprintf()</code>函数根据指定的<code>format</code>(格式)发送信息(参数)到由<code>stream</code>(流)指定的文件.因此<code>fprintf()</code>可以使得信息输出到指 定的文件。</p><p>6.<code>stdin</code>、<code>stdout</code>、<code>stderr</code>;</p><p><code>stdout</code>– 标准输出设备 <code>stdout</code>。<br><code>stderr</code>– 标准错误输出设备<br>两者默认向屏幕输出。<br>但如果用转向标准输出到磁盘文件,则可看出两者区别。<code>stdout</code>输出到磁盘文件,<code>stderr</code>在屏幕。 </p><p>在默认情况下,<code>stdout</code>是行缓冲的,他的输出会放在一个buffer里面,只有到换行的时候,才会输出到屏幕。而<code>stderr</code>是无缓冲的,会直接输出。</p><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><span class="line">fangsong@fangsong-FX503VD:~/Mycode/<span class="number">1.</span>c语言$ cat <span class="built_in">stderr</span></span><br><span class="line"><span class="built_in">stderr</span> = <span class="number">192.168</span><span class="number">.1</span><span class="number">.40</span></span><br><span class="line">fangsong@fangsong-FX503VD:~/Mycode/<span class="number">1.</span>c语言$ cat <span class="built_in">stdout</span></span><br><span class="line"><span class="built_in">stdout</span> = <span class="number">192.168</span><span class="number">.1</span><span class="number">.40</span></span><br><span class="line"><span class="number">192.168</span><span class="number">.1</span><span class="number">.40</span></span><br><span class="line">fangsong@fangsong-FX503VD:~/Mycode/<span class="number">1.</span>c语言$ vim <span class="number">2.</span><span class="built_in">printf</span>.cpp </span><br><span class="line">fangsong@fangsong-FX503VD:~/Mycode/<span class="number">1.</span>c语言$ g++ <span class="number">2.</span><span class="built_in">printf</span>.cpp </span><br><span class="line">fangsong@fangsong-FX503VD:~/Mycode/<span class="number">1.</span>c语言$ ./a.out</span><br><span class="line"><span class="number">192</span> <span class="number">168</span> <span class="number">1</span> <span class="number">40</span></span><br><span class="line"><span class="built_in">stdout</span> = <span class="number">192.168</span><span class="number">.1</span><span class="number">.40</span></span><br><span class="line"><span class="built_in">stderr</span> = <span class="number">192.168</span><span class="number">.1</span><span class="number">.40</span></span><br><span class="line">fangsong@fangsong-FX503VD:~/Mycode/<span class="number">1.</span>c语言$ cat <span class="built_in">stdout</span></span><br><span class="line"><span class="built_in">stdout</span> = <span class="number">192.168</span><span class="number">.1</span><span class="number">.40</span></span><br><span class="line"><span class="number">192.168</span><span class="number">.1</span><span class="number">.40</span></span><br><span class="line">fangsong@fangsong-FX503VD:~/Mycode/<span class="number">1.</span>c语言$ cat <span class="built_in">stderr</span></span><br><span class="line"><span class="built_in">stderr</span> = <span class="number">192.168</span><span class="number">.1</span><span class="number">.40</span></span><br><span class="line">fangsong@fangsong-FX503VD:~/Mycode/<span class="number">1.</span>c语言$ cat <span class="number">2.</span><span class="built_in">printf</span>.cpp </span><br><span class="line"><span class="comment">/*************************************************************************</span></span><br><span class="line"><span class="comment">> File Name: 2.printf.cpp</span></span><br><span class="line"><span class="comment">> Author:fangsong</span></span><br><span class="line"><span class="comment">> Mail: </span></span><br><span class="line"><span class="comment">> Created Time: 2019年12月01日 星期日 16时31分25秒</span></span><br><span class="line"><span class="comment"> ************************************************************************/</span></span><br><span class="line">#include <stdio.h></span><br><span class="line"><span class="keyword">int</span> main() {</span><br><span class="line"> <span class="keyword">char</span> str[<span class="number">100</span>], str2[<span class="number">100</span>];</span><br><span class="line"> <span class="keyword">int</span> ret;</span><br><span class="line"> <span class="comment">/* while((ret = scanf("%[^\n]s", str)) != EOF) {</span></span><br><span class="line"><span class="comment"> getchar();</span></span><br><span class="line"><span class="comment"> printf("%s ret = %d\n", str, ret);</span></span><br><span class="line"><span class="comment"> }*/</span></span><br><span class="line"> <span class="comment">/*scanf("%[^\n]s", str);</span></span><br><span class="line"><span class="comment"> getchar();</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="keyword">int</span> a, b, c, d;</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d%d%d%d"</span>, &a,&b,&c,&d);</span><br><span class="line"> <span class="built_in">sprintf</span>(str2, <span class="string">"%d.%d.%d.%d"</span>, a, b, c, d);</span><br><span class="line"> <span class="comment">// FILE *fd = fopen("out","wb");</span></span><br><span class="line"> <span class="comment">//stdin stdout stderr</span></span><br><span class="line"> <span class="built_in">fprintf</span>(<span class="built_in">stdout</span>, <span class="string">"stdout = %s\n"</span>,str2);</span><br><span class="line"> <span class="built_in">fprintf</span>(<span class="built_in">stderr</span>, <span class="string">"stderr = %s\n"</span>,str2);</span><br><span class="line"> <span class="comment">// printf("%s\n", str2);</span></span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p>![](../图片/2019-12-01 18-52-03屏幕截图.png)</p><h4 id="第二节:c语言中的数学函数库"><a href="#第二节:c语言中的数学函数库" class="headerlink" title="第二节:c语言中的数学函数库"></a>第二节:c语言中的数学函数库</h4><p>头文件:<code>math.h</code></p><p>![](../图片/2019-12-01 19-18-42屏幕截图.png)</p><p><code>Pi = acos(-1)</code></p><p>蒙特卡罗法求PI:</p><p>PI / 4 = M / N</p><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iostream></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstdio></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cmath></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><ctime></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstring></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iomanip></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><algorithm></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><map></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><vector></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> Max_n 100000000</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">int</span> n = <span class="number">0</span>, m = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i < Max_n; i++) {</span><br><span class="line"> <span class="keyword">double</span> x = rand() * <span class="number">1.0</span> / RAND_MAX * (rand() % <span class="number">2</span> ? <span class="number">1</span> : <span class="number">-1</span>); <span class="comment">//生成fudianshu随机数在区间-1~1</span></span><br><span class="line"> <span class="keyword">double</span> y = rand() * <span class="number">1.0</span> / RAND_MAX * (rand() % <span class="number">2</span> ? <span class="number">1</span> : <span class="number">-1</span>);</span><br><span class="line"> <span class="keyword">if</span>(x * x + y * y <= <span class="number">1</span>) {</span><br><span class="line"> m += <span class="number">1</span>;</span><br><span class="line"> } </span><br><span class="line"> n ++;</span><br><span class="line"> }</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%lf\n"</span>, m * <span class="number">1.0</span> / n * <span class="number">1.0</span> * <span class="number">4</span>);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p>x 是角度值</p><p><code>cos(x * Pi / 180)</code> 化为弧度值</p><p>pow, sqrt, ceil, floor ,fabs,返回类型double</p><p>abs 整形int</p><p>换底公式<br>$$<br>log a(b) = log10(b) / 10log(a);<br>$$</p><p><strong>二分开根号(非函数)</strong></p><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">define</span> EPS 1e-7</span></span><br><span class="line"><span class="function"><span class="keyword">double</span> <span class="title">my_sqrt1</span><span class="params">(<span class="keyword">double</span> x)</span> </span>{</span><br><span class="line"> <span class="keyword">double</span> l = <span class="number">0</span>, r = (x <= <span class="number">1.0</span> ? <span class="number">1.0</span> : x);</span><br><span class="line"> <span class="keyword">while</span>(r - l > EPS) {</span><br><span class="line"> <span class="keyword">double</span> mid = (l + r) / <span class="number">2.0</span>;</span><br><span class="line"> <span class="keyword">if</span>(mid * mid <= x) l = mid;</span><br><span class="line"> <span class="keyword">else</span> r = mid;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> l;</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">double</span> <span class="title">my_sqrt2</span><span class="params">(<span class="keyword">double</span> x)</span> </span>{</span><br><span class="line"> <span class="keyword">double</span> l = <span class="number">0</span>, r = (x <= <span class="number">1.0</span> ? <span class="number">1.0</span> : x);</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i < <span class="number">10000</span>; i++) {</span><br><span class="line"> <span class="keyword">double</span> mid = (l + r) / <span class="number">2.0</span>;</span><br><span class="line"> <span class="keyword">if</span>(mid * mid <= x) l = mid;</span><br><span class="line"> <span class="keyword">else</span> r = mid;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> l;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p><strong>牛顿迭代法开根号</strong></p><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">define</span> EPS 1e-7</span></span><br><span class="line"><span class="function"><span class="keyword">double</span> <span class="title">f</span><span class="params">(<span class="keyword">double</span> x, <span class="keyword">double</span> n)</span> </span>{</span><br><span class="line"> <span class="keyword">return</span> x * x - n; <span class="comment">// x * x - n = 0;</span></span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">double</span> <span class="title">f_prime</span><span class="params">(<span class="keyword">double</span> x)</span> </span>{</span><br><span class="line"> <span class="keyword">return</span> <span class="number">2</span> * x; <span class="comment">// x * x - n的导数</span></span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">double</span> <span class="title">my_sqrt</span><span class="params">(<span class="keyword">double</span> n)</span> </span>{</span><br><span class="line"> <span class="keyword">double</span> x = <span class="number">1.0</span>;</span><br><span class="line"> <span class="keyword">while</span>(<span class="built_in">fabs</span>(f(x , n)) > EPS) {</span><br><span class="line"> x = x - f(x , n) / f_prime(x);</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> x;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h4 id="第三节:各种应用"><a href="#第三节:各种应用" class="headerlink" title="第三节:各种应用"></a>第三节:各种应用</h4><h5 id="随机数"><a href="#随机数" class="headerlink" title="随机数"></a>随机数</h5><p>头文件:<code>time.h, stdlib.h</code></p><p>操作:</p><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">srand (time(<span class="number">0</span>) ); </span><br><span class="line"><span class="comment">// srand(6), 如果打 6, 就不随机了</span></span><br><span class="line"><span class="keyword">int</span> a = rand( ) % <span class="number">100</span>;</span><br></pre></td></tr></table></figure><h5 id="短路操作-amp-amp-,"><a href="#短路操作-amp-amp-," class="headerlink" title="短路操作&&, ||"></a>短路操作<code>&&</code>, <code>||</code></h5><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">i && <span class="built_in">cout</span> << <span class="string">" "</span> ; <span class="comment">// 如果i 等于0后面不进行</span></span><br><span class="line">i || <span class="built_in">cout</span> << <span class="string">" "</span>; <span class="comment">// 如果i != 0 后面不进行</span></span><br></pre></td></tr></table></figure><p>3.<code>do while</code>与<code>while</code>区别</p><p><code>do while()</code></p><h4 id="第四节:分支结构"><a href="#第四节:分支结构" class="headerlink" title="第四节:分支结构"></a>第四节:分支结构</h4><h5 id="分支预测:"><a href="#分支预测:" class="headerlink" title="分支预测:"></a>分支预测:</h5><blockquote><p>!!(x)归一化</p></blockquote><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">define</span> likely(x) __builtin_expect(!!(x), 1); <span class="comment">// 表示 x 的值为真的可能性更大</span></span></span><br><span class="line"><span class="comment">// 这个是说,如果x == 0,那么结果就是 0,如果x == 1, 那么结果就是1,使用了!!是为了让x转化成bool型的。</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> unlikely(x) __builtin_expect(!!(x), 0); <span class="comment">// 表示 x 的值为假的可能性更大</span></span></span><br><span class="line"><span class="comment">//使用 likely() ,执行 if 后面的语句 的机会更大,使用unlikely(),执行else 后面的语句的机会更大。</span></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">if( likely(val))</span></span><br><span class="line"><span class="comment">if( unlikely (val))</span></span><br><span class="line"><span class="comment">*/</span></span><br></pre></td></tr></table></figure><p>![](./图片/2019-12-05 18-27-57屏幕截图.png)</p><p>1.<strong><code>__builtin_ffs(unsigned int n)</code></strong><br>该函数判断n的二进制末尾最后一个1的位置,从一开始</p><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">int</span> n = <span class="number">1</span>;<span class="comment">//1</span></span><br><span class="line"><span class="keyword">int</span> m = <span class="number">8</span>;<span class="comment">//1000</span></span><br><span class="line"><span class="built_in">cout</span><<__builtin_ffs(n)<<<span class="built_in">endl</span>;<span class="comment">//输出1</span></span><br><span class="line"><span class="built_in">cout</span><<__builtin_ffs(m)<<<span class="built_in">endl</span>;<span class="comment">//输出4</span></span><br></pre></td></tr></table></figure><p>2.<strong><code>__builtin_popcount(unsigned int n)</code></strong><br>该函数时判断n的二进制中有多少个1</p><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">int</span> n = <span class="number">15</span>; <span class="comment">//二进制为1111</span></span><br><span class="line"><span class="built_in">cout</span><<__builtin_popcount(n)<<<span class="built_in">endl</span>;<span class="comment">//输出4</span></span><br></pre></td></tr></table></figure><p>3.<strong><code>__builtin_ctz(unsigned int n)</code></strong><br>该函数判断n的二进制末尾后面0的个数,当n为0时,和n的类型有关</p><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">int</span> n = <span class="number">1</span>;<span class="comment">//1</span></span><br><span class="line"><span class="keyword">int</span> m = <span class="number">8</span>;<span class="comment">//1000</span></span><br><span class="line"><span class="built_in">cout</span><<__builtin_ctzll(n)<<<span class="built_in">endl</span>;<span class="comment">//输出0</span></span><br><span class="line"><span class="built_in">cout</span><<__builtin_ctz(m)<<<span class="built_in">endl</span>;<span class="comment">//输出3</span></span><br></pre></td></tr></table></figure><p>4.<strong><code>__builtin_clz (unsigned int x)</code></strong><br>返回前导的0的个数。</p><p>5.<strong><code>__builtin_parity(unsigned int n)</code></strong><br>该函数是判断n的二进制中1的个数的奇偶性</p><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">int</span> n = <span class="number">15</span>;<span class="comment">//二进制为1111</span></span><br><span class="line"><span class="keyword">int</span> m = <span class="number">7</span>;<span class="comment">//111</span></span><br><span class="line"><span class="built_in">cout</span><<__builtin_parity(n)<<<span class="built_in">endl</span>;<span class="comment">//偶数个,输出0</span></span><br><span class="line"><span class="built_in">cout</span><<__builtin_parity(m)<<<span class="built_in">endl</span>;<span class="comment">//奇数个,输出1</span></span><br></pre></td></tr></table></figure><h4 id="第五节:函数"><a href="#第五节:函数" class="headerlink" title="第五节:函数"></a>第五节:函数</h4><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">**函数定义(函数声明):**</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">is_prime</span><span class="params">(<span class="keyword">int</span> x)</span> </span>{</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">2</span>; i <= x / <span class="number">2</span>; i++) {</span><br><span class="line"> <span class="keyword">if</span>(x % i == <span class="number">0</span>) {</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> retrun <span class="number">1</span>;</span><br><span class="line">}</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">int 返回值</span></span><br><span class="line"><span class="comment">is_prime 函数名</span></span><br><span class="line"><span class="comment">int x 参数声明列表</span></span><br><span class="line"><span class="comment">*/</span></span><br></pre></td></tr></table></figure><h5 id="K-amp-R风格函数定义-C99前"><a href="#K-amp-R风格函数定义-C99前" class="headerlink" title="K&R风格函数定义(C99前):"></a><strong>K&R风格函数定义(<code>C99</code>前):</strong></h5><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">is_prime</span><span class="params">( x )</span></span></span><br><span class="line">int x ;</span><br><span class="line">{</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">2</span>; i <= x / <span class="number">2</span>; i++) {</span><br><span class="line"> <span class="keyword">if</span>(x % i == <span class="number">0</span>) {</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> retrun <span class="number">1</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h5 id="递归程序"><a href="#递归程序" class="headerlink" title="递归程序"></a><strong>递归程序</strong></h5><p>(自己调用自己)<br>1.边界条件处理<br>2.针对问题的<code>处理过程</code>和<code>递归过程</code><br>3.结果返回</p><p>栈区 和 堆区</p><p><em>栈区(stack)</em> 函数运行时分配,函数结束时释放。由编译器自动分配释放 ,存放为运行函数而分配的局部变量、函数参数、返回数据、返回地址等。其操作方式类似于数据结构中的栈。</p><p><em>堆区(heap)</em>一般由程序员分配释放, 若程序员不释放,程序结束时可能由OS(操作系统)回收。分配方式类似于链表。</p><p>【若递归调用次数太多,就会只入栈不出栈,于是堆栈就被压爆了,此为栈溢出】。</p><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iostream></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">fac</span><span class="params">(<span class="keyword">int</span> n)</span> </span>{ <span class="comment">//每一次递归一下自己就开辟4个字节的(int)</span></span><br><span class="line"> <span class="keyword">if</span>(n == <span class="number">0</span>)<span class="keyword">return</span> <span class="number">1</span>; </span><br><span class="line"> <span class="keyword">return</span> n * fac(n - <span class="number">1</span>);</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">int</span> n;</span><br><span class="line"> <span class="built_in">cin</span> >> n;</span><br><span class="line"> <span class="built_in">cout</span> << fac(n) << <span class="built_in">endl</span>;</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h5 id="函数指针"><a href="#函数指针" class="headerlink" title="函数指针"></a><strong>函数指针</strong></h5><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">g</span><span class="params">(<span class="keyword">int</span> (*f1)(<span class="keyword">int</span>), <span class="keyword">int</span> (*f2)(<span class="keyword">int</span>), <span class="keyword">int</span> (*f3)(<span class="keyword">int</span>), <span class="keyword">int</span> x)</span> </span>{ <span class="comment">// int (*f1)(int)传过来的是函数 `int`返回类型 *f1把某一函数当做变量传过来(int)参数列表, </span></span><br><span class="line"> <span class="keyword">if</span>(x < <span class="number">0</span>) {</span><br><span class="line"> <span class="keyword">return</span> f1(x);</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span>(x < <span class="number">100</span>) {</span><br><span class="line"> <span class="keyword">return</span> f2(x);</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> f3(x);</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p><code>int (*f1)(int)</code>传过来的是函数<br>1 <code>int</code>返回类型<br>2<code>*f1</code>把某一函数当做变量传过来<br>3<code>(int)</code>参数列表, //只写类型</p><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iostream></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">fac1</span><span class="params">(<span class="keyword">int</span> n)</span> </span>{</span><br><span class="line"> <span class="keyword">if</span>(n == <span class="number">0</span>)<span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line"> <span class="keyword">return</span> n * fac1(n - <span class="number">1</span>);</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">fac2</span><span class="params">(<span class="keyword">int</span> n)</span> </span>{</span><br><span class="line"> <span class="keyword">return</span> (<span class="number">1</span> + n) * n / <span class="number">2</span>;</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">g</span><span class="params">(<span class="keyword">int</span> (*func)(<span class="keyword">int</span>), <span class="keyword">int</span> (*func2)(<span class="keyword">int</span>), <span class="keyword">int</span> x)</span> </span>{</span><br><span class="line"> <span class="keyword">if</span>(x < <span class="number">5</span>) {</span><br><span class="line"> <span class="keyword">return</span> func(x);</span><br><span class="line"> } <span class="keyword">else</span> {</span><br><span class="line"> <span class="keyword">return</span> func2(x);</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">int</span> n;</span><br><span class="line"> <span class="keyword">while</span>(<span class="built_in">cin</span> >> n) {</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"f(%d) = %d\n"</span>, n, g(fac1, fac2, n));</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p>![](./图片/2019-12-07 15-55-08屏幕截图.png)</p><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iostream></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><inttypes.h></span></span></span><br><span class="line"><span class="keyword">typedef</span> <span class="keyword">int64_t</span> int1;</span><br><span class="line"><span class="function">int1 <span class="title">Triangle</span><span class="params">(int1 n)</span> </span>{</span><br><span class="line"> <span class="keyword">return</span> n*(n + <span class="number">1</span>) / <span class="number">2</span>;</span><br><span class="line">}</span><br><span class="line"><span class="function">int1 <span class="title">Pentagonal</span><span class="params">(int1 n)</span> </span>{</span><br><span class="line"> <span class="keyword">return</span> n * (<span class="number">3</span> * n - <span class="number">1</span>) / <span class="number">2</span>;</span><br><span class="line">}</span><br><span class="line"><span class="function">int1 <span class="title">Hexagonal</span><span class="params">(int1 n)</span> </span>{</span><br><span class="line"> <span class="keyword">return</span> n * ( <span class="number">2</span> * n - <span class="number">1</span>);</span><br><span class="line">}</span><br><span class="line"><span class="function">int1 <span class="title">binary_search</span><span class="params">(int1 (*func)(int1),int1 x)</span> </span>{</span><br><span class="line"> int1 head = <span class="number">1</span>, tail = x, mid ;</span><br><span class="line"> <span class="keyword">while</span>(head <= tail) {</span><br><span class="line"> mid = (head + tail) / <span class="number">2</span>;</span><br><span class="line"> <span class="keyword">if</span>(func(mid) == x)<span class="keyword">return</span> mid;</span><br><span class="line"> <span class="keyword">if</span>(func(mid) < x)head = mid + <span class="number">1</span>;</span><br><span class="line"> <span class="keyword">else</span> tail = mid - <span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">int</span> n = <span class="number">286</span>;</span><br><span class="line"> <span class="keyword">for</span>(;; n++) {</span><br><span class="line"> <span class="keyword">if</span>(binary_search(Triangle, Hexagonal(n)) == <span class="number">-1</span>)<span class="keyword">continue</span>; <span class="comment">//3 5</span></span><br><span class="line"> <span class="keyword">if</span>(binary_search(Pentagonal, Hexagonal(n)) == <span class="number">-1</span>)<span class="keyword">continue</span>;<span class="comment">//3 6</span></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%"</span> PRId64 <span class="string">"\n"</span>, Hexagonal(n));</span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iostream></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstdio></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cmath></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstring></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iomanip></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><algorithm></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><map></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><vector></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">f1</span><span class="params">(<span class="keyword">int</span> n)</span> </span>{</span><br><span class="line"> <span class="keyword">return</span> n * <span class="number">2</span>;</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">f2</span><span class="params">(<span class="keyword">int</span> n)</span> </span>{</span><br><span class="line"> <span class="keyword">return</span> n * n + <span class="number">3</span>;</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">f3</span><span class="params">(<span class="keyword">int</span> n)</span> </span>{</span><br><span class="line"> <span class="keyword">return</span> n >> <span class="number">1</span>;</span><br><span class="line">}</span><br><span class="line"><span class="keyword">int</span> (*f[<span class="number">3</span>])(<span class="keyword">int</span>) = {</span><br><span class="line"> f1, f2, f3</span><br><span class="line">};</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{</span><br><span class="line"> srand(time(<span class="number">0</span>));</span><br><span class="line"> <span class="keyword">int</span> n;</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d"</span>, &n);</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d\n"</span>, f[rand() % <span class="number">3</span>](n));</span><br><span class="line"></span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p>![](/home/fangsong/文档/图片/2020-01-10 20-29-35屏幕截图.png)</p><h5 id="变参函数"><a href="#变参函数" class="headerlink" title="变参函数**"></a>变参函数**</h5><p>实现可变参<code>max_int</code>, 从若干个传入的参数返回最大值</p><p><code>int max_int(int a, ... );</code> “…”传入的变参</p><p>如何获得a往后的参数列表?<code>va_list</code>类型的变量</p><p>如何定位a往后的第一个参数的位子?<code>va_start</code>函数</p><p>如何获取下一个可变参列表中的参数?<code>va_arg</code>函数</p><p>如何结束整个获取可变参数列表的动作?<code>va_end</code>函数</p><p>![](./图片/2020-01-10 20-36-27屏幕截图.png)</p><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iostream></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><stdarg.h></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><inttypes.h></span></span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">max_int</span><span class="params">(<span class="keyword">int</span> n, ...)</span> </span>{ </span><br><span class="line"> <span class="comment">// n 个数字最大</span></span><br><span class="line"> va_list arg; <span class="comment">//定义一个变量</span></span><br><span class="line"> va_start(arg, n); <span class="comment">// 从n后第一个参数值开始放在arg中</span></span><br><span class="line"> <span class="keyword">int</span> ans = INT32_MIN;</span><br><span class="line"> <span class="keyword">while</span>(n --) {</span><br><span class="line"> <span class="keyword">int</span> temp = va_arg(arg, <span class="keyword">int</span>); <span class="comment">//取arg里的值,顺序取值</span></span><br><span class="line"> temp > ans && (ans = temp);</span><br><span class="line"> }</span><br><span class="line"> va_end(arg); <span class="comment">//销毁</span></span><br><span class="line"> <span class="keyword">return</span> ans;</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d\n"</span>,max_int(<span class="number">3</span>, <span class="number">3</span>, <span class="number">5</span>, <span class="number">16</span>));</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d\n"</span>,max_int(<span class="number">4</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">17</span>, <span class="number">21</span>));</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d\n"</span>,max_int(<span class="number">4</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">21</span>, <span class="number">-32</span>));</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p>宏:</p><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iostream></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstdio></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> swap_n(a, b) {\</span></span><br><span class="line"> __typeof(a) __temp = a;\</span><br><span class="line"> a = b, b = __temp;\</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">int</span> a, b;</span><br><span class="line"> <span class="built_in">cin</span> >> a >> b;</span><br><span class="line"> swap_n(a, b);</span><br><span class="line"> <span class="built_in">cout</span> << a << <span class="string">' '</span> << b << <span class="built_in">endl</span>;</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>; </span><br><span class="line">}</span><br></pre></td></tr></table></figure><h4 id="第六节:算法"><a href="#第六节:算法" class="headerlink" title="第六节:算法"></a>第六节:算法</h4><h5 id="欧几里得算法"><a href="#欧几里得算法" class="headerlink" title="欧几里得算法"></a><strong>欧几里得算法</strong></h5><ul><li>1、又名辗转相除</li><li>2、用于快速计算两个数字的最大公约数</li><li>3、还可以快速求解<code>a * x + b * y = 1</code>的一组整数解</li></ul><p>假想<code>gcd(a, b) _= gcd(b, a % b)</code> 恒等号:<code>_=</code></p><p><code>gcd(a, b) = r</code>, <code>r</code>最大</p><p>![](./图片/2019-12-07 18-35-02屏幕截图.png)</p><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iostream></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">gcd</span><span class="params">(<span class="keyword">int</span> a, <span class="keyword">int</span> b)</span> </span>{</span><br><span class="line"> <span class="keyword">return</span> b ? gcd(b, a % b) : a;</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">int</span> a, b;</span><br><span class="line"> <span class="keyword">while</span>(<span class="built_in">cin</span> >> a >> b) {</span><br><span class="line"> <span class="built_in">cout</span> << gcd(a, b) << <span class="built_in">endl</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h5 id="扩展欧几里得算法"><a href="#扩展欧几里得算法" class="headerlink" title="扩展欧几里得算法"></a><strong>扩展欧几里得算法</strong></h5><p>推导过程:</p><p>![](./图片/2019-12-07 19-44-23屏幕截图.png)</p><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iostream></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstdio></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cmath></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstring></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iomanip></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><algorithm></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><map></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><vector></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">ex_gcd</span><span class="params">(<span class="keyword">int</span> a, <span class="keyword">int</span> b, <span class="keyword">int</span> *x, <span class="keyword">int</span> *y)</span> </span>{</span><br><span class="line"> <span class="keyword">if</span>(!b) {</span><br><span class="line"> *x = <span class="number">1</span>;</span><br><span class="line"> *y = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">return</span> a;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">int</span> xx, yy,ret = ex_gcd(b, a % b, &xx, &yy);</span><br><span class="line"> *x = yy;</span><br><span class="line"> *y = xx - (a / b) * yy;</span><br><span class="line"> <span class="keyword">return</span> ret;</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">int</span> a, b, x, y;</span><br><span class="line"> <span class="built_in">cin</span> >> a >> b;</span><br><span class="line"> <span class="built_in">cout</span> << ex_gcd(a, b, &x, &y) << <span class="built_in">endl</span>;</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h4 id="第七节:数组与字符串"><a href="#第七节:数组与字符串" class="headerlink" title="第七节:数组与字符串"></a>第七节:数组与字符串</h4><h5 id="prime-cpp"><a href="#prime-cpp" class="headerlink" title="prime.cpp"></a><code>prime.cpp</code></h5><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><stdio.h></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> MAX_N 1000</span></span><br><span class="line"><span class="comment">/* </span></span><br><span class="line"><span class="comment">int prime[MAX_N + 5];</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">void init_prime() {</span></span><br><span class="line"><span class="comment"> prime[0] = prime[1] = 1;</span></span><br><span class="line"><span class="comment"> for (int i = 2; i * i <= MAX_N; i++) {</span></span><br><span class="line"><span class="comment"> if (prime[i]) continue;</span></span><br><span class="line"><span class="comment"> for (int j = i * i; j <= MAX_N; j += i) {</span></span><br><span class="line"><span class="comment"> prime[j] = 1;</span></span><br><span class="line"><span class="comment"> }</span></span><br><span class="line"><span class="comment"> }</span></span><br><span class="line"><span class="comment"> return ;</span></span><br><span class="line"><span class="comment">}</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">init_prime</span><span class="params">()</span> </span>{}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">prime</span><span class="params">(<span class="keyword">int</span> x)</span> </span>{</span><br><span class="line"> <span class="keyword">if</span> (x <= <span class="number">1</span>) <span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">2</span>; i * i <= x; i++) {</span><br><span class="line"> <span class="keyword">if</span> (x % i) <span class="keyword">continue</span>;</span><br><span class="line"> <span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{</span><br><span class="line"> init_prime();</span><br><span class="line"> <span class="keyword">int</span> x;</span><br><span class="line"> <span class="keyword">while</span> (~<span class="built_in">scanf</span>(<span class="string">"%d"</span>, &x)) {</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%s\n"</span>, prime(x) ? <span class="string">"No"</span> : <span class="string">"Yes"</span>);</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h5 id="数组与地址关系"><a href="#数组与地址关系" class="headerlink" title="数组与地址关系"></a>数组与地址关系</h5><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><stdio.h></span></span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">func</span><span class="params">(<span class="keyword">int</span> n)</span> </span>{ <span class="comment">// 形参</span></span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">func1</span><span class="params">(<span class="keyword">int</span> *n)</span> </span>{ <span class="comment">// *n接收</span></span><br><span class="line"> n = <span class="number">123</span>;</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">func3</span><span class="params">(<span class="keyword">int</span> *a, <span class="keyword">int</span> n)</span> </span>{</span><br><span class="line"> <span class="comment">//a[3]和 a + 3一样</span></span><br><span class="line"> <span class="comment">/*</span></span><br><span class="line"><span class="comment"> *a = sizeof(a)= 8; 而主函数中arr = sizeof(arr) = 400;</span></span><br><span class="line"><span class="comment"> *a 本身表示指向整形的变量;arr是外部总共字节4 * 100 = 400;</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">arr + 0 </span></span><br><span class="line"><span class="comment">&a[0] </span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">func4</span><span class="params">(<span class="keyword">int</span> *a, <span class="keyword">int</span> n)</span></span>{ <span class="comment">// 一位数组 </span></span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">func5</span><span class="params">(<span class="keyword">int</span> *a[<span class="number">1500</span>], <span class="keyword">int</span> n)</span> </span>{ <span class="comment">//二维数组</span></span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">func6</span><span class="params">(<span class="keyword">int</span> *a[<span class="number">1500</span>][<span class="number">1500</span>], <span class="keyword">int</span> n)</span> </span>{ <span class="comment">//三维数组</span></span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">int</span> arr[<span class="number">100</span>];</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"arr = %p, &arr[0] = %p\n"</span>, arr, &arr[<span class="number">0</span>]); </span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"arr + 1 = %p, &arr[1] = %p\n"</span>, arr + <span class="number">1</span>, &arr[<span class="number">1</span>]); </span><br><span class="line"> <span class="keyword">char</span> *p = (<span class="keyword">char</span> *)arr;</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"p + 1 = %p, p + 2 = %p"</span>, p + <span class="number">1</span>, p + <span class="number">2</span>); <span class="comment">// 其地址相差1(char)</span></span><br><span class="line"> <span class="keyword">int</span> n = <span class="number">0</span>;</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d"</span>, &n);</span><br><span class="line"> func(n); <span class="comment">// 实参</span></span><br><span class="line"> func1(&n); <span class="comment">//传参</span></span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i < n; i++) {</span><br><span class="line"> <span class="built_in">scanf</span>(<span class="string">"%d"</span>, arr + i);</span><br><span class="line"> }</span><br><span class="line"> func3(arr, n);</span><br><span class="line"> func4(arr, n);</span><br><span class="line"> func5(arr, n);</span><br><span class="line"> func6(arr, n);</span><br><span class="line"> </span><br><span class="line"><span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><stdio.h></span></span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">func_2</span><span class="params">(<span class="keyword">int</span> (*a)[<span class="number">200</span>][<span class="number">300</span>])</span> </span>{</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">int</span> arr[<span class="number">100</span>][<span class="number">200</span>][<span class="number">300</span>];</span><br><span class="line"> func_2(arr);</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"&arr[0] = %p arr = %p\n"</span>, &arr[<span class="number">0</span>], arr);</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"arr + 1 = %p arr + 2 = %p\n"</span>, arr + <span class="number">1</span>, arr + <span class="number">2</span>);</span><br><span class="line"> <span class="keyword">char</span> *p = (<span class="keyword">char</span> *)arr;</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"p + 1 = %p p + 2 = %p\n"</span>, p + <span class="number">1</span>, p + <span class="number">2</span>);</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"sizeof(arr) = %lu\n"</span>, <span class="keyword">sizeof</span>(arr));</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p><strong>将字符串按空格换行输出</strong></p><p>![](./图片/2020-01-11 16-27-23屏幕截图.png)</p><h4 id="第八节:"><a href="#第八节:" class="headerlink" title="第八节:"></a>第八节:</h4><h5 id="函数补充"><a href="#函数补充" class="headerlink" title="函数补充"></a>函数补充</h5><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iostream></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstdio></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cmath></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstring></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iomanip></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><algorithm></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><map></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><vector></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">funcA</span><span class="params">(<span class="keyword">int</span>)</span></span>; <span class="comment">// 必须得加, 为了告诉程序运行到funcB时中有一个funcA, 声明作用</span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">funcB</span><span class="params">(<span class="keyword">int</span> n)</span> </span>{</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"funcB : %d\n"</span>, n);</span><br><span class="line"> <span class="keyword">if</span>(n == <span class="number">0</span>) <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line"> funcA(n - <span class="number">1</span>);</span><br><span class="line"> <span class="keyword">return</span> n;</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">funcA</span><span class="params">(<span class="keyword">int</span> n)</span> </span>{</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"funcA : %d\n"</span>, n);</span><br><span class="line"> <span class="keyword">if</span>(n == <span class="number">0</span>)<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line"> funcB(n - <span class="number">1</span>);</span><br><span class="line"> <span class="keyword">return</span> n;</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{ </span><br><span class="line">}</span><br></pre></td></tr></table></figure><h5 id="多文件编译"><a href="#多文件编译" class="headerlink" title="多文件编译"></a>多文件编译</h5><p>选项 -c 用来告诉编译器编译源代码但不要执行链接,输出结果为对象文件。文件默认名与源码文件名相同,只是将其后缀变为 .o。</p><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">g++ -c <span class="number">1.</span>cpp <span class="comment">// 生成-o文件1</span></span><br><span class="line">g++ -c <span class="number">2.</span>cpp <span class="comment">// 生成-o文件2</span></span><br><span class="line">g++ <span class="number">1.</span>o <span class="number">2.</span>o</span><br></pre></td></tr></table></figure><h5 id="创建静态库"><a href="#创建静态库" class="headerlink" title="创建静态库"></a>创建静态库</h5><p>静态库是编译器生成的一系列对象文件的集合。链接一个程序时用库中的对象文件还是目录中的对象文件都是一样的。库中的成员包括普通函数,类定义,类的对象实例等等。静态库的另一个名字叫归档文件(archive),管理这种归档文件的工具叫 ar 。</p><p>在下面的例子中,我们先创建两个对象模块,然后用其生成静态库。</p><p>头文件 say.h 包含函数 sayHello() 的原型和类 Say 的定义:</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"> 1 /* say.h */</span><br><span class="line"> 2 #include <iostream></span><br><span class="line"> 3 void sayhello(void);</span><br><span class="line"> 4 class Say {</span><br><span class="line"> 5 private:</span><br><span class="line"> 6 char *string;</span><br><span class="line"> 7 public:</span><br><span class="line"> 8 Say(char *str)</span><br><span class="line"> 9 {</span><br><span class="line">10 string = str;</span><br><span class="line">11 }</span><br><span class="line">12 void sayThis(const char *str)</span><br><span class="line">13 {</span><br><span class="line">14 std::cout << str << " from a static library\n";</span><br><span class="line">15 }</span><br><span class="line">16 void sayString(void);</span><br><span class="line">17 };</span><br></pre></td></tr></table></figure><p>下面是文件 say.cpp 是我们要加入到静态库中的两个对象文件之一的源码。它包含 Say 类中 sayString() 函数的定义体;类 Say 的一个实例 librarysay 的声明也包含在内:</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">1 /* say.cpp */</span><br><span class="line">2 #include "say.h"</span><br><span class="line">3 void Say::sayString()</span><br><span class="line">4 {</span><br><span class="line">5 std::cout << string << "\n";</span><br><span class="line">6 }</span><br><span class="line">7 </span><br><span class="line">8 Say librarysay("Library instance of Say");</span><br></pre></td></tr></table></figure><p>源码文件 sayhello.cpp 是我们要加入到静态库中的第二个对象文件的源码。它包含函数 sayhello() 的定义:</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">1 /* sayhello.cpp */</span><br><span class="line">2 #include "say.h"</span><br><span class="line">3 void sayhello()</span><br><span class="line">4 {</span><br><span class="line">5 std::cout << "hello from a static library\n";</span><br><span class="line">6 }</span><br></pre></td></tr></table></figure><p>下面的命令序列将源码文件编译成对象文件,命令 ar 将其存进库中:</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">1 $ g++ -c sayhello.cpp</span><br><span class="line">2 </span><br><span class="line">3 $ g++ -c say.cpp</span><br><span class="line">4 </span><br><span class="line">5 $ ar -r libsay.a sayhello.o say.o</span><br></pre></td></tr></table></figure><p>程序 ar 配合参数 -r 创建一个新库 libsay.a 并将命令行中列出的对象文件插入。采用这种方法,如果库不存在的话,参数 -r 将创建一个新的库,而如果库存在的话,将用新的模块替换原来的模块。</p><p>下面是主程序 saymain.cpp,它调用库 libsay.a 中的代码:</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"> 1 /* saymain.cpp */</span><br><span class="line"> 2 #include "say.h"</span><br><span class="line"> 3 int main(int argc,char *argv[])</span><br><span class="line"> 4 {</span><br><span class="line"> 5 extern Say librarysay;</span><br><span class="line"> 6 Say localsay = Say("Local instance of Say");</span><br><span class="line"> 7 sayhello();</span><br><span class="line"> 8 librarysay.sayThis("howdy");</span><br><span class="line"> 9 librarysay.sayString();</span><br><span class="line">10 localsay.sayString();</span><br><span class="line">11 return(0);</span><br><span class="line">12 }</span><br></pre></td></tr></table></figure><p>该程序可以下面的命令来编译和链接:</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">1 $ g++ saymain.cpp libsay.a -o saymain</span><br></pre></td></tr></table></figure><p>程序运行时,产生以下输出:</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">hello from a static library</span><br><span class="line"></span><br><span class="line">howdy from a static library</span><br><span class="line"></span><br><span class="line">Library instance of Say</span><br><span class="line"></span><br><span class="line">Local instance of Say</span><br></pre></td></tr></table></figure><h5 id="实现简易的printf函数"><a href="#实现简易的printf函数" class="headerlink" title="实现简易的printf函数"></a>实现简易的printf函数</h5><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><stdio.h></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><inttypes.h></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><stdarg.h></span></span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">reverse_num</span><span class="params">(<span class="keyword">int</span> num, <span class="keyword">int</span> *temp)</span> </span>{</span><br><span class="line"> <span class="keyword">int</span> digit = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">do</span> {</span><br><span class="line"> *temp = *temp * <span class="number">10</span> + num % <span class="number">10</span>;</span><br><span class="line"> num /= <span class="number">10</span>;</span><br><span class="line"> digit += <span class="number">1</span>;</span><br><span class="line"> } <span class="keyword">while</span>(num);</span><br><span class="line"> <span class="keyword">return</span> digit;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">output_num</span><span class="params">(<span class="keyword">int</span> temp, <span class="keyword">int</span> digit)</span> </span>{</span><br><span class="line"> <span class="keyword">int</span> cnt = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">while</span> (digit--) {</span><br><span class="line"> <span class="built_in">putchar</span>(temp % <span class="number">10</span> + <span class="string">'0'</span>);</span><br><span class="line"> temp /= <span class="number">10</span>;</span><br><span class="line"> cnt += <span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> cnt;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">my_printf</span><span class="params">(<span class="keyword">const</span> <span class="keyword">char</span> *frm, ...)</span> </span>{</span><br><span class="line"> <span class="keyword">int</span> cnt = <span class="number">0</span>;</span><br><span class="line"> <span class="meta">#<span class="meta-keyword">define</span> PUTC(a) putchar(a), ++cnt</span></span><br><span class="line"> va_list arg;</span><br><span class="line"> va_start(arg, frm);</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; frm[i]; i++) {</span><br><span class="line"> <span class="keyword">switch</span> (frm[i]) {</span><br><span class="line"> <span class="keyword">case</span> <span class="string">'%'</span>: {</span><br><span class="line"> <span class="keyword">switch</span> (frm[++i]) {</span><br><span class="line"> <span class="keyword">case</span> <span class="string">'d'</span>: {</span><br><span class="line"> <span class="keyword">int</span> xx = va_arg(arg, <span class="keyword">int</span>);</span><br><span class="line"> <span class="keyword">uint32_t</span> x;</span><br><span class="line"> <span class="keyword">if</span> (xx < <span class="number">0</span>) PUTC(<span class="string">'-'</span>), x = -xx;</span><br><span class="line"> <span class="keyword">else</span> x = xx;</span><br><span class="line"> <span class="keyword">int</span> num1 = x / <span class="number">100000</span>, num2 = x % <span class="number">100000</span>;</span><br><span class="line"> <span class="keyword">int</span> temp1 = <span class="number">0</span>, temp2 = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">int</span> digit1 = reverse_num(num1, &temp1);</span><br><span class="line"> <span class="keyword">int</span> digit2 = reverse_num(num2, &temp2);</span><br><span class="line"> <span class="keyword">if</span> (num1) digit2 = <span class="number">5</span>;</span><br><span class="line"> <span class="keyword">else</span> digit1 = <span class="number">0</span>;</span><br><span class="line"> cnt += output_num(temp1, digit1);</span><br><span class="line"> cnt += output_num(temp2, digit2);</span><br><span class="line"> } <span class="keyword">break</span>;</span><br><span class="line"> <span class="keyword">case</span> <span class="string">'%'</span>: {</span><br><span class="line"> PUTC(frm[i]);</span><br><span class="line"> } <span class="keyword">break</span>;</span><br><span class="line"> <span class="keyword">case</span> <span class="string">'s'</span>: {</span><br><span class="line"> <span class="keyword">const</span> <span class="keyword">char</span> *str = va_arg(arg, <span class="keyword">const</span> <span class="keyword">char</span> *);</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; str[i]; i++) {</span><br><span class="line"> PUTC(str[i]);</span><br><span class="line"> }</span><br><span class="line"> } <span class="keyword">break</span>;</span><br><span class="line"> }</span><br><span class="line"> } <span class="keyword">break</span>;</span><br><span class="line"> <span class="keyword">default</span>: PUTC(frm[i]); <span class="keyword">break</span>;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> va_end(arg);</span><br><span class="line"> <span class="meta">#<span class="meta-keyword">undef</span> PUTC</span></span><br><span class="line"> <span class="keyword">return</span> cnt;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">int</span> a = <span class="number">-123</span>;</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"hello world\n"</span>);</span><br><span class="line"> my_printf(<span class="string">"hello world\n"</span>);</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"int (a) = %d\n"</span>, a);</span><br><span class="line"> my_printf(<span class="string">"int (a) = %d\n"</span>, a);</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"INT32_MAX = %d\n"</span>, INT32_MAX);</span><br><span class="line"> my_printf(<span class="string">"INT32_MAX = %d\n"</span>, INT32_MAX);</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"INT32_MIN = %d\n"</span>, INT32_MIN);</span><br><span class="line"> my_printf(<span class="string">"INT32_MIN = %d\n"</span>, INT32_MIN);</span><br><span class="line"> <span class="keyword">char</span> str[<span class="number">100</span>] = <span class="string">"I love Beijing !"</span>;</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%s\n"</span>, str);</span><br><span class="line"> my_printf(<span class="string">"%s\n"</span>, str);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h5 id="二分查找-amp-牛顿迭代"><a href="#二分查找-amp-牛顿迭代" class="headerlink" title="二分查找 & 牛顿迭代"></a>二分查找 & 牛顿迭代</h5><p>函数是压缩的数组,数组是函数的展开</p><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iostream></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="comment">//0 1 2 3 4 5 6 7</span></span><br><span class="line"><span class="comment">//0 1 4 9 16 25 36 49</span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">f</span><span class="params">(<span class="keyword">int</span> x)</span> </span>{</span><br><span class="line"> <span class="keyword">return</span> x * x;</span><br><span class="line">}</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">binary_search</span><span class="params">(<span class="keyword">int</span> (*arr)(<span class="keyword">int</span>), <span class="keyword">int</span> x, <span class="keyword">int</span> n)</span> </span>{</span><br><span class="line"> <span class="keyword">int</span> l = <span class="number">0</span>, r = n - <span class="number">1</span>, mid;</span><br><span class="line"> <span class="keyword">while</span>(l <= r) {</span><br><span class="line"> mid = (l + r) >> <span class="number">1</span>;</span><br><span class="line"> <span class="keyword">if</span>(arr(mid) == x) <span class="keyword">return</span> mid;</span><br><span class="line"> <span class="keyword">if</span>(arr(mid) < x) l = mid + <span class="number">1</span>;</span><br><span class="line"> <span class="keyword">else</span> r = mid - <span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">int</span> y;</span><br><span class="line"> <span class="keyword">while</span>(~<span class="built_in">scanf</span>(<span class="string">"%d"</span>, &y)) {</span><br><span class="line"> <span class="keyword">int</span> x = binary_search(f, y, <span class="number">100</span>);</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d * %d = %d\n"</span>, x , x, y ); </span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h6 id="牛顿迭代"><a href="#牛顿迭代" class="headerlink" title="牛顿迭代"></a>牛顿迭代</h6><p>求x * x - 2 = 0</p><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iostream></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstdio></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cmath></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstring></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iomanip></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><algorithm></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><map></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><vector></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> exp 1e-6</span></span><br><span class="line"><span class="function"><span class="keyword">double</span> <span class="title">F</span><span class="params">(<span class="keyword">double</span> x)</span> </span>{</span><br><span class="line"> <span class="keyword">return</span> x * x - <span class="number">2</span>; </span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">double</span> <span class="title">f</span><span class="params">(<span class="keyword">double</span> x)</span> </span>{</span><br><span class="line"> <span class="keyword">return</span> <span class="number">2</span> * x;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">double</span> <span class="title">NewTon</span><span class="params">(<span class="keyword">double</span> (*F)(<span class="keyword">double</span>), <span class="keyword">double</span> (*f)(<span class="keyword">double</span>))</span> </span>{</span><br><span class="line"> <span class="keyword">double</span> x = <span class="number">1.0</span>;</span><br><span class="line"> <span class="keyword">while</span>(<span class="built_in">fabs</span>(F(x)) > <span class="built_in">exp</span>) {</span><br><span class="line"> x -= F(x) / f(x);</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> x;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%lf\n"</span>,NewTon(F, f) );</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h6 id="二分拓展:"><a href="#二分拓展:" class="headerlink" title="二分拓展:"></a>二分拓展:</h6><ol><li><p>一组数 :1 2 3 4 5 6 6 6 6 7 7 8 9, 查找第一个“6”</p></li><li><p>一组数 :00000001111111查找第一个“1”;</p><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">binary_search1</span><span class="params">(<span class="keyword">int</span> *arr, <span class="keyword">int</span> n)</span> </span>{</span><br><span class="line"> <span class="keyword">int</span> head = <span class="number">0</span>, tail = n - <span class="number">1</span>, mid;</span><br><span class="line"> <span class="keyword">while</span>(head < tail) {</span><br><span class="line"> mid = (head + tail) >> <span class="number">1</span>;</span><br><span class="line"> <span class="keyword">if</span>(arr[mid] == <span class="number">1</span>) tail = mid;</span><br><span class="line"> <span class="keyword">else</span> head = mid + <span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> head;</span><br><span class="line">}</span><br></pre></td></tr></table></figure></li><li><p>一组数:11111111000000查找最后一个“1”;</p><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">binary_search2</span><span class="params">(<span class="keyword">int</span> *arr, <span class="keyword">int</span> n)</span> </span>{</span><br><span class="line"> <span class="keyword">int</span> head = <span class="number">-1</span>, tail = n - <span class="number">1</span>, mid;</span><br><span class="line"> <span class="keyword">while</span>(head < tail) {</span><br><span class="line"> mid = (head + tail + <span class="number">1</span>) >> <span class="number">1</span>;</span><br><span class="line"> <span class="keyword">if</span>(arr[mid] == <span class="number">1</span>)head = mid;</span><br><span class="line"> <span class="keyword">else</span> tail = mid - <span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> head;</span><br><span class="line">}</span><br></pre></td></tr></table></figure></li></ol><h4 id="第九节、预处理命令-宏定义"><a href="#第九节、预处理命令-宏定义" class="headerlink" title="第九节、预处理命令-宏定义"></a>第九节、预处理命令-宏定义</h4><p>定义符号常量:</p><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">define</span> PI 3.1415926</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> MAX_N 10000</span></span><br></pre></td></tr></table></figure><p>定义傻瓜表达式:</p><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">define</span> MAX(a, b) (a) > (b) ? (a) : (b)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> S(a, b) a * b; <span class="comment">// 只是想替换对应的变量(简单替换)</span></span></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">S(3 + 4, 5 + 6) = 3 + 4 * 5 + 6</span></span><br><span class="line"><span class="comment">*/</span></span><br></pre></td></tr></table></figure><p>定义代码段:</p><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">define</span> P(a) { \ </span></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d\n"</span>, a);\</span><br><span class="line">}</span><br></pre></td></tr></table></figure><table><thead><tr><th>宏</th><th>说明</th></tr></thead><tbody><tr><td><code>__DATE__</code></td><td>日期:<code>Mmm dd yyyy</code></td></tr><tr><td><code>__TIME__</code></td><td>时间:<code>hh:mm:ss</code></td></tr><tr><td><code>__LINE__</code></td><td>行号</td></tr><tr><td><code>__FILE__</code></td><td>文件名</td></tr><tr><td><code>__func__</code></td><td>函数名/非标准</td></tr><tr><td><code>__FUNC__</code></td><td>函数名/非标准</td></tr><tr><td><code>__PRETTY_FUNCTION__</code></td><td>更详细的函数信息/非标准</td></tr></tbody></table><p>![](./图片/2020-01-10 21-02-13屏幕截图.png)</p><p>条件式编译</p><p><img src="./%E5%9B%BE%E7%89%87/%E5%AE%8F%EF%BC%8D%E6%9D%A1%E4%BB%B6%E5%BC%8F%E7%BC%96%E8%AF%91.png" alt=""></p><p><img src="./%E5%9B%BE%E7%89%87/%E9%A2%84%E5%A4%84%E7%90%86%E5%91%BD%E4%BB%A4.png" alt=""></p><p>![](./图片/2020-01-10 20-53-21屏幕截图.png)</p><p>对象文件:<br>Linux: .o 结尾 <br>Windows: .obj 结尾 </p><ul><li>max.cpp </li></ul><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><stdio.h></span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> MAX(a, b) ({ \</span></span><br><span class="line"> __typeof(a) _a = (a); \</span><br><span class="line"> __typeof(b) _b = (b); \</span><br><span class="line"> _a > _b ? _a : _b; \</span><br><span class="line">})</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> P(a) { \</span></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"[%s : %d] %s = %d\n"</span>, __func__, __LINE__, #a, a); \ </span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">int</span> a = <span class="number">7</span>;</span><br><span class="line"> P(MAX(<span class="number">2</span>, <span class="number">3</span>));</span><br><span class="line"> P(<span class="number">5</span> + MAX(<span class="number">2</span>, <span class="number">3</span>));</span><br><span class="line"> P(MAX(<span class="number">2</span>, MAX(<span class="number">3</span>, <span class="number">4</span>)));</span><br><span class="line"> P(MAX(<span class="number">2</span>, <span class="number">3</span> > <span class="number">4</span> ? <span class="number">3</span> : <span class="number">4</span>));</span><br><span class="line"> P(MAX(a++, <span class="number">6</span>));</span><br><span class="line"> P(a);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><stdio.h></span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">ifdef</span> DEBUG <span class="comment">//如果使用g++ DEBUG fs.cpp就执行以下语句到 #else</span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> log(frm, arg...) { \</span></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"[%s : %s : %d]"</span>,__FILE__, __func__, __LINE__ );\</span><br><span class="line"> <span class="built_in">printf</span>(frm, ##arg);\</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"\n"</span>);\</span><br><span class="line">}</span><br><span class="line"><span class="meta">#<span class="meta-keyword">else</span> <span class="comment">// 否则执行下面语句到#endif</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> log(frm, arg...)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">endif</span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> contact(a, b) a##b <span class="comment">//##代表链接连个字符</span></span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">int</span> a = <span class="number">123</span>;</span><br><span class="line"> <span class="built_in">log</span>(<span class="string">"%d"</span>, a);</span><br><span class="line"> <span class="keyword">int</span> abcdef = <span class="number">15</span>;</span><br><span class="line"> contact(abc, def);</span><br><span class="line"> <span class="built_in">log</span>(<span class="string">"%d\n"</span>, abcdef);</span><br><span class="line"> <span class="built_in">log</span>(<span class="string">"hello world"</span>);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">"##"是使其停止展开</span></span><br><span class="line"><span class="comment">当遇到“#”或者“##”停止展开</span></span><br><span class="line"><span class="comment">“#”字符串化</span></span><br><span class="line"><span class="comment">“##”将其接到一起</span></span><br><span class="line"><span class="comment">*/</span></span><br></pre></td></tr></table></figure><p>![](./图片/g++ -E.png)</p><h4 id="第十节、字符串"><a href="#第十节、字符串" class="headerlink" title="第十节、字符串"></a>第十节、字符串</h4><p><img src="./%E5%9B%BE%E7%89%87/%E5%AD%97%E7%AC%A6%E4%B8%B2.png" alt=""></p><p><code>strlen(str[i]) == 0,</code>可以结束,因为’\0’ = 0<br><code>strcmp(str1, str2)</code><br>返回值 <code>> 0 ,< 0 ,= 0</code><br><code>strcpy(dest, src)</code><br><code>strncmp(str1, str2, n),</code> 最多比较n位<br><code>strncpy(str1, str2, n);</code> 最多拷贝n位</p><p><code>memset</code>是将其每一位字节初始化为c;<br><code>memset(str, - 1, sizeof(str));</code> 可以赋值为<code>-1</code></p><p><img src="./%E5%9B%BE%E7%89%87/sscanf&sprintf.png" alt=""></p><p>1.<code>sprintf</code>就是把格式化的数据写入到某个字符串中。返回值字符串的长度。<br> <code>sprintf(str, "%x", n)</code>将n以十六进制写入字符串<code>str</code>中</p><p>2.<code>sscanf</code>往一个变量赋值. </p><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iostream></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstdio></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cmath></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstring></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iomanip></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><algorithm></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><map></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><vector></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><inttypes.h></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">test_sscanf</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">char</span> str1[] = <span class="string">"123 456 789 987"</span>;</span><br><span class="line"> <span class="keyword">char</span> str2[<span class="number">100</span>];</span><br><span class="line"> <span class="keyword">int</span> a, b, c, d;</span><br><span class="line"> <span class="built_in">sscanf</span>(str1, <span class="string">"%d%[^$]s"</span>, &a, str1); <span class="comment">// "$"代表末尾 str1 = 456 789 987</span></span><br><span class="line"> <span class="built_in">sscanf</span>(str1, <span class="string">"%d%[^$]s"</span>, &b, str1);<span class="comment">//其每一次str1都在变,是剩余的一些 str1 = 789 987</span></span><br><span class="line"> <span class="built_in">sscanf</span>(str1, <span class="string">"%d%[^$]s"</span>, &c, str1); <span class="comment">// str1 = 987</span></span><br><span class="line"> <span class="built_in">sscanf</span>(str1, <span class="string">"%d%[^$]s"</span>, &d, str1);<span class="comment">// str1 = 987</span></span><br><span class="line"> <span class="comment">// sprintf(str2, "a = %db = %dc = %dd = %d", a, b, c, d);</span></span><br><span class="line"> <span class="keyword">int</span> offset = <span class="number">0</span>; <span class="comment">// offset代表偏移量</span></span><br><span class="line"> offset += <span class="built_in">sprintf</span>(str2 + offset, <span class="string">"a = %d"</span>, a); </span><br><span class="line"> offset += <span class="built_in">sprintf</span>(str2 + offset, <span class="string">"b = %d"</span>, b);</span><br><span class="line"> offset += <span class="built_in">sprintf</span>(str2 + offset, <span class="string">"c = %d"</span>, c);</span><br><span class="line"> offset += <span class="built_in">sprintf</span>(str2 + offset, <span class="string">"d = %d"</span>, d);</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%s\n"</span>, str2);</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"a = %d b = %dc = %dd = %d"</span>, a , b, c, d);</span><br><span class="line"> </span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="keyword">uint64_t</span> strlen1(<span class="keyword">const</span> <span class="keyword">char</span> *str) {</span><br><span class="line"> <span class="keyword">uint64_t</span> i = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">while</span>(str[i])i++;</span><br><span class="line"> <span class="keyword">return</span> i;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">strcmp1</span><span class="params">(<span class="keyword">const</span> <span class="keyword">char</span> *str1, <span class="keyword">const</span> <span class="keyword">char</span> *str2)</span> </span>{</span><br><span class="line"> <span class="keyword">int</span> i = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">for</span>(;str1[i]&&str2[i];i++) {</span><br><span class="line"> <span class="keyword">if</span>(str1[i] - str2[i]) <span class="keyword">return</span> str1[i] - str2[i];</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> str1[i] - str2[i];</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">int</span> n, w;</span><br><span class="line"> <span class="comment">/* char str[100];</span></span><br><span class="line"><span class="comment"> while(~scanf("%d", &n)) {</span></span><br><span class="line"><span class="comment"> w = sprintf(str,"%x", n);</span></span><br><span class="line"><span class="comment"> printf("%d\n",w);</span></span><br><span class="line"><span class="comment"> }*/</span></span><br><span class="line"> <span class="comment">//test_sscanf();</span></span><br><span class="line"> <span class="keyword">char</span> str[<span class="number">100</span>] = <span class="string">"hello haizei"</span>;</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%"</span> PRIu64 <span class="string">"\n"</span>, strlen1(str));</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%"</span> PRIu64 <span class="string">"\n"</span>, strlen1(<span class="string">"hello world"</span>));</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h4 id="十一、结构体与指针"><a href="#十一、结构体与指针" class="headerlink" title="十一、结构体与指针"></a>十一、结构体与指针</h4><p><strong>共用体:union</strong></p><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line">IP转整形:</span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iostream></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="keyword">union</span> IP {</span><br><span class="line"> <span class="keyword">unsigned</span> <span class="keyword">int</span> num;</span><br><span class="line"> <span class="class"><span class="keyword">struct</span> {</span></span><br><span class="line"> <span class="keyword">unsigned</span> <span class="keyword">char</span> a1;</span><br><span class="line"> <span class="keyword">unsigned</span> <span class="keyword">char</span> a2;</span><br><span class="line"> <span class="keyword">unsigned</span> <span class="keyword">char</span> a3;</span><br><span class="line"> <span class="keyword">unsigned</span> <span class="keyword">char</span> a4;</span><br><span class="line"> }ip;</span><br><span class="line">};</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{</span><br><span class="line"> IP p;</span><br><span class="line"> <span class="keyword">char</span> str[<span class="number">25</span>];</span><br><span class="line"> <span class="keyword">int</span> a, b ,c, d;</span><br><span class="line"> <span class="keyword">int</span> arr[<span class="number">4</span>];</span><br><span class="line"> <span class="keyword">while</span>(~<span class="built_in">scanf</span>(<span class="string">"%s"</span>, str)) {</span><br><span class="line"> <span class="built_in">sscanf</span>(str, <span class="string">"%d.%d.%d.%d"</span>, arr , arr + <span class="number">1</span>, arr + <span class="number">2</span>, arr + <span class="number">3</span> );</span><br><span class="line"> p.ip.a1 = arr[<span class="number">0</span>];</span><br><span class="line"> p.ip.a2 = arr[<span class="number">1</span>];</span><br><span class="line"> p.ip.a3 = arr[<span class="number">2</span>];</span><br><span class="line"> p.ip.a4 = arr[<span class="number">3</span>];</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d\n"</span>, p.num);</span><br><span class="line"></span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p>大端机和小端机</p><p><img src="./%E5%9B%BE%E7%89%87/%E5%B0%8F%E7%AB%AF%E6%9C%BA.png" alt=""></p><p><img src="./%E5%9B%BE%E7%89%87/xiaoduanji.png" alt=""></p><p>大端机:字节低位在高位</p><p>小端机:字节低位在低位</p><p><strong>指针:</strong></p><p><img src="./%E5%9B%BE%E7%89%87/%E6%8C%87%E9%92%88.png" alt=""></p><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// p[0] <=> a <=> *p</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iostream></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstdio></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cmath></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstring></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iomanip></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><algorithm></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><map></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><vector></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Data</span> {</span></span><br><span class="line"> <span class="keyword">int</span> x, y;</span><br><span class="line">};</span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Data</span> <span class="title">a</span>[2], *<span class="title">p</span> = <span class="title">a</span>;</span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{</span><br><span class="line"> a[<span class="number">1</span>].x = <span class="number">5</span>;</span><br><span class="line"> </span><br><span class="line"> <span class="comment">//地址用-></span></span><br><span class="line"> <span class="comment">//变量名用 . </span></span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d\n"</span>, a[<span class="number">1</span>].x);</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d\n"</span>, (p + <span class="number">1</span>)->x);</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d\n"</span>, (*(p + <span class="number">1</span>)).x);</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d\n"</span>, (&a[<span class="number">1</span>])->x);</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d\n"</span>, (a + <span class="number">1</span>)->x);</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%d\n"</span>, (p[<span class="number">1</span>]).x);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p>![](./图片/2019-12-14 20-12-06屏幕截图.png)</p><p>![2019-12-14 20-12-22屏幕截图](./图片/2019-12-14 20-12-22屏幕截图.png)</p><p>![2019-12-14 20-33-21屏幕截图](./图片/2019-12-14 20-33-21屏幕截图.png)</p><p>![2019-12-14 20-40-10屏幕截图](./图片/2019-12-14 20-40-10屏幕截图.png)</p><p>求地址的偏移量:</p><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">define</span> offset(T, a) ({\</span></span><br><span class="line"> T tmp;\</span><br><span class="line"> (<span class="keyword">char</span> *)&tmp.a - (<span class="keyword">char</span> *)&tmp;\</span><br><span class="line">})</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> offset(T, a) (long long)(&(((T *)(NULL))->a))</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Data</span> {</span></span><br><span class="line"> <span class="keyword">double</span> a;</span><br><span class="line"> <span class="keyword">int</span> b;</span><br><span class="line"> <span class="keyword">char</span> c;</span><br><span class="line">};</span><br><span class="line"></span><br><span class="line"><span class="built_in">printf</span>(<span class="string">"%ld\n"</span>, offset(struct Data, a));</span><br><span class="line"><span class="built_in">printf</span>(<span class="string">"%ld\n"</span>, offset(struct Data, b));</span><br><span class="line"><span class="built_in">printf</span>(<span class="string">"%ld\n"</span>, offset(struct Data, c));</span><br></pre></td></tr></table></figure><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iostream></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstdio></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cmath></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><cstring></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iomanip></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><algorithm></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><map></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><vector></span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> offset(T, item) (long long)(&(((T*)(NULL))->item))</span></span><br><span class="line"><span class="comment">//空地址</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> offset1(T, item) ({ \</span></span><br><span class="line"> T temp;\</span><br><span class="line"> (<span class="keyword">char</span> *)(&(temp.item)) - (<span class="keyword">char</span> *)(&temp); \</span><br><span class="line">})</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Data</span>{</span></span><br><span class="line"> <span class="keyword">char</span> a;</span><br><span class="line"> <span class="keyword">short</span> b;</span><br><span class="line"> <span class="keyword">char</span> c;</span><br><span class="line">};</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%lld\n"</span>, offset(struct Data, a));</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%lld\n"</span>, offset(struct Data, b));</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%lld\n"</span>, offset(struct Data, c));</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><stdio.h></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><stdlib.h></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><string.h></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> offset(T, a) (long long)(&(((T *)(NULL))->a))</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> pchar char *</span></span><br><span class="line"><span class="keyword">typedef</span> <span class="keyword">char</span> * ppchar;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Data</span> {</span></span><br><span class="line"> <span class="keyword">int</span> a;</span><br><span class="line"> <span class="keyword">double</span> b;</span><br><span class="line"> <span class="keyword">char</span> c;</span><br><span class="line">};</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">output_args</span><span class="params">(<span class="keyword">int</span> argc, <span class="keyword">char</span> *argv[], <span class="keyword">char</span> *env[])</span> </span>{</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i < argc; i++) {</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"argv[%d] = %s\n"</span>, i, argv[i]);</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; env[i]; i++) {</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"env[%d] = %s\n"</span>, i, env[i]);</span><br><span class="line"> <span class="keyword">if</span> (<span class="built_in">strncmp</span>(env[i], <span class="string">"USER="</span>, <span class="number">5</span>) == <span class="number">0</span>) {</span><br><span class="line"> <span class="keyword">if</span> (<span class="built_in">strcmp</span>(env[i] + <span class="number">5</span>, <span class="string">"huguang"</span>)) {</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"you are not CpatainHu, don't run me, Gun!"</span>);</span><br><span class="line"> <span class="built_in">exit</span>(<span class="number">0</span>);</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> ;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">(<span class="keyword">int</span> argc, <span class="keyword">char</span> *argv[], <span class="keyword">char</span> *env[])</span> </span>{</span><br><span class="line"> <span class="keyword">int</span> num1 = <span class="number">0x616263</span>;</span><br><span class="line"> <span class="keyword">int</span> num2 = <span class="number">0x61626364</span>;</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%s"</span>, (<span class="keyword">char</span> *)(&num2 + <span class="number">1</span>));</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%lld\n"</span>, offset(struct Data, a));</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%lld\n"</span>, offset(struct Data, b));</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%lld\n"</span>, offset(struct Data, c));</span><br><span class="line"> pchar p1, p2;</span><br><span class="line"> ppchar p3, p4;</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"sizeof(p1) = %lu sizeof(p2) = %lu\n"</span>, <span class="keyword">sizeof</span>(p1), <span class="keyword">sizeof</span>(p2));</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"sizeof(p3) = %lu sizeof(p4) = %lu\n"</span>, <span class="keyword">sizeof</span>(p3), <span class="keyword">sizeof</span>(p4));</span><br><span class="line"> output_args(argc, argv, env);</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><stdio.h></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><string.h></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><stdlib.h></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> offset(T, a) (long long)(&((T *)(NULL))->a)</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> pchar char *</span></span><br><span class="line"><span class="keyword">typedef</span> <span class="keyword">char</span> * ppchar;</span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Data</span> {</span></span><br><span class="line"> <span class="keyword">double</span> a;</span><br><span class="line"> <span class="keyword">int</span> b;</span><br><span class="line"> <span class="keyword">char</span> c;</span><br><span class="line">};</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">void output_main(int argc, char *argv[]) {</span></span><br><span class="line"><span class="comment"> for(int i = 0; i < argc; i++) {</span></span><br><span class="line"><span class="comment"> printf("argv[%d] = %s\n", i, argv[i]);</span></span><br><span class="line"><span class="comment"> }</span></span><br><span class="line"><span class="comment"> return;</span></span><br><span class="line"><span class="comment">}</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">output_main1</span><span class="params">(<span class="keyword">int</span> argc, <span class="keyword">char</span> *argv[], <span class="keyword">char</span> *env[])</span> </span>{</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; env[i]; i++) {</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"env[%d] = %s\n"</span>, i, env[i]);</span><br><span class="line"> <span class="keyword">if</span>(<span class="built_in">strncmp</span>(env[i], <span class="string">"USER="</span>, <span class="number">5</span>) == <span class="number">0</span>) {</span><br><span class="line"> <span class="keyword">if</span>(<span class="built_in">strncmp</span>(env[i] + <span class="number">5</span>, <span class="string">"fangsong"</span>, <span class="number">8</span>)) {</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"you are not user!\n"</span>);</span><br><span class="line"> <span class="built_in">exit</span>(<span class="number">0</span>);</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span>;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">(<span class="keyword">int</span> argc, <span class="keyword">char</span> *argv[], <span class="keyword">char</span> *env[])</span> </span>{</span><br><span class="line"> <span class="comment">// output_main(argc, argv);</span></span><br><span class="line"> <span class="keyword">int</span> num1 = <span class="number">0x616263</span>;</span><br><span class="line"> <span class="keyword">int</span> num2 = <span class="number">0x61626364</span>;</span><br><span class="line"> output_main1(argc, argv, env);</span><br><span class="line"> <span class="comment">/* printf("%s\n", (char *)(&num1));</span></span><br><span class="line"><span class="comment"> printf("%s\n", (char *)(&num2));*/</span></span><br><span class="line"> <span class="comment">// printf("%s\n", (char *)(&num2 + 1));</span></span><br><span class="line"> offset(struct Data, a);</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%lld\n"</span>, offset(struct Data, a));</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%lld\n"</span>, offset(struct Data, b));</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"%lld\n"</span>, offset(struct Data, c));</span><br><span class="line"> pchar p1, p2;</span><br><span class="line"> ppchar p3, p4;</span><br><span class="line"><span class="built_in">printf</span>(<span class="string">"sizeof(p1) = %lu , sizeof(p2) = %lu \n"</span>,<span class="keyword">sizeof</span>( p1) ,<span class="keyword">sizeof</span>( p2 ));</span><br><span class="line"><span class="built_in">printf</span>(<span class="string">"sizeof(p3) = %lu , sizeof(p4) = %lu \n"</span>,<span class="keyword">sizeof</span>( p3) ,<span class="keyword">sizeof</span>( p4 ));</span><br><span class="line"> </span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>]]></content>
<summary type="html">
<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" cla
</summary>
</entry>
</feed>