基本数学算法模板
一、快速乘小技巧
【__int128】
1 long long z=(__int128)x*y%P;
【利用自然溢出】
1 2 3 4 5 ll mul (ll a,ll b,ll P) { ll d=(long double )a/P*b; ll res=(ull)a*b-(ull)d*P; return res%P; }
原理如下,利用long double算出 d = ⌊ a b P ⌋ d = \left\lfloor\frac{ab}{P}\right\rfloor d = ⌊ P a b ⌋ ,所以 r e s = a × b − d × P res = a \times b - d \times P r e s = a × b − d × P ,因为最后答案一定再long long范围内,自然溢出的部分一定相同。
二、素数判定 Miller-Rabin
Miller-Rabin素性测试是一种概率性测试,但是若选取的底数正确,能在long long 范围内有 100 % 100\% 1 0 0 % 的正确率。
该算法依赖于下面两个定理:
费马小定理:若 p p p 为素数,p ∤ a p \nmid a p ∤ a ,则有 a p − 1 ≡ 1 ( m o d p ) a^{p-1} \equiv 1 \pmod{p} a p − 1 ≡ 1 ( m o d p ) 。
二次探测定理:若 p p p 为素数,x 2 ≡ 1 ( m o d p ) x^2 \equiv 1 \pmod{p} x 2 ≡ 1 ( m o d p ) ,则 x ≡ 1 , p − 1 ( m o d p ) x \equiv 1, p-1 \pmod{p} x ≡ 1 , p − 1 ( m o d p ) 。
结合这两个素数的必要条件来进行素性检测。
首先选取一些底数 a a a ,将 p − 1 p-1 p − 1 拆分为 m × 2 t m \times 2^t m × 2 t 的形式。
然后,若有 a p − 1 2 k ≡ 1 ( m o d p ) a^{\frac{p-1}{2^k}} \equiv 1 \pmod{p} a 2 k p − 1 ≡ 1 ( m o d p ) ,则判断 a p − 1 2 k + 1 ≡ 1 , p − 1 ( m o d p ) a^{\frac{p-1}{2^{k+1}}} \equiv 1, p-1 \pmod{p} a 2 k + 1 p − 1 ≡ 1 , p − 1 ( m o d p ) 是否成立。这一部分是二次探测。
最后,看 a p − 1 ≡ 1 ( m o d p ) a^{p-1} \equiv 1 \pmod{p} a p − 1 ≡ 1 ( m o d p ) 是否成立。这一部分是费马小定理检测。
底数 a a a 取 { 2 , 325 , 9375 , 28178 , 450775 , 9780504 , 1795265022 } \{2,325,9375,28178,450775,9780504,1795265022\} { 2 , 3 2 5 , 9 3 7 5 , 2 8 1 7 8 , 4 5 0 7 7 5 , 9 7 8 0 5 0 4 , 1 7 9 5 2 6 5 0 2 2 } 七个数可保证long long 范围类的正确性,但是一般没必要,随机取10个数字也行。
实现的时候需要算 long long 范围的数的平方,可以使用快速乘或者__int128。(或者写龟速乘)
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 int mul (int a,int b,int P) { int d=(long double )a/P*b; int res=(ull)a*b-(ull)d*P; return (res+P)%P; } int qpow (int a,int b,int P) { int res=1 ; while (b) { if (b&1 )res=mul (res,a,P); a=mul (a,a,P); b>>=1 ; } return res; } int chk (int n) { if (n==1 )return 0 ; if (n<=3 )return 1 ; if (n%2 ==0 )return 0 ; int t=0 ,m=n-1 ; while (m%2 ==0 )m/=2 ,++t; int A[]={2 ,325 ,9375 ,28178 ,450775 ,9780504 ,1795265022 }; for (int i=0 ;i<7 ;++i) { int a=A[i]; int k=0 ,x=qpow (a,m,n),y; while (k<t) { ++k; y=mul (x,x,n); if (y==1 &&!(x==1 ||x==n-1 ))return 0 ; x=y; } if (x!=1 )return 0 ; } return 1 ; }
三、反素数
若 n n n 是反素数,那么 n n n 是 [ 1 , n ] [1,n] [ 1 , n ] 种因子数最多的数,由唯一分解定理 n = p 1 c 1 p 2 c 2 p 3 c 3 p 4 c 4 … n = p_1^{c_1} p_2^{c_2} p_3^{c_3} p_4^{c_4} \dots n = p 1 c 1 p 2 c 2 p 3 c 3 p 4 c 4 … ,n n n 的因子数量为 ( c 1 + 1 ) ( c 2 + 1 ) ( c 3 + 1 ) … (c_1 + 1)(c_2 + 1)(c_3 + 1) \dots ( c 1 + 1 ) ( c 2 + 1 ) ( c 3 + 1 ) …
反素数的唯一分解形式会有以下性质:
p 1 , p 2 , p 3 … p_1, p_2, p_3 \dots p 1 , p 2 , p 3 … 会是最小的几个质数,即 2 , 3 , 5 , 7 … 2, 3, 5, 7 \dots 2 , 3 , 5 , 7 …
c 1 ≥ c 2 ≥ c 3 … c_1 \ge c_2 \ge c_3 \dots c 1 ≥ c 2 ≥ c 3 …
P1463 反素数
【题目描述】求不超过 N ( ≤ 2 × 1 0 9 ) N(\le 2 \times 10^9) N ( ≤ 2 × 1 0 9 ) 的最大反素数
【题解】根据反素数的性质,写个 dfs 搜一下。😀
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include <bits/stdc++.h> using namespace std;int N,p[]={2 ,3 ,5 ,7 ,11 ,13 ,17 ,19 ,23 ,29 ,31 ,37 ,41 },ans,cnt;void dfs (int nw,int lst,int i,int nwcnt) { if ((nwcnt>cnt)||(nwcnt==cnt&&nw<ans)) { cnt=nwcnt; ans=nw; } for (int z=1 ;z<=lst;++z) { if (1ll *nw*p[i]>N)break ; nw*=p[i]; dfs (nw,z,i+1 ,nwcnt*(z+1 )); } } int main () { scanf ("%d" ,&N); dfs (1 ,32 ,0 ,1 ); printf ("%d" ,ans); return 0 ; }
三、质因数分解Pollard-Rho
Pollard-Rho 用于找到合数的一个因子。
期望复杂度 O ( p log N ) O(\sqrt{p} \log N) O ( p log N ) ,用倍增后期望 O ( p log N ) O(\sqrt{p} \log N) O ( p log N ) 。
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 #include <bits/stdc++.h> #define ll long long #define ull unsigned long long using namespace std;ll mul (ll a,ll b,ll P) { ll d=(long double )a/P*b; return ((ull)a*b-(ull)P*d+P)%P; } ll qpow (ll a,ll b,ll P) { ll res=1 ; while (b) { if (b&1 )res=mul (res,a,P); a=mul (a,a,P); b>>=1 ; } return res; } bool chk (ll n) { if (n==1 )return 0 ; if (n==2 )return 1 ; if (n%2 ==0 )return 0 ; ll a,A[]={2 ,325 ,9375 ,28178 ,450775 ,9780504 ,1795265022 },m=n-1 ,t=0 ; while (m%2 ==0 )m/=2 ,++t; for (int i=0 ;i<7 ;++i) { a=A[i]%n; if (a==0 )continue ; ll x=qpow (a,m,n),y; for (int j=1 ;j<=t;++j) { y=mul (x,x,n); if (y==1 &&(x!=1 &&x!=n-1 ))return 0 ; x=y; } if (x!=1 )return 0 ; } return 1 ; } ll rd () { return rand ()*rand ()*rand ()*rand (); } ll PR (ll n) { auto add=[&n](ll x,ll y) { x+=y; return x>n?x-n:x; }; auto del=[&n](ll x,ll y) { x-=y; return x<0 ?x+n:x; }; while (1 ) { ll x=rd ()%n,y,c=rd ()%(n-1 )+1 ; auto f=[=](ll x) {return add (mul (x,x,n),c);}; y=f (x); for (int L=1 ,i,T=0 ;T<=40 &&x!=y;) { ll d=1 ; i=0 ; while (i<L&&x!=y) { d=mul (d,del (y,x),n); x=f (x);y=f (f (y)); ++i; } d=__gcd(d,n); if (d>1 &&d<n)return d; if (L<128 )L*=2 ; } } } ll factor (ll n) { ll mx=1 ; queue<ll>q; q.push (n); while (!q.empty ()) { ll x=q.front ();q.pop (); if (x<=mx)continue ; if (chk (x)) mx=max (x,mx); else { ll Z=PR (x); q.push (Z); q.push (x/Z); } } return mx; }
四、欧拉定理和欧拉函数
**欧拉定理:**若 ( a , p ) = 1 (a, p) = 1 ( a , p ) = 1 ,则 a φ ( p ) ≡ 1 ( m o d p ) a^{\varphi(p)} \equiv 1 \pmod{p} a φ ( p ) ≡ 1 ( m o d p ) 。
扩展欧拉定理:
a b ≡ { a b m o d φ ( p ) ( a , p ) = 1 a b ( a , p ) ≠ 1 , b < φ ( p ) a b m o d φ ( p ) + φ ( p ) ( a , p ) ≠ 1 , b ≥ φ ( p ) ( m o d p ) a^b \equiv \begin{cases}
a^{b \bmod \varphi(p)} & (a, p) = 1\\
a^{b} & (a, p) \neq 1, b < \varphi(p)\\
a^{b \bmod \varphi(p) + \varphi(p)} & (a, p) \neq 1, b \ge \varphi(p)
\end{cases} \pmod{p}
a b ≡ ⎩ ⎪ ⎪ ⎨ ⎪ ⎪ ⎧ a b m o d φ ( p ) a b a b m o d φ ( p ) + φ ( p ) ( a , p ) = 1 ( a , p ) = 1 , b < φ ( p ) ( a , p ) = 1 , b ≥ φ ( p ) ( m o d p )
【求欧拉函数】
φ ( n ) = n × ∏ i = 1 s p i − 1 p i \varphi(n) = n \times \prod_{i=1}^{s} \frac{p_i - 1}{p_i}
φ ( n ) = n × i = 1 ∏ s p i p i − 1
分解质因数后做即可。
【线筛求欧拉函数】
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 void prepapre () { phi[1 ]=1 ; for (int i=2 ;i<N;i++) { if (!ok[i]) { pr[++cnt]=i; phi[i]=i-1 ; } for (int j=1 ;j<=cnt&&pr[j]*i<N;++j) { ok[pr[j]*i]=1 ; if (i%pr[j]==0 ) { phi[i*pr[j]]=phi[i]*pr[j]; break ; } phi[i*pr[j]]=phi[i]*(pr[j]-1 ); } }
五、二元一次不定方程和扩展欧几里得算法
裴蜀定理: 设 a , b a, b a , b 是不全为零的整数,对任意整数 x , y x, y x , y ,满足 gcd ( a , b ) ∣ a x + b y \gcd(a, b) \mid ax + by g cd( a , b ) ∣ a x + b y ,且存在整数 x , y x, y x , y , 使得 a x + b y = gcd ( a , b ) ax + by = \gcd(a, b) a x + b y = g cd( a , b ) 。
**exgcd:**扩展欧几里得算法用于求解不定方程 a x + b y = gcd ( a , b ) ax + by = \gcd(a, b) a x + b y = g cd( a , b ) ,该算法综合利用了欧几里得算法和裴蜀定理。
a x + b y = gcd ( a , b ) b y 0 + ( a m o d b ) x 0 = gcd ( b , a m o d b ) = gcd ( a , b ) b y 0 + ( a − b × ⌊ a b ⌋ ) x 0 = gcd ( a , b ) 整理后有, a x 0 + b ( y 0 − ⌊ a b ⌋ × x 0 ) = gcd ( a , b ) ax + by = \gcd(a, b)\\
by_0 + (a \bmod b)x_0 = \gcd(b, a \bmod b) = \gcd(a, b)\\
by_0 + (a - b \times \left\lfloor\frac{a}{b}\right\rfloor)x_0 = \gcd(a, b)\\
\text{整理后有,}\\
ax_0 + b(y_0 - \left\lfloor\frac{a}{b}\right\rfloor \times x_0) = \gcd(a, b)\\
a x + b y = g cd( a , b ) b y 0 + ( a m o d b ) x 0 = g cd( b , a m o d b ) = g cd( a , b ) b y 0 + ( a − b × ⌊ b a ⌋ ) x 0 = g cd( a , b ) 整理后有, a x 0 + b ( y 0 − ⌊ b a ⌋ × x 0 ) = g cd( a , b )
【exgcd求二元一次不定方程的特解】
由此递推,我们便能设计出求解不定方程的算法,
1 2 3 4 5 6 7 void exgcd (int a,int b,int &x,int &y) { if (b==0 )x=1 ,y=0 ; else { exgcd (b,a%b,y,x); y-=a/b*x; } }
【求二元一次不定方程的通解】
先判断 c c c 是否是 gcd ( a , b ) \gcd(a, b) g cd( a , b ) 的倍数,若不是则无解。
再利用 exgcd 求出 a x + b y = gcd ( a , b ) ax + by = \gcd(a, b) a x + b y = g cd( a , b ) 的解。
最后将 x , y x, y x , y 都乘上 c gcd ( a , b ) \frac{c}{\gcd(a, b)} g c d ( a , b ) c 即可。
可以求出不定方程 a x + b y = c ax + by = c a x + b y = c 的一个特解 ,记作 ( x 0 , y 0 ) (x_0, y_0) ( x 0 , y 0 ) ,那怎么求出其他的解呢?
注意到,若知道 a x 1 + b y 1 = 0 ax_1 + by_1 = 0 a x 1 + b y 1 = 0 的通解 形式 ( x 1 , y 1 ) (x_1, y_1) ( x 1 , y 1 ) ,那么 a x + b y = c ax + by = c a x + b y = c 的解就是 ( x = x 0 + x 1 , y = y 0 + y 1 ) (x = x_0 + x_1, y = y_0 + y_1) ( x = x 0 + x 1 , y = y 0 + y 1 ) 。
考虑 a x 1 + b y 1 = 0 ax_1 + by_1 = 0 a x 1 + b y 1 = 0 ,方程除以 gcd ( a , b ) \gcd(a, b) g cd( a , b ) ,记 a = a 0 gcd ( a , b ) , b = b 0 gcd ( a , b ) a = a_0\gcd(a, b), b = b_0\gcd(a, b) a = a 0 g cd( a , b ) , b = b 0 g cd( a , b ) 。
只需求解 a 0 x 1 + b 0 y 1 = 0 a_0x_1 + b_0y_1 = 0 a 0 x 1 + b 0 y 1 = 0 ,其中 a 0 , b 0 a_0, b_0 a 0 , b 0 互质。
方程变形有 a 0 x 1 b 0 = − y 1 \frac{a_0x_1}{b_0} = -y_1 b 0 a 0 x 1 = − y 1 ,因为 a 0 , b 0 a_0, b_0 a 0 , b 0 互质,所以有 b 0 ∣ x 1 b_0 \mid x_1 b 0 ∣ x 1 ,所以 x 1 = k b 0 x_1 = k b_0 x 1 = k b 0 。
b 0 ∣ a 0 x 1 ∵ ( a 0 , b 0 ) = 1 , ∴ b 0 ∣ x 1 x 1 = k b 0 b_0 \mid a_0x_1\\
\because (a_0, b_0) = 1, \therefore b_0 \mid x_1\\
x_1 = k b_0
b 0 ∣ a 0 x 1 ∵ ( a 0 , b 0 ) = 1 , ∴ b 0 ∣ x 1 x 1 = k b 0
带入原式有 a 0 k b 0 + b 0 y 1 = 0 a_0k b_0 + b_0 y_1 = 0 a 0 k b 0 + b 0 y 1 = 0 ,解得 y 1 = − a 0 k y_1 = -a_0 k y 1 = − a 0 k 。
所以不定方程 a x + b y = c ax + by = c a x + b y = c 的通解形式为 x = x 0 + k b 0 , y = y 0 − k a 0 x = x_0 + k b_0, y = y_0 - k a_0 x = x 0 + k b 0 , y = y 0 − k a 0 。
x = x 0 + k b gcd ( a , b ) , y = y 0 − k a gcd ( a , b ) x = x_0 + k\frac{b}{\gcd(a, b)}, y = y_0 - k\frac{a}{\gcd(a, b)}
x = x 0 + k g cd( a , b ) b , y = y 0 − k g cd( a , b ) a
x > 0 , y > 0 k > − x 0 b 0 , y 0 a 0 > k − x 0 b 0 < k < y 0 a 0 ∧ k ∈ Z − x 0 b 0 < k < y 0 a 0 ∧ k ∈ Z L ≤ k ≤ R R − L + 1 x > 0, y > 0\\
k > -\frac{x_0}{b_0}, \frac{y_0}{a_0} > k\\
-\frac{x_0}{b_0} < k < \frac{y_0}{a_0} \land k \in \mathbb{Z}\\
-\frac{x_0}{b_0} < k < \frac{y_0}{a_0} \land k \in \mathbb{Z}\\
L \le k \le R\\
R - L + 1
x > 0 , y > 0 k > − b 0 x 0 , a 0 y 0 > k − b 0 x 0 < k < a 0 y 0 ∧ k ∈ Z − b 0 x 0 < k < a 0 y 0 ∧ k ∈ Z L ≤ k ≤ R R − L + 1
六、乘法逆元
费马小定理
exgcd
解下面的二元一次不定方程,
a x + p y = 1 ax + py = 1
a x + p y = 1
由裴蜀定理知道,当 gcd ( a , p ) = 1 \gcd(a, p) = 1 g cd( a , p ) = 1 时候才有解,也就是说这就是 a a a 在模 p p p 意义下逆元存在的条件。
线性求1~n的逆元
1 2 inv[1 ]=1 ; for (int i=2 ;i<=n;++i)inv[i]=(P-P/i)*inv[P%i];
七、同余方程组与中国剩余定理
【模数互质情况】
解下面的同余方程组
{ x ≡ c 1 ( m o d p 1 ) x ≡ c 2 ( m o d p 2 ) … x ≡ c n ( m o d p n ) \left\{
\begin{matrix}
x \equiv c_1 \pmod {p_1}\\
x \equiv c_2 \pmod {p_2}\\
\dots\\
x \equiv c_n \pmod {p_n}\\
\end{matrix}
\right.
⎩ ⎪ ⎪ ⎪ ⎨ ⎪ ⎪ ⎪ ⎧ x ≡ c 1 ( m o d p 1 ) x ≡ c 2 ( m o d p 2 ) … x ≡ c n ( m o d p n )
其中 p 1 , p 2 … p n p_1, p_2 \dots p_n p 1 , p 2 … p n 两两互质。
中国剩余定理 用下面的方式求得解 x x x 。
令 N = ∏ i = 1 n p i N = \prod_{i=1}^{n} p_i N = ∏ i = 1 n p i ,N i = N p i N_i = \frac{N}{p_i} N i = p i N ,因为 p i p_i p i 两两互质。则 gcd ( N i , p i ) = 1 \gcd(N_i, p_i) = 1 g cd( N i , p i ) = 1 ,那么一定存在 N i − 1 N_i^{-1} N i − 1 满足 N i N i − 1 ≡ 1 ( m o d p i ) N_i N_i^{-1} \equiv 1 \pmod{p_i} N i N i − 1 ≡ 1 ( m o d p i ) 。
令 M i = c i N i − 1 N i M_i = c_i N_i^{-1} N_i M i = c i N i − 1 N i ,有
M i ≡ { c i ( m o d p i ) 0 ( m o d p j ) j ≠ i M i = ( N i N i − 1 ) c i ≡ c i ( m o d p i ) 又 ∵ p j ∣ N i , ∴ M i ≡ 0 ( m o d p j ) M_i \equiv \left\{
\begin{matrix}
c_i & \pmod {p_i}\\
0 & \pmod {p_j} & j \neq i\\
\end{matrix}
\right.
\\
M_i = (N_i N_i^{-1})c_i \equiv c_i \pmod {p_i}\\
\text{又} \because p_j \mid N_i, \therefore M_i \equiv 0 \pmod{p_j}
M i ≡ { c i 0 ( m o d p i ) ( m o d p j ) j = i M i = ( N i N i − 1 ) c i ≡ c i ( m o d p i ) 又 ∵ p j ∣ N i , ∴ M i ≡ 0 ( m o d p j )
所以满足
∑ i = 1 n M i ≡ c i ( m o d p i ) \sum_{i=1}^{n} M_i \equiv c_i \pmod {p_i}\\
i = 1 ∑ n M i ≡ c i ( m o d p i )
x 0 = ∑ i = 1 n M i 是方程的一个特解 x_0 = \sum_{i=1}^{n} M_i \text{是方程的一个特解}
x 0 = i = 1 ∑ n M i 是方程的一个特解
所以
x ≡ ∑ i = 1 n M i ( m o d N ) x \equiv \sum_{i=1}^{n} M_i \pmod N
x ≡ i = 1 ∑ n M i ( m o d N )
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 void exgcd (int a,int b,int &x,int &y) { if (b==0 )x=1 ,y=0 ; else { exgcd (b,a%b,y,x); y-=a/b*x; } } int inv (int a,int p) { int x,y; a%=p; exgcd (a,p,x,y); x=(x%p+p)%p; return x; } int n,c[20 ],p[20 ],N=1 ,ans;int mul (int a,int b,int P) { int x=(long double )a/P*b; int res=(unsigned long long )a*b-(unsigned long long )x*P; return res; } signed main () { scanf ("%lld" ,&n); for (int i=1 ;i<=n;++i)scanf ("%lld%lld" ,&p[i],&c[i]),N*=p[i]; for (int i=1 ;i<=n;++i) { int Ni=N/p[i],M=mul (mul (Ni,inv (Ni,p[i]),N),c[i],N); ans+=M; } ans%=N; printf ("%lld" ,ans); return 0 ; }
【模数不互质】
如果不保证模数互质该怎么做呢?
沿用每次将两个同余方程合并成一个新的的思路。
考虑这个同余方程组,
{ x ≡ c 1 ( m o d p 1 ) x ≡ c 2 ( m o d p 2 ) \left\{
\begin{matrix}
x \equiv c_1 \pmod {p_1}\\
x \equiv c_2 \pmod {p_2}
\end{matrix}
\right.
{ x ≡ c 1 ( m o d p 1 ) x ≡ c 2 ( m o d p 2 )
其中 p 1 , p 2 p_1, p_2 p 1 , p 2 不一定互质。
那么有,
{ x = c 1 + a p 1 x = c 2 + b p 2 ∴ a p 1 + ( − b ) p 2 = c 2 − c 1 \left\{
\begin{matrix}
x = c_1 + a p_1\\
x = c_2 + b p_2\\
\end{matrix}
\right.
\\
\therefore a p_1 + (-b) p_2 = c_2 - c_1\\
{ x = c 1 + a p 1 x = c 2 + b p 2 ∴ a p 1 + ( − b ) p 2 = c 2 − c 1
还是考虑解二元一次线性方程,
当 gcd ( p 1 , p 2 ) ∤ ( c 2 − c 1 ) \gcd(p_1, p_2) \nmid (c_2 - c_1) g cd( p 1 , p 2 ) ∤ ( c 2 − c 1 ) ,由裴蜀定理知无解;
否则,我们先求解
a ′ p 1 + b ′ p 2 = gcd ( p 1 , p 2 ) a' p_1 + b' p_2 = \gcd(p_1, p_2)
a ′ p 1 + b ′ p 2 = g cd( p 1 , p 2 )
再令
d = c 2 − c 1 gcd ( p 1 , p 2 ) a = a ′ d , b = − b ′ d d = \frac{c_2 - c_1}{\gcd(p_1, p_2)}\\
a = a' d, b = -b' d
d = g cd( p 1 , p 2 ) c 2 − c 1 a = a ′ d , b = − b ′ d
再将 a a a 带入 x = c 1 + a p 1 x = c_1 + a p_1 x = c 1 + a p 1 求出一个 x 0 x_0 x 0 .
然后能将问题转化为
x = x 0 ( m o d p 1 p 2 gcd ( p 1 , p 2 ) ) x = x_0 \pmod {\frac{p_1 p_2}{\gcd(p_1, p_2)}}
x = x 0 ( m o d g cd( p 1 , p 2 ) p 1 p 2 )
一个特解 x 0 若求得 C ≡ 0 ( m o d p 1 ) ∧ C ≡ 0 ( m o d p 2 ) 那么 x = x 0 + k C , 即 x ≡ x 0 ( m o d C ) \text{一个特解} x_0\\
\text{若求得} C \equiv 0 \pmod {p_1} \land C \equiv 0 \pmod {p_2}\\
\text{那么} x = x_0 + k C, \text{即} x \equiv x_0 \pmod C
一个特解 x 0 若求得 C ≡ 0 ( m o d p 1 ) ∧ C ≡ 0 ( m o d p 2 ) 那么 x = x 0 + k C , 即 x ≡ x 0 ( m o d C )
写代码的时候有一些细节需要注意
算lcm要先除后乘(否则要用快速乘)。
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 #include <bits/stdc++.h> #define int long long using namespace std;void exgcd (int a,int b,int &x,int &y) { if (b==0 )x=1 ,y=0 ; else { exgcd (b,a%b,y,x); y-=a/b*x; } } int n,c[100005 ],p[100005 ],ans;int mul (int a,int b,int P) { int x=(long double )a/P*b; int res=(unsigned long long )a*b-(unsigned long long )x*P; return res; } signed main () { scanf ("%lld" ,&n); for (int i=1 ;i<=n;++i)scanf ("%lld%lld" ,&p[i],&c[i]); for (int i=1 ;i<n;++i) { int d=__gcd(p[i],p[i+1 ]); int x,y,x0,pp=p[i]/d*p[i+1 ]; exgcd (p[i],p[i+1 ],x,y); x=mul (x,(c[i+1 ]-c[i])/d,pp); x0=((c[i]+mul (x,p[i],pp))%pp+pp)%pp; c[i+1 ]=x0;p[i+1 ]=pp; } printf ("%lld" ,c[n]); return 0 ; }
八、Lucas与exLucas
Lucas定理:
当p是质数
( n m ) ≡ ( n m o d p m m o d p ) ⋅ ( ⌊ n p ⌋ ⌊ m p ⌋ ) ( m o d p ) \binom{n}{m} \equiv \binom{n \bmod p}{m \bmod p} \cdot \binom{\left\lfloor \frac{n}{p} \right\rfloor}{\left\lfloor \frac{m}{p} \right\rfloor} \pmod{p}
( m n ) ≡ ( m m o d p n m o d p ) ⋅ ( ⌊ p m ⌋ ⌊ p n ⌋ ) ( m o d p )
1 2 3 4 5 6 7 8 9 10 11 LL C (LL n,LL m,LL P) { if (n<m)return 0 ; LL up=1 ,down=1 ; for (LL i=m+1 ;i<=n;++i)up=up*i%P; for (LL i=1 ;i<=n-m;++i)down=down*i%P; return up*inv (down,P); } LL lucas (LL n,LL m,LL P) { if (m==0 )return 1 ; return C (n%P,m%P,P)*lucas (n/P,m/P,P)%P; }
【exLucas】
计算
( n m ) m o d P n , m ≤ 1 0 18 , p ≤ 1 0 6 , 不保证 P 为质数 \binom{n}{m} \bmod P\\
n, m \le 10^{18}, p \le 10^6, \text{不保证} P \text{为质数}
( m n ) m o d P n , m ≤ 1 0 1 8 , p ≤ 1 0 6 , 不保证 P 为质数
首先利用唯一分解定理和中国剩余定理,将问题转化为求
( n m ) m o d p k 其中 p 为质数 \binom{n}{m} \bmod p^{k}\\
\text{其中} p \text{为质数}
( m n ) m o d p k 其中 p 为质数
拆成阶乘后,因为分母可能含因子 p p p ,可能没法求逆元,考虑分子分母除掉因子 p p p 。
即求
n ! p x m ! p y ( n − m ) ! p z × p x − y − z m o d p k \frac{\frac{n!}{p^x}}{\frac{m!}{p^y}\frac{(n-m)!}{p^z}} \times p^{x-y-z} \bmod p^k
p y m ! p z ( n − m ) ! p x n ! × p x − y − z m o d p k
问题转化为求
n ! p x m o d p k \frac{n!}{p^x} \bmod p^k
p x n ! m o d p k
推导
n ! = q ⌊ n q ⌋ ( ⌊ n q ⌋ ! ) ( ∏ i = 1 , q ∤ i q k i ) ⌊ n q k ⌋ ( ∏ i = 1 , q ∤ i n m o d q k i ) n! = q^{\left\lfloor\frac{n}{q}\right\rfloor} \left(\left\lfloor\frac{n}{q}\right\rfloor!\right) \left(\prod_{i=1, q \nmid i}^{q^k} i\right)^{\left\lfloor\frac{n}{q^k}\right\rfloor} \left(\prod_{i=1, q \nmid i}^{n \bmod q^k} i\right)
n ! = q ⌊ q n ⌋ ( ⌊ q n ⌋ ! ) ⎝ ⎛ i = 1 , q ∤ i ∏ q k i ⎠ ⎞ ⌊ q k n ⌋ ⎝ ⎛ i = 1 , q ∤ i ∏ n m o d q k i ⎠ ⎞
等式右侧,后边两部分都不含因子 q q q
所以有
n ! q x = ⌊ n q ⌋ ! q x ′ ( ∏ i = 1 , q ∤ i q k i ) ⌊ n q k ⌋ ( ∏ i = 1 , q ∤ i n m o d q k i ) \frac{n!}{q^x} = \frac{\left\lfloor\frac{n}{q}\right\rfloor!}{q^{x'}} \left(\prod_{i=1, q \nmid i}^{q^k} i\right)^{\left\lfloor\frac{n}{q^k}\right\rfloor} \left(\prod_{i=1, q \nmid i}^{n \bmod q^k} i\right)
q x n ! = q x ′ ⌊ q n ⌋ ! ⎝ ⎛ i = 1 , q ∤ i ∏ q k i ⎠ ⎞ ⌊ q k n ⌋ ⎝ ⎛ i = 1 , q ∤ i ∏ n m o d q k i ⎠ ⎞
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 #include <cstdio> #define int long long using namespace std;int n,m,p;void exgcd (int a,int b,int &x,int &y) { if (b==0 ) x=1 ,y=0 ; else { exgcd (b,a%b,y,x); y-=a/b*x; } } int inv (int x,int P) { int a,b; exgcd (x,P,a,b); a=(a%P+P)%P; return a; } int mul (int a,int b,int P) { return (__int128)a*b%P; } int CRT (int n,int *A,int *P) { int M=1 ; for (int i=1 ;i<=n;++i)M*=P[i]; int res=0 ; for (int i=1 ;i<=n;++i) { int Mi=M/P[i],nw=Mi*inv (Mi,P[i])%M*A[i]%M; (res+=nw)%=M; } return res; } int A[20 ],P[20 ];int cnt (int n,int p) { if (n<p)return 0 ; return n/p+cnt (n/p,p); } int qpow (int a,int b,int P) { int res=1 ; while (b) { if (b&1 )res=res*a%P; a=a*a%P; b>>=1 ; } return res; } int factor (int n,int p,int pk) { if (n==0 )return 1 ; int turn=1 ,remain=1 ; for (int i=1 ;i<=pk;++i) if (i%p)(turn*=i)%=pk; for (int i=1 ;i<=n%pk;++i) if (i%p)(remain*=i)%=pk; return factor (n/p,p,pk)*qpow (turn,n/pk,pk)%pk*remain%pk; } int exlucas (int n,int m,int pk,int p) { return factor (n,p,pk) *inv (factor (m,p,pk),pk)%pk*inv (factor (n-m,p,pk),pk)%pk *qpow (p,cnt (n,p)-cnt (m,p)-cnt (n-m,p),pk)%pk; } signed main () { scanf ("%lld%lld%lld" ,&n,&m,&p); int len=0 ; for (int pi=2 ;pi<=p;++pi) { if (p%pi==0 ) { int z=1 ; while (p%pi==0 )z*=pi,p/=pi; ++len; A[len]=exlucas (n,m,z,pi); P[len]=z; } } printf ("%lld" ,CRT (len,A,P)); return 0 ; }
九、原根
阶定义
原根定义
原根判定定理
什么样的数有原根?
怎么求 n n n 的所有原根?
怎么求 n n n 的最小原根?
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 #include <bits/stdc++.h> #define int long long using namespace std;const int N=1e6 +5 ;int ok[N],phi[N],pr[N],cnt;int read () { int x=0 ,f=1 ;char c=getchar (); while (c>'9' ||c<'0' ){if (c=='-' )f=-1 ;c=getchar ();} while (c>='0' &&c<='9' ){x=x*10 +c-'0' ;c=getchar ();} return x*f; } int qpow (int a,int b,int P) { int res=1 ; while (b) { if (b&1 )res=res*a%P; a=a*a%P; b>>=1 ; } return res; } void prepare () { ok[1 ]=1 ; phi[1 ]=1 ; for (int i=2 ;i<N;++i) { if (!ok[i])pr[++cnt]=i,phi[i]=i-1 ; for (int j=1 ;j<=cnt&&i*pr[j]<N;++j) { ok[i*pr[j]]=1 ; if (i%pr[j]==0 ) { phi[i*pr[j]]=phi[i]*pr[j]; break ; } else phi[i*pr[j]]=phi[i]*phi[pr[j]]; } } } int gg[N],tot; signed main () { prepare (); int T=read (); while (T--) { int n=read (),d=read (),g=0 ; tot=0 ; vector<int >list; int m=phi[n]; for (int i=1 ;pr[i]*pr[i]<=phi[n];++i) { if (m%pr[i]==0 ) { while (m%pr[i]==0 )m/=pr[i]; list.push_back (phi[n]/pr[i]); } } if (m>1 )list.push_back (phi[n]/m); for (int i=1 ;!g&&i<=n;++i) if (__gcd(i,n)==1 ) { g=i; for (int k:list) if (qpow (g,k,n)==1 ) { g=0 ; break ; } } if (g==0 )printf ("0\n\n" ); else { int nw=1 ;tot=0 ; for (int i=1 ;i<=phi[n];++i) { (nw*=g)%=n; if (__gcd(i,phi[n])==1 ) gg[++tot]=nw; } printf ("%lld\n" ,tot);sort (gg+1 ,gg+tot+1 ); for (int i=d;i<=tot;i+=d)printf ("%lld " ,gg[i]);puts ("" ); } } return 0 ; }
十、BSGS
求解
a x ≡ b ( m o d m ) a^x \equiv b \pmod{m}
a x ≡ b ( m o d m )
其中,( a , m ) = 1 (a, m) = 1 ( a , m ) = 1 。
确定一个块的大小 B B B ,求出 a j ( j < B ) a^{j} (j < B) a j ( j < B ) ,并且存起来。
令 x = i B − j x = iB - j x = i B − j ,则有 a i B ≡ a j b ( m o d m ) a^{iB} \equiv a^j b \pmod{m} a i B ≡ a j b ( m o d m ) 。(这里用了逆元存在的性质,所以要求 ( a , m ) = 1 (a, m) = 1 ( a , m ) = 1 )
枚举 j j j 再到哈希表里面去查询 a i B a^{iB} a i B 对应的 i B iB i B 是多少即可。
当 B B B 取 m \sqrt{m} m ,复杂度为 O ( m ) O(\sqrt{m}) O ( m ) 。
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 #include <bits/stdc++.h> #define int long long using namespace std;struct Hash { int H (int x) { return x%114514 ; } int head[114514 ],nxt[100000 ],to[100000 ],w[100000 ],sze; int find (int y) { int x=H (y); for (int i=head[x];i;i=nxt[i]) if (to[i]==y)return w[i]; return -1 ; } int & operator [] (int y) { int x=H (y); for (int i=head[x];i;i=nxt[i]) if (to[i]==y)return w[i]; ++sze; to[sze]=y;w[sze]=0 ; nxt[sze]=head[x];head[x]=sze; return w[sze]; } }H; int p,b,n,B,powB,nw;signed main () { scanf ("%d%d%d" ,&p,&b,&n); B=sqrt (p); nw=1 ; for (int i=1 ;i<=B;++i)(nw*=b)%=p; powB=nw; nw=1 ; for (int i=1 ;i*B<=p+B;++i) { (nw*=powB)%=p; if (H.find (nw)==-1 )H[nw]=i*B; } int x=p;nw=1 ; for (int j=0 ;j<=B;++j) { int iB=H.find (n*nw%p); if (iB!=-1 ) x=min (x,iB-j); nw=nw*b%p; } if (x==p)printf ("no solution" ); else printf ("%d" ,x); return 0 ; }
多项式部分
一、多项式乘法
FFT (非递归版)
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 int n,m;struct cpdd { double x,y; cpdd (){} cpdd (double _x,double _y) {x=_x;y=_y;} cpdd operator + (const cpdd &q)const {return cpdd (x+q.x,y+q.y);} cpdd operator - (const cpdd &q)const {return cpdd (x-q.x,y-q.y);} cpdd operator * (const cpdd &q)const {return cpdd (x*q.x-y*q.y,x*q.y+y*q.x);} cpdd operator / (const int n)const {return cpdd (x/n,y/n);} }; cpdd F[N],G[N],H[N]; void FFT (cpdd *F,int n) { static int rev[N]; for (int i=1 ;i<n;++i) { rev[i]=(rev[i>>1 ]>>1 )|((i&1 )*(n>>1 )); if (rev[i]<i)swap (F[i],F[rev[i]]); } for (int m=2 ;m<=n;m<<=1 ) { int k=m/2 ; cpdd g=cpdd (cos (2 *pi/m),sin (2 *pi/m)); for (int i=0 ;i<n;i+=m) { cpdd omega=cpdd (1 ,0 ); for (int j=i;j<i+k;++j,omega=omega*g) { cpdd tmp=F[j|k]*omega; F[j|k]=F[j]-tmp; F[j]=F[j]+tmp; } } } } void DFT (cpdd *F,int n) { FFT (F,n); } void IDFT (cpdd *F,int n) { FFT (F,n); reverse (F+1 ,F+n); for (int i=0 ;i<n;++i)F[i]=F[i]/n; } void mul (cpdd *H,cpdd *F,cpdd *G,int len) { int n=1 ; while (n<len)n<<=1 ; DFT (F,n); DFT (G,n); for (int i=0 ;i<n;++i)H[i]=F[i]*G[i]; IDFT (H,n); } int main () { n=read ()+1 ;m=read ()+1 ; for (int i=0 ;i<n;++i)F[i].x=read (); for (int i=0 ;i<m;++i)G[i].x=read (); mul (H,F,G,n+m-1 ); for (int i=0 ;i<=n+m-2 ;++i)printf ("%d " ,(int )(H[i].x+0.5 )); return 0 ; }
FFT(三次变两次优化)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 void mul (cpdd *H,cpdd *F,int len) { int n=1 ; while (n<len)n<<=1 ; DFT (F,n); for (int i=0 ;i<n;++i)H[i]=F[i]*F[i]; IDFT (H,n); } int main () { n=read ()+1 ;m=read ()+1 ; for (int i=0 ;i<n;++i)F[i].x=read (); for (int i=0 ;i<m;++i)F[i].y=read (); mul (H,F,n+m-1 ); for (int i=0 ;i<=n+m-2 ;++i)printf ("%d " ,(int )(H[i].y/2 +0.5 )); return 0 ; }
NTT(纯净版)
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 void NTT (ll *F,int n) { static int rev[N]; for (int i=1 ;i<n;++i) { rev[i]=(rev[i>>1 ]>>1 )|((i&1 )*(n>>1 )); if (i>rev[i])swap (F[i],F[rev[i]]); } for (int m=2 ;m<=n;m<<=1 ) { int k=m/2 ; ll g=qpow (3 ,(P-1 )/m); for (int i=0 ;i<n;i+=m) { ll omega=1 ; for (int j=i;j<i+k;++j,omega=omega*g%P) { ll tmp=F[j|k]*omega%P; F[j|k]=(F[j]-tmp+P)%P; F[j]=(F[j]+tmp)%P; } } } } void DFT (ll *F,int n) { NTT (F,n); } void IDFT (ll *F,int n) { NTT (F,n); reverse (F+1 ,F+n); int iv=inv (n); for (int i=0 ;i<n;++i)F[i]=F[i]*iv%P; } void mul (ll *H,ll *F,ll *G,int len) { int n=1 ; while (n<len)n<<=1 ; DFT (F,n); DFT (G,n); for (int i=0 ;i<n;++i)H[i]=F[i]*G[i]%P; IDFT (H,n); } int main () { n=read ()+1 ;m=read ()+1 ; for (int i=0 ;i<n;++i)F[i]=read (); for (int i=0 ;i<m;++i)G[i]=read (); mul (H,F,G,n+m-1 ); for (int i=0 ;i<=n+m-2 ;++i)printf ("%lld " ,(H[i]+P)%P); return 0 ; }
素数
原根
4179340454199820289
3
1945555039024054273
5
180143985094819841
6
31525197391593473
3
7881299347898369
6
4222124650659841
19
3799912185593857
5
1337006139375617
3
1231453023109121
3
263882790666241
7
79164837199873
5
39582418599937
5
6597069766657
5
2748779069441
3
2061584302081
7
206158430209
22
77309411329
7
75161927681
3
3221225473
5
2281701377
3
2013265921
31
1004535809
3
469762049
3
167772161
3
104857601
3
23068673
3
7340033
3
5767169
3
786433
10
65537
3
40961
3
12289
11
7681
17
257
3
193
5
97
5
17
3
5
2
3
2
二、多项式牛顿迭代及其应用
问题描述:
已知 G ( x , y ) = 0 ( m o d x n ) 求 y = f ( x ) \text{已知}~ G(x, y) = 0 \pmod {x^n}\\
\text{求}~ y = f(x)
已知 G ( x , y ) = 0 ( m o d x n ) 求 y = f ( x )
倍增法:
记录 f i ( x ) f_i(x) f i ( x ) 表示模 x i x^i x i 的解。
首先,求出 f 1 ( x ) f_1(x) f 1 ( x ) 。
若已经知道了 f n ( x ) f_n(x) f n ( x ) ,要求 f 2 n ( x ) f_{2n}(x) f 2 n ( x ) 。
将 G ( x , y ) = 0 G(x, y) = 0 G ( x , y ) = 0 在 f n f_n f n 处泰勒展开,去掉零项,后化简,得到:
f 2 n ( x ) ≡ f n ( x ) − G ( x , f n ( x ) ) ∂ G ∂ y ( x , f n ( x ) ) ( m o d x 2 n ) f_{2n}(x) \equiv f_n(x) - \frac{G(x, f_n(x))}{\frac{\partial G}{\partial y}(x, f_n(x))} \pmod {x^{2n}}
f 2 n ( x ) ≡ f n ( x ) − ∂ y ∂ G ( x , f n ( x ) ) G ( x , f n ( x ) ) ( m o d x 2 n )
多项式求逆
G ( x , y ) = 1 y − h ( x ) ∂ G ∂ y ( x , y ) = − 1 y 2 G(x, y) = \frac{1}{y} - h(x)\\
\frac{\partial G}{\partial y}(x, y) = -\frac{1}{y^2}\\
G ( x , y ) = y 1 − h ( x ) ∂ y ∂ G ( x , y ) = − y 2 1
带入公式化简后有
f 2 n ( x ) ≡ 2 f n ( x ) − f n 2 ( x ) h ( x ) ( m o d x 2 n ) f_{2n}(x) \equiv 2f_n(x) - f_n^2(x)h(x) \pmod {x^{2n}}
f 2 n ( x ) ≡ 2 f n ( x ) − f n 2 ( x ) h ( x ) ( m o d x 2 n )
多项式开方
G ( x , y ) = y 2 − h ( x ) ∂ G ∂ y ( x , y ) = 2 y G(x, y) = y^2 - h(x)\\
\frac{\partial G}{\partial y}(x, y) = 2y\\
G ( x , y ) = y 2 − h ( x ) ∂ y ∂ G ( x , y ) = 2 y
带入公式化简后有
f 2 n ( x ) ≡ f n 2 ( x ) + h ( x ) 2 f n ( x ) ( m o d x 2 n ) f_{2n}(x) \equiv \frac{f_n^2(x) + h(x)}{2f_n(x)} \pmod {x^{2n}}
f 2 n ( x ) ≡ 2 f n ( x ) f n 2 ( x ) + h ( x ) ( m o d x 2 n )
多项式取对数
定理:当且仅当 [ x 0 ] h ( x ) = 1 [x^0]h(x) = 1 [ x 0 ] h ( x ) = 1 时候,h ( x ) h(x) h ( x ) 存在对数。
y = ln h ( x ) y = \ln h(x)
y = ln h ( x )
两边同时求导
y ′ = h ′ ( x ) h ( x ) y' = \frac{h'(x)}{h(x)}\\
y ′ = h ( x ) h ′ ( x )
求出来后积分回去即可。注意常数项为 0 0 0 。
多项式exp
G ( x , y ) = ln y − h ( x ) G(x, y) = \ln y - h(x)\\
G ( x , y ) = ln y − h ( x )
带入公式化简有
f 2 n ( x ) ≡ f n ( x ) ( 1 − ln f n ( x ) + h ( x ) ) ( m o d x 2 n ) f_{2n}(x) \equiv f_n(x)(1 - \ln f_n(x) + h(x)) \pmod {x^{2n}}
f 2 n ( x ) ≡ f n ( x ) ( 1 − ln f n ( x ) + h ( x ) ) ( m o d x 2 n )
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 struct Poly { vector<ll>F; Poly () {} Poly (int n) {F.resize (n);} void change (int n) { while (F.size ()<n)F.push_back (0 ); } ll& operator [] (int x) {return F[x];} int size () const {return F.size ();} }; void NTT (Poly &F,int n) { static int rev[N]; for (int i=1 ;i<n;++i) { rev[i]=(rev[i>>1 ]>>1 )|((i&1 )*(n>>1 )); if (i>rev[i])swap (F[i],F[rev[i]]); } for (int m=2 ;m<=n;m<<=1 ) { int k=m/2 ; ll g=qpow (3 ,(P-1 )/m); for (int i=0 ;i<n;i+=m) { ll omega=1 ; for (int j=i;j<i+k;++j,omega=omega*g%P) { ll tmp=F[j|k]*omega%P; F[j|k]=(F[j]-tmp+P)%P; F[j]=(F[j]+tmp)%P; } } } } Poly DFT (const Poly &F,int n,int len=-1 ) { if (len==-1 )len=n; Poly tmp (n) ; for (int i=0 ;i<len;++i)tmp[i]=F.F[i]; for (int i=len;i<n;++i)tmp[i]=0 ; NTT (tmp,n); return tmp; } Poly IDFT (const Poly &F,int n,int len=-1 ) { Poly tmp=DFT (F,n,len); reverse (tmp.F.begin ()+1 ,tmp.F.end ()); int iv=inv (n); for (int i=0 ;i<n;++i)tmp.F[i]=tmp.F[i]*iv%P; return tmp; } Poly mul (const Poly &F,const Poly &G,int n) { Poly P1=DFT (F,n,F.size ()),P2=DFT (G,n,G.size ()); Poly res (n) ; for (int i=0 ;i<n;++i)res.F[i]=P1.F [i]*P2.F [i]%P; return IDFT (res,n); } Poly Finv (const Poly &H,int n) { if (n==1 ) { Poly res (2 *n) ; res[0 ]=inv (H.F[0 ]); res[1 ]=0 ; return res; } Poly F=DFT (Finv (H,n/2 ),2 *n,n/2 ),G=DFT (H,2 *n,n); Poly res (2 *n) ; for (int i=0 ;i<2 *n;++i)res[i]=(2 *F[i]-F[i]*F[i]%P*G[i])%P; res=IDFT (res,2 *n); for (int i=n;i<2 *n;++i)res[i]=0 ; return res; } ll iv[N]; Poly Fsqrt (const Poly &H,int n) { if (n==1 ) { Poly res (2 *n) ; res[0 ]=1 ; res[1 ]=0 ; return res; } Poly F0=Fsqrt (H,n/2 ),res (2 *n); Poly F1=DFT (F0,2 *n,n/2 ),F2=DFT (Finv (F0,n),2 *n,n),F3=DFT (H,2 *n,n); for (int i=0 ;i<2 *n;++i)res[i]=(F1[i]*F1[i]+F3[i])%P*F2[i]%P*inv2%P; res=IDFT (res,2 *n); for (int i=n;i<2 *n;++i)res[i]=0 ; return res; } Poly Fln (const Poly &H,int n) { Poly F0 (2 *n) ,F1 (2 *n) ; for (int i=0 ;i<n-1 ;++i)F0[i]=H.F[i+1 ]*(i+1 )%P; for (int i=n-1 ;i<2 *n;++i)F0[i]=0 ; F0=DFT (F0,2 *n); F1=DFT (Finv (H,n),2 *n,n); for (int i=0 ;i<2 *n;++i)(F0[i]*=F1[i])%=P; F0=IDFT (F0,2 *n); for (int i=n-1 ;i>0 ;--i)F0[i]=iv[i]*F0[i-1 ]%P; for (int i=n;i<2 *n;++i)F0[i]=0 ; F0[0 ]=0 ; return F0; } Poly Fexp (const Poly &H,int n) { if (n==1 ) { Poly res (2 *n) ; res[0 ]=1 ; res[1 ]=0 ; return res; } Poly F0=Fexp (H,n/2 ),res (2 *n); Poly F1=DFT (Fln (F0,n),2 *n,n),F2=DFT (H,2 *n,n); F0=DFT (F0,2 *n,n); for (int i=0 ;i<2 *n;++i)res[i]=F0[i]*(1 -F1[i]+F2[i])%P; res=IDFT (res,2 *n); for (int i=n;i<2 *n;++i)res[i]=0 ; return res; } int main () { iv[1 ]=1 ; for (int i=2 ;i<N;++i)iv[i]=(P-P/i)*iv[P%i]%P; n=read (); int k=1 ; while (k<n)k<<=1 ; Poly F (k) ; for (int i=0 ;i<n;++i)F[i]=read (); for (int i=n;i<k;++i)F[i]=0 ; Poly H=Fexp (F,k); for (int i=0 ;i<n;++i)printf ("%lld " ,(H[i]+P)%P); return 0 ; }
三、FWT相关
or、and、xor卷积
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 void FWT_or (ll* F,int n,int flag) { int len=1 <<n; if (flag==1 ) { for (int m=2 ;m<=len;m<<=1 ) { int k=m/2 ; for (int i=0 ;i+m<=len;i+=m) for (int j=i;j<i+k;++j) (F[j|k]+=F[j])%=P; } } else { for (int m=2 ;m<=len;m<<=1 ) { int k=m/2 ; for (int i=0 ;i+m<=len;i+=m) for (int j=i;j<i+k;++j) (F[j|k]-=F[j])%=P; } } } void FWT_and (ll* F,int n,int flag) { int len=1 <<n; if (flag==1 ) { for (int m=2 ;m<=len;m<<=1 ) { int k=m/2 ; for (int i=0 ;i+m<=len;i+=m) for (int j=i;j<i+k;++j) (F[j]+=F[j|k])%=P; } } else { for (int m=2 ;m<=len;m<<=1 ) { int k=m/2 ; for (int i=0 ;i+m<=len;i+=m) for (int j=i;j<i+k;++j) (F[j]-=F[j|k])%=P; } } } void FWT_xor (ll* F,int n,int flag) { int len=1 <<n; if (flag==1 ) { for (int m=2 ;m<=len;m<<=1 ) { int k=m/2 ; for (int i=0 ;i+m<=len;i+=m) for (int j=i;j<i+k;++j) { int tmp=F[j]; F[j]=(F[j]+F[j|k])%P; F[j|k]=(tmp-F[j|k])%P; } } } else { for (int m=2 ;m<=len;m<<=1 ) { int k=m/2 ; for (int i=0 ;i+m<=len;i+=m) for (int j=i;j<i+k;++j) { int tmp=F[j]; F[j]=(F[j]+F[j|k])*inv2%P; F[j|k]=(tmp-F[j|k])*inv2%P; } } } }
不交并(子集卷积)
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 #include <bits/stdc++.h> #define ll long long using namespace std;const int N=20 ;const ll P=1e9 +9 ,inv2=(P+1 )/2 ;ll A[N+1 ][1 <<N],B[N+1 ][1 <<N],H[N+1 ][1 <<N]; int cnt[1 <<N]; int read () { int x=0 ,f=1 ;char c=getchar (); while (c>'9' ||c<'0' ) {if (c=='-' )f=-1 ;c=getchar ();} while (c>='0' &&c<='9' ) {x=x*10 +c-'0' ;c=getchar ();} return x*f; } void FWT_or (ll* F,int n,int flag) { int len=1 <<n; if (flag==1 ) { for (int m=2 ;m<=len;m<<=1 ) { int k=m/2 ; for (int i=0 ;i+m<=len;i+=m) for (int j=i;j<i+k;++j) (F[j|k]+=F[j])%=P; } } else { for (int m=2 ;m<=len;m<<=1 ) { int k=m/2 ; for (int i=0 ;i+m<=len;i+=m) for (int j=i;j<i+k;++j) (F[j|k]-=F[j])%=P; } } } int n;signed main () { n=read (); for (int i=1 ;i<(1 <<n);++i)cnt[i]=cnt[i>>1 ]+(i&1 ); for (int i=0 ;i<(1 <<n);++i)A[cnt[i]][i]=read (); for (int i=0 ;i<(1 <<n);++i)B[cnt[i]][i]=read (); for (int i=0 ;i<=n;++i)FWT_or (A[i],n,1 ),FWT_or (B[i],n,1 ); for (int i=0 ;i<=n;++i) { for (int j=0 ;j<=i;++j) for (int k=0 ;k<(1 <<n);++k) (H[i][k]+=A[j][k]*B[i-j][k])%=P; FWT_or (H[i],n,-1 ); } for (int i=0 ;i<(1 <<n);++i)printf ("%lld " ,(H[cnt[i]][i]+P)%P); return 0 ; }
四、半在线卷积
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 void Solve (Poly &F,const Poly &G,int l,int r) { if (r-l==1 ) { if (l==0 )++F[l]; return ; } int mid=(l+r)>>1 ; Solve (F,G,l,mid); int m=2 *(r-l); Poly P1 (m) ,P2 (m) ; for (int i=0 ;i<m/4 ;++i)P1[i]=F[l+i]; for (int i=m/4 ;i<m;++i)P1[i]=0 ; P2[0 ]=0 ; for (int i=1 ;i<m/2 ;++i)P2[i]=G[i]; for (int i=m/2 ;i<m;++i)P2[i]=0 ; NTT (P1,m);NTT (P2,m); for (int i=0 ;i<m;++i)(P1[i]*=P2[i])%=P; NTT (P1,m,-1 ); for (int i=mid;i<r;++i) (F[i]+=P1[i-l])%=P; Solve (F,G,mid,r); }