Gcd 与 Lcm
[ ext{lcm}(S)=prod_{Tsubseteq S}gcd(T)^{{(-1)}^{|T|+1}}\
f(n)=af(n-1)+bf(n-2),gcd(a,b)=1 \ gcd(f(x),f(y))=f(gcd(x,y))\
gcd(x^a-1,x^b-1)=x^{gcd(a,b)}-1
]
欧拉定理
[a^{phi(p)}=1 ( ext{mod } p) (gcd(a,p=1))
]
扩展欧拉定理
[a^b=a^{min (b\%phi(p)+phi(p),b)}( ext {mod }p)
]
中国剩余定理
[forall i<j le n gcd(b_i,b_j)=1\
egin{cases}
x≡a_1mod b_1\
x≡a_2mod b_2\
...\
x≡a_nmod b_n\
end{cases}\
x=sum_{i = 1}^na_i imes ans_i imesprod_{j=1}^n b_j imesfrac 1 {b_i}
]
扩展中国剩余定理
[∃ i<j le n gcd(b_i,b_j)>1\
egin{cases}
x≡a_1mod b_1\
x≡a_2mod b_2\
end{cases}\
x=b_1x_1+a_1=b_2x_2+a_2\
b_1x_1+(-b_2)x_2=a_2-a_1\
x=b_1x_1+a_1 mod ext{lcm}(b_1,b_2)
]
卢卡斯定理
[pin
m Prime
\inom n m =inom {lfloorfrac n p
floor}{lfloorfrac m p
floor} imesinom {n ext{ mod }p}{m ext{ mod }p}
]
扩展卢卡斯定理
[P = prod_{i = 1}^np_i^{k_i} \ inom n m=frac{frac {n!}{p_i^{u}}}{frac {m!}{p_i^{v}} frac{{(n-m)!}}{p_i^w}} imes p_i^{u-v-w}mod p_i^{k_i}\ ext{Let } f(n)=max(x) ext{ makes }n!mod p_i^x=0\
g(n)=frac{n!}{p_i^{f(n)}}\
f(n)=f(lfloorfrac{n}{p_i}
floor) imeslfloorfrac{n}{p_i}
floor\
g(n)=g(lfloorfrac{n}{p_i}
floor) imes frac{p_i^{k_i}!}{p_i^{u_1}} imes frac {(n ext{ mod }p_i^{k_i})!}{p_i^{u2}}
\ ext{Finally just merge each ans for mod }p_i^{k_i} ext{ by CRT.}
]
BSGS 算法
[a^x=b ext{ (mod p)}\
x = pm-q (m=sqrt p)\
a^{pm-q}=b ext{ (mod p)}\
a^{pm}=b imes a^q ext{ (mod p)}\
]
扩展 BSGS 算法
[a imes b ext{ mod } p=frac a d imes frac b d ext{ mod } frac p d\ ext{Let d = gcd(a, p)}\ a^{x-1} imesfrac a d = frac{b}{d}( ext{mod } frac p {d})\ ext{Specially When b = 1, x = 0.}
]
组合数
[inom n m =inom {n - 1} m+inom {n - 1}{m - 1}\
sum_{i = 0}^ninom n i=2^i \
sum_{i = B}^{n} inom i B=inom {n + 1} {B + 1}\
inom n {a + b}=sum_{i = 0}^n inom i a inom {n - i } b\
inom n m =sum_{j = 0}^m inom {n - m - 1 + j} {j}
]
二项式定理
[(a+b)^n=sum_{i = 0}^n inom n i a^ib_{n-i}
]
二项式反演
[f(n)=sum_{i = 0}^n inom n ig(i)→g(n)=sum_{i = 0}^n(-1)^{n-i}inom n i f(i)\ f(k)=sum_{i = k}^ninom i kg(i)→g(k)=sum_{i = k}^n(-1)^{i-k}inom i k f(i)
]
上升幂与下降幂
[x^{underline{k}}=prod_{i = 0}^{k-1}(x-i),x^{overline{k}}=prod_{i = 0}^{k - 1}(x+i)\x^{underline{k}}=inom x k imes k!
]
斯特林数
[genfrac{[}{]}{0pt}{}{n}{m}=(n-1)genfrac{[}{]}{0pt}{}{n-1}{m}+genfrac{[}{]}{0pt}{}{n-1}{m-1}\
genfrac{{}{}}{0pt}{}{n}{m}=mgenfrac{{}{}}{0pt}{}{n-1}{m}+genfrac{{}{}}{0pt}{}{n-1}{m-1}\
genfrac{{}{}}{0pt}{}{n}{m}=frac 1 {m!}sum_{k = 0}^m(-1)^kinom m k (m-k)^n\
x^{overline n}=sum_{i = 0}^n genfrac{[}{]}{0pt}{}{n}{i}x^i,x^n=sum_{i = 0}^ngenfrac{{}{}}{0pt}{}{n}{i}x^{underline{i}}\
x^{underline n}=(-1)^n(-x)^{overline n}\
x^{underline{n}}=sum_{i = 0}^n(-1)^{n-i}genfrac{[}{]}{0pt}{}{n}{i}x^i,x^n=sum_{i = 0}^n(-1)^{n-i}genfrac{{}{}}{0pt}{}{n}{i}x^{overline i}\
n!=sum_{i = 0}^ngenfrac{[}{]}{0pt}{}{n}{i}
]
斯特林反演
[f(n)=sum_{i = 0}^ngenfrac{{}{}}{0pt}{}{n}{i}g(i)→g(n)=sum_{i = 0}^n(-1)^{n-i}genfrac{[}{]}{0pt}{}{n}{i}f(i)
]
常见积性函数
[ ext{Let n = }prod_{i = 1}^mp_i^{k_i}\
e(n)=[n=1]\ I(n)=1\id(n)=(n)\ mu(n)=[max_{i = 1}^m(k_i)le1](-1)^m
\ phi(n)=sum_{i = 1}^n[gcd(i,n)=1]]
狄利克雷卷积
[(f*g)(n) = sum_{d|n}f(d)g(frac n d)\ f*g=g*f\(f*g)*h=f*(g*h)\
(f+g)*h=f*h+g*h\f*e=f]
积性函数相关套路
[sum_{d|n}mu(d)=[n=1],e=mu*1\ sum_{d | n}phi(d)=n, phi*I=id\ sum_{d|n}frac {mu(d)}{d}=frac {phi(n)} n,phi=mu*id
]
杜教筛
[f*g=h, ext{How to get the sum of f.}\sum_{i = 1}^nh(i)=sum_{i = 1}^nsum_{d | i}f(d)g(frac i d) \ sum_{i = 1}^nh(i)=sum_{d = 1}^ng(d)sum_{k = 1}^{lfloorfrac n d
floor}f(k) \
sum_{i = 1}^nh(i)-sum_{d = 2}^ng(d)sum_{k = 1}^{lfloorfrac n d
floor}f(k) =g(1)sum_{i = 1}^nf(i) ]
FFT 共轭优化
[F(a+bi)=F(a)+iF(b)\
(overline{F(a+bi)})'=F(a)-iF(b)\
]