forked from EndlessCheng/codeforces-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmath.go
2516 lines (2266 loc) · 88.3 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 (
. "fmt"
"math"
"math/big"
"math/bits"
"math/rand"
"sort"
)
/* 数论 组合数学 博弈论 积分 插值 随机算法
一些不等式及其证明 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/
todo 期望与贡献 https://codeforces.com/blog/entry/62690 https://codeforces.com/blog/entry/62792
一类概率期望问题的杀器:势函数和鞅的停时定理 https://www.cnblogs.com/TinyWong/p/12887591.html https://codeforces.com/blog/entry/87598 最后一题
NOTE: a%-b == a%b
NOTE: 对于整数来说有
ax≤by => x≤⌊by/a⌋
ax<by => x<⌈by/a⌉
ax≥by => x≥⌈by/a⌉
ax>by => x>⌊by/a⌋
NOTE: ⌊⌊x/n⌋/m⌋ = ⌊x/(n*m)⌋
NOTE: ⌈⌈x/n⌉/m⌉ = ⌈x/(n*m)⌉
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://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://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/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
https://leetcode-cn.com/problems/minimum-number-of-operations-to-reinitialize-a-permutation/
http://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
椭圆曲线加密算法 https://ac.nowcoder.com/acm/contest/6916/C
挑战 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
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
*/
func numberTheoryCollection() {
const mod int64 = 1e9 + 7 // 998244353
pow := func(x, n, p int64) (res int64) {
x %= p
res = 1
for ; n > 0; n >>= 1 {
if n&1 == 1 {
res = res * x % p
}
x = x * x % p
}
return
}
/* GCD LCM 相关
TIPS: 一般 LCM 的题目都需要用 LCM=x*y/GCD 转换成研究 GCD 的性质
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
todo https://codeforces.com/contest/1462/problem/D 的 O(nlogn) 解法
*/
gcd := func(a, b int64) int64 {
for a != 0 {
a, b = b%a, a
}
return b
}
// 例题 https://nanti.jisuanke.com/t/A1633
gcdPrefix := func(a []int64) []int64 {
n := len(a)
gp := make([]int64, n+1)
for i, v := range a {
gp[i+1] = gcd(gp[i], v)
}
return gp
}
gcdSuffix := func(a []int64) []int64 {
n := len(a)
gs := make([]int64, n+1)
for i := n - 1; i >= 0; i-- {
gs[i] = gcd(gs[i+1], a[i])
}
return gs
}
lcm := func(a, b int64) int64 { return a / gcd(a, b) * b }
// 前 n 个数的 LCM https://oeis.org/A003418 a(n) = lcm(1,...,n) ~ exp(n)
// 1, 2, 6, 12, 60, 60, 420, 840, 2520, 2520, 27720, 27720, 360360, 360360, 360360, 720720, 12252240, 12252240, 232792560, 232792560, 232792560, 232792560, 5354228880, 5354228880, 26771144400, 26771144400, 80313433200, 80313433200
// 相关题目 https://atcoder.jp/contests/arc110/tasks/arc110_a
// https://codeforces.com/contest/1485/problem/D
// 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 的种类数
// 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,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,k) = ∑phi(i)*(floor(n/i))^3 https://ac.nowcoder.com/acm/contest/7608/B
// 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 的不同个数,复杂度 O(Clog^2C)
// LC1819/周赛235D https://leetcode-cn.com/problems/number-of-different-subsequences-gcds/
countDifferentSubsequenceGCDs := func(a []int, gcd func(int, int) 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; j += i {
if has[j] {
g = gcd(g, j)
}
}
if g == i {
ans++
}
}
return
}
// 最简分数
type pair struct{ x, y int64 }
frac := func(a, b int64) pair { g := gcd(a, b); return pair{a / g, b / g} }
// 类欧几里得算法
// ∑⌊(ai+b)/m⌋, i in [0,n-1]
// todo https://www.luogu.com.cn/blog/AlanWalkerWilson/Akin-Euclidean-algorithm-Basis
// todo https://www.luogu.com.cn/blog/Shuchong/qian-tan-lei-ou-ji-li-dei-suan-fa
//
// 模板题 https://atcoder.jp/contests/practice2/tasks/practice2_c
// https://www.luogu.com.cn/problem/P5170
// todo https://codeforces.com/problemset/problem/1182/F
// https://codeforces.com/problemset/problem/1098/E
floorSum := func(n, m, a, b int64) (res int64) {
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 int64) bool { r := int64(math.Round(math.Sqrt(float64(a)))); return r*r == a }
cubeCheck := func(a int64) bool { r := int64(math.Round(math.Cbrt(float64(a)))); return r*r*r == a }
// 平方数开平方
sqrt := func(a int64) int64 {
r := int64(math.Round(math.Sqrt(float64(a))))
if r*r == a {
return r
}
return -1
}
// 立方数开立方
cbrt := func(a int64) int64 {
r := int64(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 http://oeis.org/A000265
// 质数表 https://oeis.org/A000040
// primes[i]%10 http://oeis.org/A007652
// 10-primes[i]%10 http://oeis.org/A072003
// p-1 http://oeis.org/A006093
// p+1 http://oeis.org/A008864
// p^2+p+1 http://oeis.org/A060800 = sigma(p^2)
primes := []int{
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 */
}
// map{小于 10^n 的素数个数: 小于 10^n 的最大素数} http://oeis.org/A006880 http://oeis.org/A003618 10^n-a(n): http://oeis.org/A033874
primes10 := map[int]int64{
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 的最小素数 http://oeis.org/A090226 http://oeis.org/A003617 a(n)-10^n: http://oeis.org/A033873
primes10_ := []int64{
2,
11,
101,
1009, // 1e3
10007,
100003,
1000003, // 1e6
10000019,
100000007,
1000000007, //1e9
10000000019,
}
/* 质数性质统计相关
质数的幂次组成的集合 {p^k} https://oeis.org/A000961
补集 https://oeis.org/A024619
Exponential of Mangoldt function https://oeis.org/A014963
质数前缀和 http://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 http://oeis.org/A034960
仍然是质数的前缀和 http://oeis.org/A013918 对应的前缀和下标 http://oeis.org/A013916
质数前缀积 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
质数差分 Gap https://oeis.org/A001223
Positions of records 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
Numbers whose distance to the closest prime number is a prime number http://oeis.org/A160666
任意质数之差 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
不与质数相邻的合数 http://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
哥德巴赫猜想 - 偶数分拆的最小质数 Goldbach’s conjecture https://oeis.org/A020481
由质数分布可知选到一对质数的概率是 O(1/ln^2(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
勒让德猜想 - 在两个相邻平方数之间,至少有一个质数 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
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)
http://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 int64) bool {
for i := int64(2); i*i <= n; i++ {
if n%i == 0 {
return false
}
}
return n >= 2
}
// https://www.luogu.com.cn/problem/U82118
isPrime = func(n int64) bool { return big.NewInt(n).ProbablyPrime(0) }
// 判断质数+求最大质因子
// 先用 Pollard-Rho 算法求出一个因子,然后递归求最大质因子
// https://zhuanlan.zhihu.com/p/267884783
// https://www.luogu.com.cn/problem/P4718
pollardRho := func(n int64) int64 {
if n == 4 {
return 2
}
if isPrime(n) {
return n
}
abs := func(x int64) int64 {
if x < 0 {
return -x
}
return x
}
mul := func(a, b int64) (res int64) {
for ; b > 0; b >>= 1 {
if b&1 == 1 {
res = (res + a) % n
}
a = (a + a) % n
}
return
}
for {
c := 1 + rand.Int63n(n-1)
f := func(x int64) int64 { 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
}
}
}
}
{
max := func(a, b int64) int64 {
if a > b {
return a
}
return b
}
cacheGPF := map[int64]int64{}
var gpf func(int64) int64
gpf = func(x int64) (res int64) {
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))
}
}
// 预处理: [2,mx] 范围内的质数
// 埃拉托斯特尼筛法 Sieve of Eratosthenes
// 质数个数 π(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,
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 := 2 * i; j <= mx; j += i {
pid[j] = -1
}
}
}
// 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 访问到
// 参考 https://oi-wiki.org/math/sieve/ 以及进阶指南 p.136-137
// 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 {
primes = append(primes, i)
pid[i] = len(primes)
}
for _, p := range primes {
if p*i > mx {
break
}
pid[p*i] = -1
if i%p == 0 {
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) 的线性筛求法
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
}
// 区间筛法
// 预处理 [2,√R] 的所有质数,去筛 [L,R] 之间的质数
// 质因数分解 prime factorization
// 返回分解出的质数及其指数
// https://mathworld.wolfram.com/PrimeFactorization.html
// todo 更高效的算法 - Pollard's Rho
// n 的质因数分解中 2 的幂次 http://oeis.org/A007814
// n 的质因数分解中非 2 的幂次之和 http://oeis.org/A087436
type factor struct {
p int64
e int
}
factorize := func(x int64) (factors []factor) {
for i := int64(2); i*i <= x; i++ {
e := 0
for ; x%i == 0; x /= i {
e++
}
if e > 0 {
factors = append(factors, factor{i, e})
}
}
if x > 1 {
factors = append(factors, factor{x, 1})
}
return
}
primeDivisors := func(x int) (primes []int) {
for i := 2; i*i <= x; i++ {
if x%i == 0 {
for x /= i; x%i == 0; x /= i {
}
primes = append(primes, i)
}
}
if x > 1 {
primes = append(primes, x)
}
return
}
// 阶乘的质因数分解 Finding Power of Factorial Divisor
// 见进阶指南 p.138
// https://cp-algorithms.com/algebra/factorial-divisors.html
// https://codeforces.com/contest/1114/problem/C
powerOfFactorialPrimeDivisor := func(n, p int64) (k int64) {
for pp := p; ; pp *= p {
k += n / pp
if pp > n/p { // 注意判断方式,防止爆 int64
break
}
}
return
}
// 预处理: [2,mx] 的质因数分解的系数和 bigomega(n) or Omega(n) https://oeis.org/A001222
// https://en.wikipedia.org/wiki/Prime_omega_function
// a(n) depends only on prime signature of n (cf. https://oeis.org/A025487)
// So a(24) = a(375) since 24 = 2^3 * 3 and 375 = 3 * 5^3 both have prime signature (3, 1)
//
// Omega(n) - omega(n) https://oeis.org/A046660
//
// 另一种写法 https://math.stackexchange.com/questions/1955105/corectness-of-prime-factorization-over-a-range
// 性质:Omega(nm)=Omega(n)+Omega(m)
// 前缀和 https://oeis.org/A022559 = Omega(n!) ~ O(nloglogn)
// EXTRA: https://oeis.org/A005361 Product of exponents of prime factorization of n
// https://oeis.org/A135291 Product of exponents of prime factorization of n!
primeExponentsCountAll := func() {
const mx int = 1e6
Omega := [mx + 1]int{} // int8
primes := []int{}
for i := 2; i <= mx; i++ {
if Omega[i] == 0 {
Omega[i] = 1
primes = append(primes, i)
}
for _, p := range primes {
if p*i > mx {
break
}
Omega[p*i] = Omega[i] + 1
}
}
// EXTRA: 前缀和,即 Omega(n!) https://oeis.org/A022559
for i := 3; i <= mx; i++ {
Omega[i] += Omega[i-1]
}
}
/* 因子/因数/约数
n 的因子个数 d(n) = Π(ei+1), ei 为第 i 个质数的系数 https://oeis.org/A000005 d(n) 也写作 τ(n)
Positions of records (高合成数,反素数) https://oeis.org/A002182
Values of records https://oeis.org/A002183
相关题目:范围内的最多约数个数 https://www.luogu.com.cn/problem/P1221
max(d(i)), i=1..10^n https://oeis.org/A066150
方便估计复杂度 - 近似为开立方
4, 12, 32, 64, 128, /5/
240, 448, 768, 1344, /9/
2304, 4032, 6720, 10752, 17280, 26880, 41472, 64512, 103680, 161280, /19/
上面这些数对应的最小的 n https://oeis.org/A066151
6, 60, 840, 7560, 83160, 720720, 8648640, 73513440, 735134400,
6983776800, 97772875200, 963761198400, 9316358251200, 97821761637600, 866421317361600, 8086598962041600, 74801040398884800, 897612484786617600
d(n) 前缀和 = Σ{k=1..n} floor(n/k) https://oeis.org/A006218
= 见后文「数论分块/除法分块」
n+d(n) https://oeis.org/A062249
n-d(n) https://oeis.org/A049820 count https://oeis.org/A060990 前缀和 https://oeis.org/A161664
n*d(n) https://oeis.org/A038040 前缀和 https://oeis.org/A143127 = Sum_{i=1..floor(√n)}i*(i+floor(n/i))*(floor(n/i)+1-i) - 平方和(floor(√n))
https://atcoder.jp/contests/abc172/tasks/abc172_d
n*n*d(n) https://oeis.org/A034714 前缀和 https://oeis.org/A319085
d(n)|n https://oeis.org/A033950 refactorable numbers / tau numbers
n/d(n) https://oeis.org/A036762
n%d(n) https://oeis.org/A054008
a(1)=1, a(n+1)=a(n)+d(a(n)) https://oeis.org/A064491
Smallest k such that d(k) = n https://oeis.org/A005179
a(p) = 2^(p-1) for primes p
相关题目 https://codeforces.com/problemset/problem/27/E https://www.luogu.com.cn/problem/P1128
质数的情况 https://oeis.org/A061286
Number of divisors of n^2 less than n https://oeis.org/A063647 Also number of ways to write 1/n as a difference of exactly 2 unit fractions
a(n) = (d(n^2)-1)/2
n 的因子之和 σ(n) = Π(pi^(ei+1)-1)/(pi-1) https://oeis.org/A000203 todo 相关题目 https://www.luogu.com.cn/problem/P1593
线性筛求法见后面
max(σ(i)), i=1..10^n https://oeis.org/A066410
对应的 n https://oeis.org/A066424
Smallest k such that sigma(k) = n http://oeis.org/A051444
σ(n) 前缀和 = Σ{k=1..n} k*floor(n/k) https://oeis.org/A024916
真因子之和 https://oeis.org/A001065 σ(n)-n
完全数/完美数/完备数 https://oeis.org/A000396 Perfect numbers (σ(n) = 2n)
https://en.wikipedia.org/wiki/Perfect_number
过剩数/丰数/盈数 https://oeis.org/A005101 Abundant numbers (σ(n) > 2n)
https://en.wikipedia.org/wiki/Abundant_number
亏数/缺数/不足数 https://oeis.org/A005100 Deficient numbers (σ(n) < 2n)
https://en.wikipedia.org/wiki/Deficient_number
https://ac.nowcoder.com/acm/contest/10322/A O(nlogn) 可以先预处理因子
n 的因子之积 μ(n) = n^(d(n)/2.0) https://oeis.org/A007955
because we can form d(n)/2 pairs from the factors, each with product n
若 n 是完全平方数,则 ei+1 全为奇数,此时可以计算 [n^(1/2)]^d(n)
否则,ei+1 中必有偶数,将其除二
相关题目 https://codeforces.com/problemset/problem/615/D
n 的因子的差分表的最后一个数 https://oeis.org/A187202 https://oeis.org/A187203
NOTE: a(2^k) = 1
正数 https://oeis.org/A193671
零 https://oeis.org/A187204
负数 https://oeis.org/A193672
d(d(...d(n))) 迭代至 2 所需要的迭代次数
0,0,1,0,2,0,2,1,2,0,3,0,2,2,1,0,3,0,3,2,2,0,3,1,2,2,3
高合成数/反质数 Highly Composite Numbers https://oeis.org/A002182
https://oi-wiki.org/math/prime/#_7
性质:一个高合成数一定是由另一个高合成数乘一个质数得到
见进阶指南 p.140-141
Number of divisors of n-th highly composite number https://oeis.org/A002183
Number of highly composite numbers not divisible by n https://oeis.org/A199337
求出不超过 n 的最大的反质数 https://www.luogu.com.cn/problem/P1463
Largest divisor of n having the form 2^i*5^j http://oeis.org/A132741
a(n) = A006519(n)*A060904(n) = 2^A007814(n)*5^A112765(n)
Squarefree numbers https://oeis.org/A005117 (介绍了一种筛法)
Numbers that are not divisible by a square greater than 1
Lim_{n->infinity} a(n)/n = Pi^2/6
Numbers that are not squarefree https://oeis.org/A013929
Numbers that are divisible by a square greater than 1
a(n) = Min {m>n | m has same prime factors as n ignoring multiplicity} https://oeis.org/A065642
Numbers such that a(n)/n is not an integer are listed in https://oeis.org/A284342
*/
// 枚举一个数的全部因子
divisors := func(n int64) (ds []int64) {
for d := int64(1); d*d <= n; d++ {
if n%d == 0 {
ds = append(ds, d)
if d*d < n {
ds = append(ds, n/d)
}
}
}
//sort.Slice(ds, func(i, j int) bool { return ds[i] < ds[j] })
return
}
// 不需要排序的写法
divisors = func(n int64) (ds []int64) {
ds2 := []int64{}
for d := int64(1); d*d <= n; d++ {
if n%d == 0 {
ds = append(ds, d)
if d*d < n {
ds2 = append(ds2, n/d)
}
}
}
for i := len(ds2) - 1; i >= 0; i-- {
ds = append(ds, ds2[i])
}
return
}
divisorPairs := func(n int64) (ds [][2]int64) {
for d := int64(1); d*d <= n; d++ {
if n%d == 0 {
ds = append(ds, [2]int64{d, n / d})
}
}
return
}
doDivisors := func(n int, do func(d int)) {
for d := 1; d*d <= n; d++ {
if n%d == 0 {
do(d)
if d*d < n {
do(n / d)
}
}
}
}
doDivisors2 := func(n int, do func(d1, d2 int)) {
for d := 1; d*d <= n; d++ {
if n%d == 0 {
do(d, n/d)
}
}
}
// Number of odd divisors of n https://oeis.org/A001227
// a(n) = d(2*n) - d(n)
// 亦为整数 n 分拆成若干连续整数的方法数
// Number of partitions of n into consecutive positive integers including the trivial partition of length 1
// e.g. 9 = 2+3+4 or 4+5 or 9 so a(9)=3
oddDivisorsNum := func(n int) (ans int) {
for i := 1; i*i <= n; i++ {
if n%i == 0 {
if i&1 == 1 {
ans++
}
if i*i < n && n/i&1 == 1 {
ans++
}
}
}
return
}
// 因子的中位数(偶数个因子时取小的那个)
// Lower central (median) divisor of n https://oeis.org/A060775
// EXTRA: Largest divisor of n <= sqrt(n) https://oeis.org/A033676
maxSqrtDivisor := func(n int) int {
for d := int(math.Sqrt(float64(n))); ; d-- {
if n%d == 0 {
return d
}
}
}
// 预处理: [1,mx] 范围内数的所有因子
// 复杂度 O(nlogn)
// NOTE: 1~n 的因子个数总和大约为 nlogn
// NOTE: divisors[x] 为奇数 => x 为完全平方数 https://oeis.org/A000290
// NOTE: halfDivisors(x) 为 ≤√x 的因数集合 https://oeis.org/A161906
divisorsAll := func() {
const mx int = 1e6
divisors := [mx + 1][]int{}
for i := 1; i <= mx; i++ {
for j := i; j <= mx; j += i {
divisors[j] = append(divisors[j], i)
}
}
{
// 统计因子个数 d(n)
// NOTE: 复杂度可以做到线性 https://codeforces.com/contest/920/submission/76859782
// 相关 OEIS 见上面的注释块
const mx int = 1e6
d := [mx + 1]int{}
for i := 1; i <= mx; i++ {
for j := i; j <= mx; j += i {
d[j]++
}
}
}
{
// 去掉 1 作为因子
const mx = 1e6
divisors := [mx + 1][]int{1: {1}} // 仅保留 1 的因子 1
for i := 2; i <= mx; i++ {
for j := i; j <= mx; j += i {
divisors[j] = append(divisors[j], i)
}
}
}
{
// 线性筛求 n 的因子之和 σ(n)
// https://codeforces.com/contest/1512/problem/G
const mx int = 1e7
d := make([]int, mx+1)
d[1] = 1
s := make([]int, mx+1)
primes := []int{}
for i := 2; i <= mx; i++ {
if d[i] == 0 {
s[i] = 1 + i
d[i] = s[i]
primes = append(primes, i)
}
for _, p := range primes {
if p*i > mx {
break
}
if i%p == 0 {
s[p*i] = s[i]*p + 1
d[p*i] = d[i] / s[i] * s[p*i]
break
}
s[p*i] = 1 + p
d[p*i] = d[i] * s[p*i]
}
}
}
isSquareNumber := func(x int) bool { return len(divisors[x])&1 == 1 }
halfDivisors := func(x int) []int { d := divisors[x]; return d[:(len(d)-1)/2+1] }
_, _ = isSquareNumber, halfDivisors
}
// LPF(n): least prime dividing n (when n > 1); a(1) = 1 https://oeis.org/A020639
// 有时候数据范围比较大,用 primeFactorsAll 预处理会 MLE,这时候就要用 LPF 了(同样是预处理但是内存占用低)
// 先预处理出 LPF,然后对要处理的数 v 不断地除 LPF(v) 直到等于 1
// LPF 前缀和 https://oeis.org/A046669 前缀积 https://oeis.org/A072486
// n+LPF(n) https://oeis.org/A061228 the smallest number greater than n which is not coprime to n
// n-LPF(n) https://oeis.org/A046666
// 迭代至 0 的次数 https://oeis.org/A175126 相关题目 https://codeforces.com/contest/1076/problem/B
// n*LPF(n) https://oeis.org/A285109
// n/LPF(n) https://oeis.org/A032742 即 n 的最大因子 = Max{gcd(n,j); j=n+1..2n-1}
//
// 只考虑奇质数 http://oeis.org/A078701
//
// GPF(n): greatest prime dividing n, for n >= 2; a(1)=1 https://oeis.org/A006530
// GPF(p-1) http://oeis.org/A023503
// GPF(p+1) http://oeis.org/A023509
// GPF 前缀和 https://oeis.org/A046670 前缀积 https://oeis.org/A104350
// n+GPF(n) https://oeis.org/A070229 the next m>n such that GPF(n)|m
// n-GPF(n) https://oeis.org/A076563
// 迭代至 0 的次数 https://oeis.org/A309892
// n*GPF(n) https://oeis.org/A253560
// n/GPF(n) https://oeis.org/A052126
// a(1)=1, a(n+1)=a(n)+GPF(a(n)) https://oeis.org/A076271
//
// n/LPF(n)*GPF(n) https://oeis.org/A130064
// n/GPF(n)*LPF(n) https://oeis.org/A130065
//
lpfAll := func() {
const mx int = 1e6
lpf := [mx + 1]int{1: 1}
for i := 2; i <= mx; i++ {
if lpf[i] == 0 {
for j := i; j <= mx; j += i {
if lpf[j] == 0 { // 去掉这个判断就变成求 GPF,也可以用来(从大到小地)分解质因数
lpf[j] = i
}
}
}
}
// EXTRA: 分解 x
// lpf[x]==p 也可以写成 x%p==0
factorize := func(x int) {
for x > 1 {
p := lpf[x]
e := 1
for x /= p; lpf[x] == p; x /= p {
e++
}
// do(p,e) ...
}
}