maomao90's Library
A C++20 library for competitive programming.
Loading...
Searching...
No Matches
math.hpp
Go to the documentation of this file.
1#pragma once
2
4constexpr long long pow_mod_constexpr(long long x, long long n, int m) {
5 if (m == 1)
6 return 0;
7 unsigned int _m = (unsigned int)(m);
8 unsigned long long r = 1;
9 unsigned long long y = x < 0 ? x % m + m : x % m;
10 while (n) {
11 if (n & 1)
12 r = (r * y) % _m;
13 y = (y * y) % _m;
14 n >>= 1;
15 }
16 return r;
17}
18// Compile time primitive root
19// @param m must be prime
20// @return primitive root (and minimum in now)
21constexpr int primitive_root_constexpr(int m) {
22 if (m == 2)
23 return 1;
24 if (m == 167772161)
25 return 3;
26 if (m == 469762049)
27 return 3;
28 if (m == 754974721)
29 return 11;
30 if (m == 998244353)
31 return 3;
32 int divs[20] = {};
33 divs[0] = 2;
34 int cnt = 1;
35 int x = (m - 1) / 2;
36 while (x % 2 == 0)
37 x /= 2;
38 for (int i = 3; (long long)(i)*i <= x; i += 2) {
39 if (x % i == 0) {
40 divs[cnt++] = i;
41 while (x % i == 0) {
42 x /= i;
43 }
44 }
45 }
46 if (x > 1) {
47 divs[cnt++] = x;
48 }
49 for (int g = 2;; g++) {
50 bool ok = true;
51 for (int i = 0; i < cnt; i++) {
52 if (pow_mod_constexpr(g, (m - 1) / divs[i], m) == 1) {
53 ok = false;
54 break;
55 }
56 }
57 if (ok)
58 return g;
59 }
60}
61template <int m> constexpr int primitive_root = primitive_root_constexpr(m);
62
63struct barrett {
64 unsigned int _m;
65 unsigned long long im;
66
67 explicit barrett(unsigned int m)
68 : _m(m), im((unsigned long long)(-1) / m + 1) {}
69
70 unsigned int umod() const { return _m; }
71
72 unsigned int mul(unsigned int a, unsigned int b) const {
73 // [1] m = 1
74 // a = b = im = 0, so okay
75
76 // [2] m >= 2
77 // im = ceil(2^64 / m)
78 // -> im * m = 2^64 + r (0 <= r < m)
79 // let z = a*b = c*m + d (0 <= c, d < m)
80 // a*b * im = (c*m + d) * im = c*(im*m) + d*im = c*2^64 + c*r + d*im
81 // c*r + d*im < m * m + m * im < m * m + 2^64 + m <= 2^64 + m * (m + 1) <
82 // 2^64 * 2
83 // ((ab * im) >> 64) == c or c + 1
84 unsigned long long z = a;
85 z *= b;
86#ifdef _MSC_VER
87 unsigned long long x;
88 _umul128(z, im, &x);
89#else
90 unsigned long long x =
91 (unsigned long long)(((unsigned __int128)(z)*im) >> 64);
92#endif
93 unsigned long long y = x * _m;
94 return (unsigned int)(z - y + (z < y ? _m : 0));
95 }
96};
97} // namespace maomao90::internal::math
Definition math.hpp:3
constexpr int primitive_root_constexpr(int m)
Definition math.hpp:21
constexpr long long pow_mod_constexpr(long long x, long long n, int m)
Definition math.hpp:4
constexpr int primitive_root
Definition math.hpp:61
barrett(unsigned int m)
Definition math.hpp:67
unsigned int _m
Definition math.hpp:64
unsigned long long im
Definition math.hpp:65
unsigned int umod() const
Definition math.hpp:70
unsigned int mul(unsigned int a, unsigned int b) const
Definition math.hpp:72