or
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up
Syntax | Example | Reference | |
---|---|---|---|
# Header | Header | 基本排版 | |
- Unordered List |
|
||
1. Ordered List |
|
||
- [ ] Todo List |
|
||
> Blockquote | Blockquote |
||
**Bold font** | Bold font | ||
*Italics font* | Italics font | ||
~~Strikethrough~~ | |||
19^th^ | 19th | ||
H~2~O | H2O | ||
++Inserted text++ | Inserted text | ||
==Marked text== | Marked text | ||
[link text](https:// "title") | Link | ||
 | Image | ||
`Code` | Code |
在筆記中貼入程式碼 | |
```javascript var i = 0; ``` |
|
||
:smile: | ![]() |
Emoji list | |
{%youtube youtube_id %} | Externals | ||
$L^aT_eX$ | LaTeX | ||
:::info This is a alert area. ::: |
This is a alert area. |
On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?
Please give us some advice and help us improve HackMD.
Do you want to remove this version name and description?
Syncing
xxxxxxxxxx
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →2019 Week 14: Number theory
在演算法競賽中,通常涉及的數論大部份是整數的代數性質
Modular arithmetic (同餘運算)
在競賽中若計算結果超出了數值範圍外,則通常會要求結果對某個數字取餘數
所以得熟悉取完餘數後的數字們之間的關係及運算。
練習:
CODEFORCES 1165A Remainder
若數字們都對 \(n\) 取餘數後,它們就處於"同餘 \(n\)"的狀態下了
將 \(a\) 除以 \(n\) 的餘數記做 \(a \space (\text{mod } n)\)
若 \(a \space (\text{mod } n)\) 與 \(b \space (\text{mod } n)\) 相等,則記 \(a \equiv b \space (\text{mod } n)\)
並且若
\[a \equiv b \mod n \\c \equiv d \mod n\]
則
\[a+c \equiv b+d \mod n\\a\times c \equiv b\times d \mod n\]
練習:
CODEFORCES 1151C Problem for Nazar
CODEFORCES 1165E Two Arrays and Sum of Functions[1]
歐拉定理
如果 \(a, n\) 互質,那麼
\[a^{\phi(n)} \equiv 1 \mod n\]
這裡 \(\phi(n)\) 是與 \(n\) 互質且小於 \(n\) 的正整數的個數
費馬小定理
如果 \(a, p\) 互質,且 \(p\) 為質數,那麼
\[a^{p-1} \equiv 1 \mod p\]
Greatest Common Divisor
中譯 最大公因數,以下簡稱 GCD
給定整數 \(a, b\),它倆各自有自己的因數[2],取相同的因數中最大的數,即最大公因數 \(\gcd(a, b)\)
#include<algorithm>
有內建的__gcd
函數練習:
CODEFORCES 1155C Alarm Clocks Everywhere
CODEFORCES 1133D Zero Quantity Maximization
CODECHEF SnackDown 2019 Round 1A Chef and Periodic Sequence
CODEFORCES 1107D Compression
GCD 性質
Euclidean algorithm
給定整數 \(a, b\) 求出 \(\gcd(a, b)\)
令 \(a_0=a, b_0=b\),根據 GCD 性質:
\[ \begin{split} \gcd(a_0, b_0) &= \gcd(b_0 \% a_0 = \underline{b_1}, a_0) \\ \gcd(\underline{b_1}, a_0) &= \gcd(a_0 \% b_1 = \underline{a_1}, b_1) \\ \gcd(\underline{a_1}, b_1) &= \gcd(b_1 \% a_1 = b_2, a_1) \\ &\vdots \\ \gcd(a_i, b_i) &= \gcd(b_i \% a_i = a_{i+1}, b_i) \\ &\vdots \\ \gcd(\mathbf{0}, b_n) &= |b_n| \end{split} \]
練習:
證明上述過程 \(\gcd\) 中左參數比右參數要先變為 \(0\)
綜合上面過程,實作程式碼:
練習:
UVa OJ 10407 Simple division
Bezout’s Theorem
對於所有整數 \(a, b\),存在整數 \(x, y\) 使得 \(ax + by = \gcd(a, b)\)
Extended Euclidean algorithm
給定整數 \(a, b\) 求出整數 \(x, y\) 滿足 \(ax + by = \gcd(a, b)\)
令 \(g = \gcd(a, b)\),配合 Bezout’s Thm 得到:
\[ 0\cdot x_n + g\cdot y_n = g\]
明顯的,\(x_n \in \mathbb{Z}, y_n = 1\)
而根據 GCD 性質 \(\gcd(a_i, b_i) = \gcd(b_i\%a_i, a_i)\)
以及 \(b_i \% a_i = b_i - \lfloor{b_i\over a_i}\rfloor \cdot a_i\)
得到
\[ \begin{split} g &= x_j\cdot (b_i \% a_i) &+ y_j &\cdot a_i \\ &= x_j\cdot (b_i - \lfloor{b_i\over a_i}\rfloor \cdot a_i) &+ y_j &\cdot a_i \\ &= x_j \cdot b_i &+ (y_j - \lfloor{b_i\over a_i}\rfloor \cdot x_j) &\cdot a_i \end{split} \]
也就是說
\[ g = a_i \cdot x_{j-1} + b_i \cdot y_{j-1} \text{ for }\begin{cases} x_{j-1} = y_j - \lfloor{b_i\over a_i}\rfloor \cdot x_j \\ y_{j-1} = x_j \end{cases} \]
綜合上述過程:
練習:
UVa OJ 10104 Euclid Problem
UVa OJ 10090 Marbles
UVa OJ 10413 Crazy Savages
請留意 mod 後大小關係會改變QQ ↩︎
整數 的因數為可整除 的數 ↩︎
Wikipedia/ 輾轉相除法的演示動畫 ↩︎