forked from EndlessCheng/codeforces-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
math.go
3475 lines (3158 loc) · 123 KB
/
math.go
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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package copypasta
import (
"math"
"math/big"
"math/bits"
"math/rand"
"slices"
"sort"
)
/* 数论 组合数学
鸽巢原理 抽屉原理
https://en.wikipedia.org/wiki/Pigeonhole_principle
https://codeforces.com/problemset/problem/1178/E
アルゴリズムと数学 演習問題集 https://atcoder.jp/contests/math-and-algorithm
一些不等式及其证明 https://www.luogu.com.cn/blog/chinesepikaync/oi-zhong-kuai-yong-dao-di-yi-suo-fou-deng-shi-ji-ji-zheng-ming
https://en.wikipedia.org/wiki/List_of_recreational_number_theory_topics
https://euler.stephan-brumme.com/toolbox/
a%-b == a%b
a < b 等价于 a ≤ b-1
a > b 等价于 a ≥ b+1
对于整数来说有
ax≤b => x≤⌊b/a⌋ ax<b => x<⌈b/a⌉
ax>b => x>⌊b/a⌋ ax≥b => x≥⌈b/a⌉
推论
x<<i ≤ s => x ≤ s>>i x<<i < s => x ≤ (s-1)>>i
x<<i > s => x > s>>i x<<i ≥ s => x > (s-1)>>i
1<<x ≤ v => x ≤ bits.Len(uint(v))-1 1<<x < v => x ≤ bits.Len(uint(v-1))-1
1<<x > v => x ≥ bits.Len(uint(v)) 1<<x ≥ v => x ≥ bits.Len(uint(v-1))
p<<x ≤ q
https://codeforces.com/problemset/problem/1883/E 1600
⌊⌊x/n⌋/m⌋ = ⌊x/(n*m)⌋
⌈⌈x/n⌉/m⌉ = ⌈x/(n*m)⌉
https://oeis.org/A257212 Least d>0 such that floor(n/d) - floor(n/(d+1)) <= 1
https://oeis.org/A257213 mex(n/i); Least d>0 such that floor(n/d) = floor(n/(d+1))
另见数论分块
基本不等式:总长为 x 的篱笆能围出来的最大面积是多少
AP: Sn = n*(2*a1+(n-1)*d)/2
GP: Sn = a1*(q^n-1)/(q-1), q!=1
= a1*n, q==1
∑i*q^(i-1) = n*q^n - (q^n-1)/(q-1)
若干无穷级数之和的公式 https://mathwords.net/mugenwa
∑^∞ r^i = 1/(1-r)
∑^∞ i*r^i = r/(1-r)^2
等幂和 Faulhaber's formula
https://zh.wikipedia.org/wiki/%E7%AD%89%E5%B9%82%E6%B1%82%E5%92%8C#%E4%B8%80%E8%88%AC%E6%95%B0%E5%88%97%E7%9A%84%E7%AD%89%E5%B9%82%E5%92%8C
1^2 + ... + n^2 = n*(n+1)*(2*n+1)/6
1^3 + ... + n^3 = [n(n+1)/2]^2
一元二次方程/不等式
https://codeforces.com/problemset/problem/1857/F
反比例函数
https://atcoder.jp/contests/arc158/tasks/arc158_b
调和级数(枚举倍数)
https://atcoder.jp/contests/abc089/tasks/abc089_d
长为 n 的数组的所有子数组的长度之和 n*(n+1)*(n+2)/6 https://oeis.org/A000292
长为 n 的数组的所有子数组的「长度/2下取整」之和
n 为偶数时:m=n/2, m*(m+1)*(4*m-1)/6 https://oeis.org/A002412
n 为奇数时:m=n/2, m*(m+1)*(4*m+5)/6 https://oeis.org/A016061
综合:m*(m+1)*(m*4+n%2*6-1)/6
- https://atcoder.jp/contests/abc290/tasks/abc290_e
处理绝对值·曼哈顿距离转切比雪夫距离
见 geometry.go
由 1~m 的排列组成的质数
https://oeis.org/A216444 List of primes with property that if they have d digits, these digits are a permutation of {1..d}
1423, 2143, 2341, 4231
1234657, 1245763, 1246537, ..., 7641253, 7642513, 7652413
https://oeis.org/A216444/b216444.txt 所有数据,共 538 个
N*N 的乘法表中有多少个不同数字?
https://oeis.org/A027424 Number of distinct products ij with 1 <= i, j <= n (number of distinct terms in n X n multiplication table)
https://mathoverflow.net/questions/31663/distinct-numbers-in-multiplication-table
勾股数 https://oeis.org/A008846
斜边 https://oeis.org/A004613 Numbers that are divisible only by primes congruent to 1 mod 4
https://en.wikipedia.org/wiki/Pythagorean_triple https://zh.wikipedia.org/wiki/%E5%8B%BE%E8%82%A1%E6%95%B0
https://oeis.org/A000079 2^n
虽然是个很普通的序列,但也能出现在一些意想不到的地方
例如,在该页面搜索 permutation 可以找到一些有趣的计数问题
a(n) is the number of permutations on [n+1] such that every initial segment is an interval of integers.(每个前缀都对应一段连续的整数)
Example: a(3) counts 1234, 2134, 2314, 2341, 3214, 3241, 3421, 4321.
The map "p -> ascents of p" is a bijection from these permutations to subsets of [n].
An ascent of a permutation p is a position i such that p(i) < p(i+1).
The permutations shown map to 123, 23, 13, 12, 3, 2, 1 and the empty set respectively.
相关题目 https://codeforces.com/problemset/problem/1515/E
https://oeis.org/A001787 n*2^(n-1) = ∑i*C(n,i) number of ones in binary numbers 1 to 111...1 (n bits)
https://oeis.org/A000337 ∑i*2^(i-1) = (n-1)*2^n+1
https://oeis.org/A036799 ∑i*2^i = (n-1)*2^(n+1)+2 = A000337(n)*2
https://oeis.org/A027992 a(n) = 2^n*(3n-1)+2 = The total sum of squares of parts in all compositions of n (做 https://codeforces.com/problemset/problem/235/B 时找到的序列)
https://oeis.org/A271638 a(n) = (13*n-36)*2^(n-1)+6*n+18 = The total sum of the cubes of all parts of all compositions of n
https://oeis.org/A014217 a(n) = floor(phi^n), where phi = (1+sqrt(5))/2 = 1.618...
a(n) = a(n-1) + 2*a(n-2) - a(n-3) - a(n-4)
证明 https://www.luogu.com.cn/discuss/show/318570
https://en.wikipedia.org/wiki/Faulhaber%27s_formula
https://oeis.org/A000330 平方和 = n*(n+1)*(2*n+1)/6
https://oeis.org/A000537 立方和 = (n*(n+1)/2)^2
https://oeis.org/A061168 ∑floor(log2(i)) = ∑(bits.Len(i)-1)
∑∑|ai-aj|
= 2*∑(i*ai-preSum(i-1)), i=[0,n-1], a 需要排序
https://www.luogu.com.cn/blog/DPair2005/solution-cf340c
https://codeforces.com/problemset/problem/340/C
https://oeis.org/A005326 Number of permutations p of (1,2,3,...,n) such that k and p(k) are relatively prime for all k in (1,2,3,...,n)
https://oeis.org/A009679 Number of partitions of {1, ..., 2n} into coprime pairs
https://oeis.org/A333885 Number of triples (i,j,k) with 1 <= i < j < k <= n such that i divides j divides k https://ac.nowcoder.com/acm/contest/7613/A
https://oeis.org/A000295 Eulerian numbers: Sum_{k=0..n} (n-k)*2^k = 2^n - n - 1
Number of permutations of {1,2,...,n} with exactly one descent
Number of partitions of an n-set having exactly one block of size > 1
a(n-1) is the number of subsets of {1..n} in which the largest element of the set exceeds by at least 2 the next largest element
For example, for n = 5, a(4) = 11 and the 11 sets are {1,3}, {1,4}, {1,5}, {2,4}, {2,5}, {3,5}, {1,2,4}, {1,2,5}, {1,3,5}, {2,3,5}, {1,2,3,5}
a(n-1) is also the number of subsets of {1..n} in which the second smallest element of the set exceeds by at least 2 the smallest element
For example, for n = 5, a(4) = 11 and the 11 sets are {1,3}, {1,4}, {1,5}, {2,4}, {2,5}, {3,5}, {1,3,4}, {1,3,5}, {1,4,5}, {2,4,5}, {1,3,4,5}
https://oeis.org/A064413 EKG sequence (or ECG sequence)
a(1) = 1; a(2) = 2; for n > 2, a(n) = smallest number not already used which shares a factor with a(n-1)
https://oeis.org/A002326 least m > 0 such that 2n+1 divides 2^m-1
LC1806 https://leetcode.cn/problems/minimum-number-of-operations-to-reinitialize-a-permutation/
https://oeis.org/A003136 Loeschian number: numbers of the form x^2 + xy + y^2
https://en.wikipedia.org/wiki/Loeschian_number
https://www.bilibili.com/video/BV1or4y1A76q
数的韧性 https://en.wikipedia.org/wiki/Persistence_of_a_number 乘法: https://oeis.org/A003001 加法: https://oeis.org/A006050
Smallest number h such that n*h is a repunit (111...1), or 0 if no such h exists
https://oeis.org/A190301 111...1
https://oeis.org/A216485 222...2
相关题目 https://atcoder.jp/contests/abc174/tasks/abc174_c 快速算法见 https://img.atcoder.jp/abc174/editorial.pdf
Least k such that the decimal representation of k*n contains only 1's and 0's
https://oeis.org/A079339
0's and d's (2~9): A096681-A096688
a(n) is the least value of k such that k*n uses only digits 1 and 2. a(n) = -1 if no such multiple exists
https://oeis.org/A216482
a(n) is the smallest positive number such that the decimal digits of n*a(n) are all 0, 1 or 2
https://oeis.org/A181061
Berlekamp–Massey algorithm
https://en.wikipedia.org/wiki/Berlekamp%E2%80%93Massey_algorithm
https://oi-wiki.org/math/berlekamp-massey/
椭圆曲线加密算法 https://ac.nowcoder.com/acm/contest/6916/C
Gaussian integer https://en.wikipedia.org/wiki/Gaussian_integer
Eisenstein integer https://en.wikipedia.org/wiki/Eisenstein_integer
Eisenstein prime https://en.wikipedia.org/wiki/Eisenstein_prime
https://oeis.org/A054710 Number of powers of 10 mod n https://codeforces.com/problemset/problem/1070/A
https://oeis.org/A050295 Number of strongly triple-free subsets of {1, 2, ..., n}
https://leetcode.cn/circle/discuss/QH0XWr/
https://oeis.org/A005245 The (Mahler-Popken) complexity of n: minimal number of 1's required to build n using + and *
3 log_3 n <= a(n) <= 3 log_2 n
https://oeis.org/A001108 a(n)-th triangular number is a square: a(n+1) = 6*a(n) - a(n-1) + 2, with a(0) = 0, a(1) = 1
https://oeis.org/A001109 a(n)^2 is a triangular number: a(n) = 6*a(n-1) - a(n-2) with a(0)=0, a(1)=1
https://oeis.org/A001110 Square triangular numbers: numbers that are both triangular and square
https://oeis.org/A034836 Number of ways to write n as n = x*y*z with 1 <= x <= y <= z
https://oeis.org/A331072 A034836 前缀和 O(n^(2/3))
https://atcoder.jp/contests/abc227/tasks/abc227_c
https://oeis.org/A244478 a(0)=2, a(1)=0, a(2)=2; thereafter a(n) = a(n-1-a(n-1))+a(n-2-a(n-2)) unless a(n-1) <= n-1 or a(n-2) <= n-2 in which case the sequence terminates
https://oeis.org/A244479
LC1140 https://leetcode.cn/problems/stone-game-ii/ 需要记忆化的 M 的上界
Collatz conjecture (3n+1) https://en.wikipedia.org/wiki/Collatz_conjecture
https://oeis.org/A006577 Number of halving and tripling steps to reach 1 in '3x+1' problem, or -1 if 1 is never reached
https://oeis.org/A008884 3x+1 sequence starting at 27
LC1387 https://leetcode.cn/problems/sort-integers-by-the-power-value/
Funny sum https://codeforces.com/blog/entry/125796?#comment-1116197
挑战 2.6 节练习题
2429 分解 LCM/GCD = a*b 且 gcd(a,b)=1 且 a+b 最小
1930 https://www.luogu.com.cn/problem/UVA10555 https://www.luogu.com.cn/problem/SP1166 floatToRat
3126 https://www.luogu.com.cn/problem/UVA12101 https://www.luogu.com.cn/problem/SP1841 BFS
3421 质因数幂次和 可重排列
3292 https://www.luogu.com.cn/problem/UVA11105 在 Z={4k+1} 上筛素数
3641 https://www.luogu.com.cn/problem/UVA11287 Carmichael Numbers https://oeis.org/A002997 https://en.wikipedia.org/wiki/Carmichael_number
4.1 节练习题(模运算的世界)
1150 https://www.luogu.com.cn/problem/UVA10212
1284
2115
3708
2720
GCJ Japan 2011 Final B
CF tag https://codeforces.com/problemset?order=BY_RATING_ASC&tags=number+theory
CF tag https://codeforces.com/problemset?order=BY_RATING_ASC&tags=combinatorics
*/
const mod = 1_000_000_007 // 998244353
// https://en.wikipedia.org/wiki/Exponentiation_by_squaring
// 已知 x + 1/x = k,计算 x^n + 1/x^n https://www.luogu.com.cn/problem/P9777
// 标准做法见 math.matrix.go
// 其它结论
// x^2n + 1/x^2n = (x^n + 1/x^n)^2 - 2
// x^(2n+1) + 1/x^(2n+1) = (x^n + 1/x^n) * (x^(n+1) + 1/x^(n+1)) - (x+1/x)
func pow(x, n int) int {
x %= mod
res := 1 % mod
for ; n > 0; n /= 2 {
if n%2 > 0 {
res = res * x % mod
}
x = x * x % mod
}
return res
}
func powM(x, n, p int) (res int) {
x %= p
res = 1 % p
for ; n > 0; n /= 2 {
if n%2 > 0 {
res = res * x % p
}
x = x * x % p
}
return
}
// 等比数列求和取模
// 返回 (x^0 + x^1 + ... + x^n) % mod
// https://atcoder.jp/contests/abc293/tasks/abc293_e
func gp(x, n int) int {
if n == 0 {
return 1 % mod
}
res := (1 + pow(x, (n+1)/2)) * gp(x, (n-1)/2)
if n%2 == 0 {
res += pow(x, n)
}
return res % mod
}
// 适用于 mod 超过 int32 范围的情况
// 还有一种用浮点数的写法,此略
func mul(a, b int) (res int) {
for ; b > 0; b /= 2 {
if b%2 > 0 {
res = (res + a) % mod
}
a = (a + a) % mod
}
return
}
func _(abs func(int) int) {
/* GCD LCM 相关
https://mathworld.wolfram.com/EuclideanAlgorithm.html
https://en.wikipedia.org/wiki/Euclidean_algorithm
https://stackoverflow.com/questions/3980416/time-complexity-of-euclids-algorithm
GCD 卷积(GCD Convolution)
https://codeforces.com/blog/entry/112346
https://judge.yosupo.jp/problem/gcd_convolution
https://atcoder.jp/contests/agc038/tasks/agc038_c
https://codeforces.com/gym/103688/problem/E
https://codeforces.com/problemset/problem/1884/D
https://ac.nowcoder.com/acm/contest/73854/G
https://oeis.org/A051010 Triangle T(m,n) giving of number of steps in the Euclidean algorithm for gcd(m,n) with 0<=m<n
https://oeis.org/A034883 Maximum length of Euclidean algorithm starting with n and any nonnegative i<n
https://oeis.org/A049826 GCD(n,i) 的迭代次数之和,O(nlogn)
Tighter time complexity for GCD https://codeforces.com/blog/entry/63771
Runtime of finding the GCD of an array https://codeforces.com/blog/entry/92720
更相减损术
GCD(x,y) = GCD(x,y-x) x<=y
https://codeforces.com/problemset/problem/1458/A
GCD 套路:枚举倍数(调和级数复杂度)
GCD(x,x+y) = GCD(x,y) https://codeforces.com/problemset/problem/1110/C
GCD 与质因子 https://codeforces.com/problemset/problem/264/B
数组中最小的 LCM(ai,aj) https://codeforces.com/problemset/problem/1154/G
分拆与 LCM https://ac.nowcoder.com/acm/contest/5961/D https://ac.nowcoder.com/discuss/439005
https://codeforces.com/problemset/problem/1736/B 1200
TIPS: 一般 LCM 的题目都需要用 LCM=x*y/GCD 转换成研究 GCD 的性质
todo https://atcoder.jp/contests/abc162/tasks/abc162_e
https://atcoder.jp/contests/abc206/tasks/abc206_e
todo 基于值域预处理的快速 GCD https://www.luogu.com.cn/problem/P5435
GCD = 1 的子序列个数 https://codeforces.com/problemset/problem/803/F https://ac.nowcoder.com/acm/problem/112055
见后面的 mu
a 中任意两数互质 <=> 每个质数至多整除一个 a[i]
https://codeforces.com/contest/1770/problem/C
todo https://codeforces.com/contest/1462/problem/D 的 O(nlogn) 解法
Frobenius problem / Coin problem / Chicken McNugget Theorem
两种硬币面额为 a 和 b,互质,数量无限,所不能凑出的数值的最大值为 a*b-a-b
https://artofproblemsolving.com/wiki/index.php/Chicken_McNugget_Theorem
https://en.wikipedia.org/wiki/Coin_problem
https://www.luogu.com.cn/problem/P3951
https://codeforces.com/contest/1526/problem/B
- [2979. 最贵的无法购买的商品](https://leetcode.cn/problems/most-expensive-item-that-can-not-be-bought/)(会员题)
*/
gcd := func(a, b int) int {
for a != 0 {
a, b = b%a, a
}
return b
}
lcm := func(a, b int) int { return a / gcd(a, b) * b }
// 前 n 个数的 LCM https://oeis.org/A003418 a(n) = lcm(1,...,n) ~ exp(n)
// 相关题目 https://atcoder.jp/contests/arc110/tasks/arc110_a
// https://codeforces.com/problemset/problem/1485/D
// https://codeforces.com/problemset/problem/1542/C
// https://codeforces.com/problemset/problem/1603/A
// a(n)/a(n-1) = https://oeis.org/A014963
// 前缀和 https://oeis.org/A072107 https://ac.nowcoder.com/acm/contest/7607/A
// LCM(2, 4, 6, ..., 2n) https://oeis.org/A051426
// Mangoldt Function https://mathworld.wolfram.com/MangoldtFunction.html
// a(n) 的因子个数 d(lcm(1,...,n)) https://oeis.org/A056793
// 这同时也是 1~n 的子集的 LCM 的种类数
// 另一种通分:「排水系统」的另一种解法 https://zxshetzy.blog.luogu.org/ling-yi-zhong-tong-fen
// https://oeis.org/A000793 Landau's function g(n): largest order of permutation of n elements
// Equivalently, largest LCM of partitions of n
lcms := []int{
0, 1, 2, 6, 12, 60, 60, 420, 840, 2520, 2520, // 10
27720, 27720, 360360, 360360, 360360, 720720, 12252240, 12252240, 232792560, 232792560, // 20
232792560, 232792560, // 22 (int32)
5354228880, 5354228880, 26771144400, 26771144400, 80313433200, 80313433200, 2329089562800, 2329089562800, // 30
72201776446800, 144403552893600, 144403552893600, 144403552893600, 144403552893600, 144403552893600, 5342931457063200, 5342931457063200, 5342931457063200, 5342931457063200, // 40
219060189739591200, 219060189739591200, // 9419588158802421600,
}
// GCD 性质统计相关
// NOTE: 对于一任意非负序列,前 i 个数的 GCD 是非增序列,且至多有 O(logMax) 个不同值
// 应用:https://codeforces.com/problemset/problem/1210/C
// #{(a,b) | 1<=a<=b<=n, gcd(a,b)=1} https://oeis.org/A002088
// = ∑phi(i)
// #{(a,b) | 1<=a,b<=n, gcd(a,b)=1} https://oeis.org/A018805
// = 2*(∑phi(i))-1
// = 2*A002088(n)-1
// #{(a,b) | 1<=a,b<=n, gcd(a,b) is prime} todo https://www.luogu.com.cn/problem/P2568
// #{(a,b,c) | 1<=a,b,c<=n, gcd(a,b,c)=1} https://oeis.org/A071778
// = ∑mu(i)*floor(n/i)^3
// #{(a,b,c,d) | 1<=a,b,c,d<=n, gcd(a,b,c,d)=1} https://oeis.org/A082540
// = ∑mu(i)*floor(n/i)^4
// 证明见后面【莫比乌斯反演】
// GCD 求和相关
// 证明需要用到莫比乌斯函数,见后面的【莫比乌斯反演】附近的小技巧
// ∑gcd(n,i) = ∑{d|n}d*phi(n/d) https://oeis.org/A018804 https://www.luogu.com.cn/problem/P2303
// 更简化的公式见小粉兔博客 https://www.cnblogs.com/PinkRabbit/p/8278728.html
// ∑n/gcd(n,i) = ∑{d|n}d*phi(d) https://oeis.org/A057660
// ∑∑gcd(i,j) = ∑phi(i)*(floor(n/i))^2 https://oeis.org/A018806 https://www.luogu.com.cn/problem/P2398
// ∑∑gcd(i,j) j<=i = (1/2)∑phi(i)*floor(n/i)*(floor(n/i)+1) https://oeis.org/A272718
// ∑∑gcd(i,j) j<i = (A018806(n) - n*(n+1)/2) / 2 https://oeis.org/A178881
// https://www.luogu.com.cn/problem/P1390
// 训练指南例题 2-9,UVa11426 https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=26&page=show_problem&problem=2421
// ∑∑∑gcd(i,j,k) = ∑phi(i)*(floor(n/i))^3 https://ac.nowcoder.com/acm/contest/7608/B
// 证明见后面【莫比乌斯反演】
// LCM 性质统计相关
// https://oeis.org/A048691 #{(a,b) | lcm(a,b)=n},等价于 #{(x,y) | x|n, y|n, gcd(x,y)=1}
// = d(n^2)
// = (2*e1+1)(2*e2+1)...(2*ek+1), 其中 ei 是 n 的质因子分解中第 i 个质数的幂次
// https://oeis.org/A018892 #{(a,b) | a<=b, lcm(a,b)=n},等价于 #{(x,y) | x|n, y|n, x<=y, gcd(x,y)=1}
// = (d(n^2)+1)/2
// = ((2*e1+1)(2*e2+1)...(2*ek+1) + 1) / 2, 其中 ei 是 n 的质因子分解中第 i 个质数的幂次
// Number of ways to write 1/n as a sum of exactly 2 unit fractions
// Number of divisors of n^2 less than or equal to n
// 训练指南 2.10 习题,UVa10892 https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=20&page=show_problem&problem=1833
// https://oeis.org/A182082 A018892 的前缀和
// https://projecteuler.net/problem=379
// https://zhenweiliu.gitee.io/blog/2019/08/05/Project-Euler-Problem-379-Least-common-multiple-count/
// LCM 求和相关
// ∑lcm(n,i) = n*(1+∑{d|n}d*phi(d))/2 = n*(1+A057660(n))/2 https://oeis.org/A051193
// ∑lcm(n,i)/n = A051193(n)/n = (1+∑{d|n}d*phi(d))/2 = (1+A057660(n))/2 https://oeis.org/A057661
// ∑∑lcm(i,j) https://oeis.org/A064951
// 统计数组的所有子区间的 GCD 的不同个数
// 代码和题目见 bits.go 中的 bitOpTrick
// 统计数组的所有子序列的 GCD 的不同个数,复杂度 O(Clog^2C)
// LC1819 https://leetcode.cn/problems/number-of-different-subsequences-gcds/
// 我的题解 https://leetcode.cn/problems/number-of-different-subsequences-gcds/solution/ji-bai-100mei-ju-gcdxun-huan-you-hua-pyt-get7/
countDifferentSubsequenceGCDs := func(a []int) (ans int) {
const mx int = 4e5 //
has := [mx + 1]bool{}
for _, v := range a {
has[v] = true
}
for i := 1; i <= mx; i++ {
g := 0
for j := i; j <= mx && g != i; j += i { // 枚举 i 的倍数 j
if has[j] { // 如果 j 在 nums 中
g = gcd(g, j) // 更新最大公约数
}
}
if g == i { // 找到一个答案
ans++
}
}
return
}
// 最简分数
// https://codeforces.com/problemset/problem/1468/F
type frac struct{ num, den int }
// 如果有负数需要对 g 取绝对值
makeFrac := func(a, b int) frac { g := gcd(a, b); return frac{a / g, b / g} }
// 比较两个(最简化后的)frac
// 不使用高精度、浮点数等
// 核心思路是将 a b 写成连分数形式,逐个比较
// 复杂度 O(log)
lessFrac := func(a, b frac) bool {
// 如果保证 a b 均为正数,for 前面的这些 if 可以去掉
if a == b {
return false
}
if a.num == 0 {
return b.num > 0
}
if b.num == 0 {
return a.num < 0
}
if a.num > 0 != (b.num > 0) {
return a.num < b.num
}
if a.num < 0 { // b.num < 0
a, b = frac{-b.num, b.den}, frac{-a.num, a.den}
}
for {
if a.den == 0 {
return false
}
if b.den == 0 {
return true
}
da, db := a.num/a.den, b.num/b.den
if da != db {
return da < db
}
a, b = frac{b.den, b.num - db*b.den}, frac{a.den, a.num - da*a.den}
}
}
// 类欧几里得算法
// ∑⌊(ai+b)/m⌋, i in [0,n-1]
// https://oi-wiki.org/math/euclidean/
// todo https://www.luogu.com.cn/blog/AlanWalkerWilson/Akin-Euclidean-algorithm-Basis
// https://www.luogu.com.cn/blog/Shuchong/qian-tan-lei-ou-ji-li-dei-suan-fa
// 万能欧几里得算法 https://www.luogu.com.cn/blog/ILikeDuck/mo-neng-ou-ji-li-dei-suan-fa
//
// 模板题 https://atcoder.jp/contests/practice2/tasks/practice2_c
// https://www.luogu.com.cn/problem/P5170
// https://loj.ac/p/138
// todo https://codeforces.com/problemset/problem/1182/F
// https://codeforces.com/problemset/problem/1098/E
floorSum := func(n, m, a, b int) (res int) {
if a < 0 {
a2 := a%m + m
res -= n * (n - 1) / 2 * ((a2 - a) / m)
a = a2
}
if b < 0 {
b2 := b%m + m
res -= n * ((b2 - b) / m)
b = b2
}
for {
if a >= m {
res += n * (n - 1) / 2 * (a / m)
a %= m
}
if b >= m {
res += n * (b / m)
b %= m
}
yMax := a*n + b
if yMax < m {
break
}
n = yMax / m
b = yMax % m
m, a = a, m
}
return
}
sqCheck := func(a int) bool { r := int(math.Round(math.Sqrt(float64(a)))); return r*r == a }
cubeCheck := func(a int) bool { r := int(math.Round(math.Cbrt(float64(a)))); return r*r*r == a }
// 平方数开平方
sqrt := func(a int) int {
r := int(math.Round(math.Sqrt(float64(a))))
if r*r == a {
return r
}
return -1
}
// 立方数开立方
cbrt := func(a int) int {
r := int(math.Round(math.Cbrt(float64(a))))
if r*r*r == a {
return r
}
return -1
}
// 返回差分表的最后一个数
// return the bottom entry in the difference table
// 另一种做法是用公式 ∑(-1)^i * C(n,i) * a_i, i=0..n-1
bottomDiff := func(a []int) int {
for ; len(a) > 1; a = a[:len(a)-1] {
for i := 0; i+1 < len(a); i++ {
a[i] = a[i+1] - a[i]
}
}
return a[0]
}
/* 质数 质因数分解 */
// n/2^k https://oeis.org/A000265
// A000265 的前缀和 https://oeis.org/A135013
// a(n) = Sum_{k>=1} (round(n/2^k))^2
// 质数表 https://oeis.org/A000040
// primes[i]%10 https://oeis.org/A007652
// 10-primes[i]%10 https://oeis.org/A072003
// p-1 https://oeis.org/A006093
// p+1 https://oeis.org/A008864
// p^2+p+1 https://oeis.org/A060800 = sigma(p^2)
// prime index prime https://oeis.org/A006450
primes := []int{ // 预处理 mask 的见下
2, 3, 5, 7, 11,
13, 17, 19, 23, 29,
31, 37, 41, 43, 47,
53, 59, 61, 67, 71,
73, 79, 83, 89, 97,
101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199,
211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293,
307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397,
401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499,
503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599,
601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691,
701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797,
809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887,
907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997, /* #=168 */
1009, 1013, 1019, 1021, 1031, 1033, 1039, 1049, 1051, 1061, 1063, 1069, 1087, 1091, 1093, 1097,
1103, 1109, 1117, 1123, 1129, 1151, 1153, 1163, 1171, 1181, 1187, 1193,
1201, 1213, 1217, 1223, 1229, 1231, 1237, 1249, 1259, 1277, 1279, 1283, 1289, 1291, 1297,
1301, 1303, 1307, 1319, 1321, 1327, 1361, 1367, 1373, 1381, 1399,
1409, 1423, 1427, 1429, 1433, 1439, 1447, 1451, 1453, 1459, 1471, 1481, 1483, 1487, 1489, 1493, 1499,
1511, 1523, 1531, 1543, 1549, 1553, 1559, 1567, 1571, 1579, 1583, 1597,
1601, 1607, 1609, 1613, 1619, 1621, 1627, 1637, 1657, 1663, 1667, 1669, 1693, 1697, 1699,
1709, 1721, 1723, 1733, 1741, 1747, 1753, 1759, 1777, 1783, 1787, 1789,
1801, 1811, 1823, 1831, 1847, 1861, 1867, 1871, 1873, 1877, 1879, 1889,
1901, 1907, 1913, 1931, 1933, 1949, 1951, 1973, 1979, 1987, 1993, 1997, 1999, /* #=303 */
}
{
// 小范围质数状压
// Squarefree numbers https://oeis.org/A005117
const mx = 30
primeMask := [mx + 1]int{}
for i := 2; i <= mx; i++ {
for j, p := range primes {
if i%p == 0 {
//if i%(p*p) == 0 { primeMask[i] = -1; break } // 有平方因子
primeMask[i] |= 1 << j // 把 j 加到集合中
}
}
// 只保留奇数次数质因子的写法
// https://codeforces.com/problemset/problem/895/C 2000
x := i
for j, p := range primes {
for ; x%p == 0; x /= p {
primeMask[i] ^= 1 << j
}
}
}
}
// 第 10^k 个素数
// https://oeis.org/A006988
// 补充:第 1e5, 2e5, 3e5, ..., 1e6 个素数
// 1299709, 2750159, 4256233, 5800079, 7368787, 8960453, 10570841, 12195257, 13834103, 15485863
primes10k := []int{
2, 29, 541, 7919, // k=3
104729, 1299709, 15485863, // k=6
179424673, 2038074743, 22801763489, // k=9
252097800623, 2760727302517, 29996224275833, // k=12
323780508946331, 3475385758524527, 37124508045065437, // k=15
394906913903735329, 4185296581467695669,
}
// map{小于 10^n 的素数个数: 小于 10^n 的最大素数} https://oeis.org/A006880 https://oeis.org/A003618 10^n-a(n): https://oeis.org/A033874
primes10 := map[int]int{
4: 7,
25: 97,
168: 997, // 1e3
1229: 9973,
9592: 99991,
78498: 999983, // 1e6
664579: 9999991,
5761455: 99999989,
50847534: 999999937, // 1e9
455052511: 9999999967,
}
// 大于 10^n 的最小素数 https://oeis.org/A090226 https://oeis.org/A003617 a(n)-10^n: https://oeis.org/A033873
primes10_ := []int{
2,
11,
101,
1009, // 1e3
10007,
100003,
1000003, // 1e6
10000019,
100000007,
1000000007, // 1e9
10000000019,
100000000003,
1000000000039, // 1e12
10000000000037,
100000000000031,
1000000000000037, // 1e15
10000000000000061,
100000000000000003,
1000000000000000003, // 1e18
//10000000000000000051,
}
/* 质数性质统计相关
Counting primes
https://en.wikipedia.org/wiki/Meissel%E2%80%93Lehmer_algorithm
https://oi-wiki.org/math/meissel-lehmer/
https://www.zhihu.com/question/29580448
O(n^(2/3)log^(1/3)(n)) https://codeforces.com/blog/entry/91632
质数的幂次组成的集合 {p^k} https://oeis.org/A000961
补集 https://oeis.org/A024619
Exponential of Mangoldt function https://oeis.org/A014963
质数前缀和 https://oeis.org/A007504
a(n) ~ n^2 * log(n) / 2
a(n)^2 - a(n-1)^2 = A034960(n)
EXTRA: divide odd numbers into groups with prime(n) elements and add together https://oeis.org/A034960
仍然是质数的前缀和 https://oeis.org/A013918 对应的前缀和下标 https://oeis.org/A013916
交替和 https://oeis.org/A008347
质数前缀积 prime(n)# https://oeis.org/A002110
the least number with n distinct prime factors
2, 6, 30, 210, 2310, 30030, 510510, 9699690, 223092870, /9/
6469693230, 200560490130, 7420738134810, 304250263527210, 13082761331670030, 614889782588491410
质数间隙 prime gap https://en.wikipedia.org/wiki/Prime_gap https://oeis.org/A001223
Positions of records https://oeis.org/A002386 https://oeis.org/A005669
Values of records https://oeis.org/A005250
Gap 均值 https://oeis.org/A286888 a(n)= floor((prime(n) - 2)/(n - 1))
相关题目 https://www.luogu.com.cn/problem/P6104 https://class.luogu.com.cn/classroom/lgr69
Kick Start 2021 Round B Consecutive Primes https://codingcompetitions.withgoogle.com/kickstart/round/0000000000435a5b/000000000077a8e6
Numbers whose distance to the closest prime number is a prime number https://oeis.org/A160666
孪生素数 https://en.wikipedia.org/wiki/Twin_prime https://oeis.org/A001359 https://oeis.org/A006512 https://oeis.org/A077800
https://oeis.org/A113274 Record gaps between twin primes
Upper bound: gaps between twin primes are smaller than 0.76*(log p)^3, where p is the prime at the end of the gap.
https://oeis.org/A113275 Lesser of twin primes for which the gap before the following twin primes is a record
Prime k-tuple https://en.wikipedia.org/wiki/Prime_k-tuple
Prime constellations / diameter https://en.wikipedia.org/wiki/Prime_k-tuple#Prime_constellations https://oeis.org/A008407
Cousin prime https://en.wikipedia.org/wiki/Cousin_prime https://oeis.org/A023200
Sexy prime https://en.wikipedia.org/wiki/Sexy_prime https://oeis.org/A023201
Prime triplet https://en.wikipedia.org/wiki/Prime_triplet https://oeis.org/A098420
Primes in arithmetic progression https://en.wikipedia.org/wiki/Primes_in_arithmetic_progression
First Hardy–Littlewood conjecture https://en.wikipedia.org/wiki/First_Hardy%E2%80%93Littlewood_conjecture
Second Hardy–Littlewood conjecture https://en.wikipedia.org/wiki/Second_Hardy%E2%80%93Littlewood_conjecture 哈代-李特尔伍德第二猜想
https://oeis.org/A007918 Least prime >= n (version 1 of the "next prime" function)
https://oeis.org/A007920 Smallest number k such that n + k is prime
任意质数之差 https://oeis.org/A030173
非任意质数之差 https://oeis.org/A007921
质数的逆二项变换 Inverse binomial transform of primes https://oeis.org/A007442
合数前缀和 https://oeis.org/A053767
合数前缀积 Compositorial number https://oeis.org/A036691
不与质数相邻的合数 https://oeis.org/A079364
半素数 https://oeis.org/A001358 也叫双素数/二次殆素数 Semiprimes (or biprimes): products of two primes
https://en.wikipedia.org/wiki/Semiprime
https://en.wikipedia.org/wiki/Almost_prime
非平方半素数 https://oeis.org/A006881 Squarefree semiprimes: Numbers that are the product of two distinct primes.
绝对素数 https://oeis.org/A003459 各位数字可以任意交换位置,其结果仍为素数
https://en.wikipedia.org/wiki/Permutable_prime
哥德巴赫猜想:大于 2 的偶数,都可表示成两个素数之和。
偶数分拆的最小质数 Goldbach’s conjecture https://oeis.org/A020481
Conjecture: a(n) ~ O(√n)
https://en.wikipedia.org/wiki/Goldbach%27s_conjecture
Positions of records https://oeis.org/A025018
Values of records https://oeis.org/A025019
1e9 内最大的为 a(721013438) = 1789
2e9 内最大的为 a(1847133842) = 1861
https://codeforces.com/problemset/problem/735/D
将 1~n 这 n 个数分成若干组,使每组数之和为质数 https://codeforces.com/problemset/problem/45/G
这题需要用到 a(n) ~ O(√n)
勒让德猜想 - 在两个相邻平方数之间,至少有一个质数 Legendre’s conjecture
https://en.wikipedia.org/wiki/Legendre%27s_conjecture
Number of primes between n^2 and (n+1)^2 https://oeis.org/A014085
Number of primes between n^3 and (n+1)^3 https://oeis.org/A060199
伯特兰-切比雪夫定理 - n ~ 2n 之间至少有一个质数 Bertrand's postulate
https://en.wikipedia.org/wiki/Bertrand%27s_postulate
Number of primes between n and 2n (inclusive) https://oeis.org/A035250
Number of primes between n and 2n exclusive https://oeis.org/A060715
n ~ 1.5n https://codeforces.com/contest/1178/problem/D
Least k such that H(k) > n, where H(k) is the harmonic number ∑{i=1..k} 1/i
https://oeis.org/A002387
https://oeis.org/A004080
a(n) = smallest prime p such that ∑{primes q = 2, ..., p} 1/q exceeds n
5, 277, 5_195_977, 1801241230056600523
https://oeis.org/A016088 pi
https://oeis.org/A046024 i
a(n) = largest m such that the harmonic number H(m)= ∑{i=1..m} 1/i is < n
https://oeis.org/A115515
a(n) = largest prime p such that ∑{primes q = 2, ..., p} 1/q does not exceed n
3, 271, 5_195_969, 1801241230056600467
https://oeis.org/A223037
Exponent of highest power of 2 dividing n, a.k.a. the binary carry sequence, the ruler sequence, or the 2-adic valuation of n
a(n) = 0 if n is odd, otherwise 1 + a(n/2)
https://oeis.org/A007814
https://oeis.org/A000043 Mersenne exponents: primes p such that 2^p - 1 is prime. Then 2^p - 1 is called a Mersenne prime
*/
// 判断一个数是否为质数
isPrime := func(n int) bool {
if n < 2 || n >= 4 && n%2 == 0 {
return false
}
for i := 2; i*i <= n; i++ {
if n%i == 0 {
return false
}
}
return true
}
// https://www.luogu.com.cn/problem/U82118
isPrime = func(n int) bool { return big.NewInt(int64(n)).ProbablyPrime(0) }
// 判断质数+求最大质因子
// 先用 Pollard-Rho 算法求出一个因子,然后递归求最大质因子
// https://zhuanlan.zhihu.com/p/267884783
// https://www.luogu.com.cn/problem/P4718
pollardRho := func(n int) int {
if n == 4 {
return 2
}
if isPrime(n) {
return n
}
mul := func(a, b int) (res int) {
for ; b > 0; b >>= 1 {
if b&1 == 1 {
res = (res + a) % n
}
a = (a + a) % n
}
return
}
for {
c := 1 + rand.Intn(n-1)
f := func(x int) int { return (mul(x, x) + c) % n }
for t, r := f(0), f(f(0)); t != r; t, r = f(t), f(f(r)) {
if d := gcd(abs(t-r), n); d > 1 {
return d
}
}
}
}
{
cacheGPF := map[int]int{}
var gpf func(int) int
gpf = func(x int) (res int) {
if cacheGPF[x] > 0 {
return cacheGPF[x]
}
defer func() { cacheGPF[x] = res }()
p := pollardRho(x)
if p == x {
return p
}
return max(gpf(p), gpf(x/p))
}
}
/* 题单:预处理质数
- [204. 计数质数](https://leetcode.cn/problems/count-primes/)
- [2761. 和等于目标值的质数对](https://leetcode.cn/problems/prime-pairs-with-target-sum/) 1505
- [2523. 范围内最接近的两个质数](https://leetcode.cn/problems/closest-prime-numbers-in-range/) 1650
- [2601. 质数减法运算](https://leetcode.cn/problems/prime-subtraction-operation/) 1779
- [2867. 统计树中的合法路径数目](https://leetcode.cn/problems/count-valid-paths-in-a-tree/) 2428
*/
// 预处理: [2,mx] 范围内的质数
// 埃筛 埃氏筛 埃拉托斯特尼筛法 Sieve of Eratosthenes
// 该算法也说明了:前 n 个数的平均质因子数量是 O(loglogn) 级别的
// https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
// https://oeis.org/A055399 Number of stages of sieve of Eratosthenes needed to identify n as prime or composite
// https://oeis.org/A230773 Minimum number of steps in an alternate definition of the Sieve of Eratosthenes needed to identify n as prime or composite
// 质数个数 π(n) https://oeis.org/A000720
// π(10^n) https://oeis.org/A006880
// 4, 25, 168, 1229, 9592, 78498, 664579, 5761455, 50847534, /* 1e9 */
// 455052511, 4118054813, 37607912018, 346065536839, 3204941750802, 29844570422669, 279238341033925, 2623557157654233, 24739954287740860, 234057667276344607,
// 思想应用 https://codeforces.com/contest/1646/problem/E
// https://codeforces.com/problemset/problem/576/A
sieve := func() {
const mx int = 1e6
primes := []int{}
pid := [mx + 1]int{-1, -1}
for i := 2; i <= mx; i++ {
if pid[i] == 0 {
primes = append(primes, i)
pid[i] = len(primes)
for j := i * i; j <= mx; j += i {
pid[j] = -1
}
}
}
// 预处理质数后,可以用 O(√x/logx) 的时间分解质因子 factorizeFast
// 预处理 sqrt(mx) 以内的质数
// https://codeforces.com/problemset/problem/1771/C 1600
// https://www.lanqiao.cn/problems/6281/learning/?contest_id=146
primeDivisors := func(x int) (ps []int) {
// 如果超时,改成 int32 试试
for _, p := range primes {
//if x == 1 {
// break
//}
if x%p > 0 {
continue
}
//e := 1
for x /= p; x%p == 0; x /= p {
//e++
}
ps = append(ps, p)
}
if x > 1 {
//e := 1
ps = append(ps, x)
}
return
}
_ = primeDivisors
// 或者,只是单纯想标记一下
np := [mx + 1]bool{true, true}
for i := 2; i*i <= mx; i++ {
if !np[i] {
for j := i * i; j <= mx; j += i {
np[j] = true
}
}
}
// EXTRA: pi(n), the number of primes <= n https://oeis.org/A000720
pi := [mx + 1]int{}
for i := 2; i <= mx; i++ {
pi[i] = pi[i-1]
if pid[i] > 0 {
pi[i]++
}
}
}
// 线筛 线性筛 欧拉筛
// 每个合数都从其 LPF 标记到(在遍历到 i = 合数/LPF 的时候,标记这些合数)
// 参考 https://oi-wiki.org/math/sieve/ 以及进阶指南 pp.136-137
// mx = 3e7 时比埃氏筛大约快 100ms https://codeforces.com/problemset/submission/986/206447142
// https://codeforces.com/problemset/submission/986/206445786
// https://www.luogu.com.cn/problem/P3383
sieveEuler := func() {
const mx int = 1e7
primes := []int{}
pid := [mx + 1]int{-1, -1}
for i := 2; i <= mx; i++ {
if pid[i] == 0 {
pid[i] = len(primes) + 1
primes = append(primes, i)
}
for _, p := range primes {
if p*i > mx {
break
}
pid[p*i] = -1
if i%p == 0 { // 后面的「质数*i」标记出的合数,其 LPF 不是该质数,应及时退出,从而避免重复标记
break
}
}
}
}
// 一般线性筛的模板
// 记 f(n) 为积性函数
// 其满足 1. f(p) = p ...
// 2. f(p^(k+1)) = f(p^k) ... p
// 3. f(x*p) = f(x) ... p (p 不是 x 的因子)
// 一个典型的例子见下面 σ(n) 的线性筛求法
// https://codeforces.com/contest/1512/problem/G
// https://codeforces.com/gym/103107/problem/F
// i^N 的异或和 https://ac.nowcoder.com/acm/problem/112055
//
// 一个有用的性质:积性函数的和函数也是积性函数
// https://codeforces.com/problemset/problem/757/E 2500
sieveEulerTemplate := func() []int {
const mx int = 1e7
f := make([]int, mx+1)
f[1] = 1 //
vis := make([]bool, mx+1)
primes := []int{}
for i := 2; i <= mx; i++ {
if !vis[i] {
// 1: p
f[i] = i
primes = append(primes, i)
}
for _, p := range primes {
v := p * i
if v > mx {
break
}
vis[v] = true
if i%p == 0 {
// 2: p^(k+1) <- p^k
f[v] = f[i] * p
break
}
// 3: x*p <- x 且 x 的质因子是没有 p 的
f[v] = f[i] * p
}
}
return f