syru
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # The Extended Euclidean Algorithm $ax+by=\gcd(a,b)$를 만족하는 베주 항등식(Bézout's identity)의 계수(Coefficient)를 x, y를 구하는 알고리즘이다. ## 베주 항등식 ### 정의 둘 중 하나는 0이 아닌 정수 a,b에 대해 gcd(a,b)=d라고 하면 다음 3가지 명제를 만족한다. 1) ax+by=d를 만족하는 정수 x,y가 존재한다. 2) d는 정수 x,y에 대하여 ax+by로 표현할 수 있는 가장 작은 정수이다. 3) ax+by로 표현될 수 있는 모든 정수는 d의 배수이다. ### 증명 둘 중 하나는 0이 아닌 정수 a, b에 대해서 집합 S를 다음과 같이 정의한다. $$ S = \left \{ax+by \gt 0 \mid x,y \in \Bbb{Z} \right \} $$ 이 집합이 공집합이 아니라면 정렬성의 원리에 의해 최소 원소를 가진다. > 공집합이 아니고, 음이 아닌 정수들을 원소로 갖는 모든 집합 S는 최소 원소를 가진다. > 다시 말해, S는 S에 속하는 모든 원소 b에 대해 $a\leq b$ 를 만족하는 a를 포함한다. ( 정수에 대한 정의까지 들어가고 싶다면 [이 글](https://chocobear.tistory.com/36?category=851735)을 추천한다. ) 그렇다면 공집합이 아닌지를 증명해보자 $$ if \; a\gt 0, \lvert a \rvert = a \cdot 1 + b \cdot 0 \in S \\ if \; a\lt 0, \lvert a \rvert = a \cdot -1 + b \cdot 0 \in S \\ if \; a = 0, \; then \; b \neq 0, \; \lvert a \rvert = 0 \cdot x + b \cdot 0 \in S $$ 모든 a에 대하여 $\lvert a \rvert$가 집합 S에 속하므로 공집합이 아니다. 반대로 b를 가지고 해도 동일 결과가 난다, 즉 $\lvert a \rvert$, $\lvert b \rvert$ 중에 적어도 하나는 집합 S에 속한다. 그렇게 ax+by로 나타낼 수 있는 최소 원소를 가짐을 보였고 이 값을 d라고 하겠다. $$ d = ak + bl \; (k,l \in \Bbb{Z}) \\ x = au + bv \; (u,v \in \Bbb{Z}) \\ x = dq + r \; ( 0 \leq r \lt d) \\ if \; d \nmid x, \; then \; r \neq 0 \; , \; r \in \Bbb{N} \\ au + bv = (ak + bl) \cdot q + r \\ a(u-kq) + b(v-kl) = r \in S \\ r \lt d, but \; \forall x \in S \gt d \\ thus \; d \mid x $$ 위에서 증명했듯이 집합 S의 모든 원소는 d를 약수로 가진다. $$ a\cdot(1) + b\cdot(0) = a \\ a\cdot(0) + b\cdot(1) = b $$ a,b도 집합 S의 원소이므로 d는 a,b의 공약수이다. e가 a,b의 공약수라 하면 a = a'e, b = b'e라 할 수 있다. d = ak + bl = e(a'k + b'l) 이므로 e|d이다. 모든 공약수가 d의 약수이므로 d = gcd(a,b) 이로써 최소원소 d 가 gcd(a,b) 임을 보였고 일련의 과정을 통해 3가지 명제를 만족함을 보였다. ## 확장된 유클리드 알고리즘 ### 유클리드 알고리즘 $$ a = bq+r \; (0 \leq r \lt b)\\ \gcd(a,b) = \gcd(b,r) $$ 두번째 식을 증명해보자면 $$ d = gcd(a,b) \\ a = dA,\; b = dB \\ dA = dBq + r \\ r = d(A-Bq) \\ gcd(b,r) = gcd(dB, d(A-Bq)) $$ $B$와 $A-Bq$가 서로소가 아니라고 가정하자 $$ A-Bq = kB \\ A = B(K+q) $$ 하지만 A, B는 서로소여야 하므로 모순이다. 즉 $B$와 $A-Bq$가 서로소이며 $gcd(b,r) = gcd(dB, d(A-Bq)) = d$ 를 만족한다. 해당 알고리즘을 활용하기 위하여 $a_n$의 점화식을 세워보자 $a_0 = a, a_1 = b$ 라 정의하면 $a_{n-1} = a_nq_n + a_{n+1}$ 으로 전개가 가능하며 $a_{n+1} = 0$ 일 때 $a_n = gcd(a,b)$ 이다. ### 전개 우리는 a,b를 알고 있고 x, y를 알려고 한다. $$ y_n = 0, \; y_{n-1}=1 \\ y_{k-1} = y_kq_k + y_{k+1} \quad (for \; k = n-2, \cdots ,2,1) $$ 위와 같은 $y_n$의 점화식을 정의해보자 이 때 $(1 \leq k \leq n)$ 에서 $(-1)^{n+k+1}a_{k-1}y_k + (-1)^{n+k}a_ky_{k-1} = a_n$이 성립한다. $$ k = n, (-1)^{2n+1}a_{n-1}y_n + a_ny_{n-1} = a_n \\ k = k-1, (-1)^{n+k}a_{k-2}y_{k-1} + (-1)^{n+k-1}a_{k-1}y_{k-2} \\ since \; (-1)^2 = 1 \; (-1)^{n+k}a_{k-2}y_{k-1} + (-1)^{n+k+1}a_{k-1}y_{k-2} $$ $a_{k-2} = a_{k-1}q_{k-1} + a_k$ 이므로 $$ =(-1)^{n+k}(a_{k-1}q_{k-1} + a_k)y_{k-1} + (-1)^{n+k+1}a_{k-1}y_{k-2} \\ =(-1)^{n+k}a_ky_{k-1} + (-1)^{n+k}(a_{k-1}q_{k-1}y_{k-1}) + (-1)^{n+k+1}a_{k-1}y_{k-2} \\ =(-1)^{n+k}a_ky_{k-1} + (-1)^{n+k+1}((-1)\cdot a_{k-1}q_{k-1}y_{k-1}) + (-1)^{n+k+1}a_{k-1}y_{k-2} \\ =(-1)^{n+k}a_ky_{k-1} + (-1)^{n+k+1}a_{k-1}(y_{k-2} - q_{k-1}y_{k-1}) $$ $y_{k-2} - y_{k-1}q_{k-1} = y_k$ 이므로 $$ =(-1)^{n+k}a_ky_{k-1} + (-1)^{n+k+1}a_{k-1}y_k = a_n $$ 일련의 과정을 통해 성립함을 보였고 이제 k=1을 대입하면 $(-1)^na_0y_1 + (-1)^{n+1}a_1y_0 = a_n$ 이다. 우리가 알고싶은 값들은 각각 $x = (-1)^ny_1, \; y = (-1)^{n+1}y_0$ 임을 알게 되었다. ### 구현 마지막으로 해당 코드를 구현해보자 y_n을 구하면서 q_n을 구하자니 방향이 서로 엇갈려서 q_n을 미리 다 구해두고 y_n을 구하도록 하겠다. ``` python def egcd(a, b): _q = [] while b != 0: _q.append(a//b) a,b = b, a%b n = len(_q) _q.pop() # [y_{n-2}, y_{n-1}, y_{n}] y = [0,1,0] for q in reversed(_q): y[0] = y[1] * q + y[2] y[2], y[1] = y[1], y[0] return (-1)**n * y[2], (-1)**(n+1) * y[1] ``` 구글링을 좀 해보니 보통 아래와 같은 방식으로 구한다. 근데 해당 방식의 증명 방법이 맘에 들지 않아서 난 쓰진 않았다. (코드가 아래가 훨 깔끔해보여서 맘에 드는 증명 방식을 찾으면 바로 아래로 전환할려고 한다... ) ``` python def egcd(a, b): u, u1 = 1, 0 v, v1 = 0, 1 g, g1 = a, b while g1: q = g // g1 u, u1 = u1, u - q * u1 v, v1 = v1, v - q * v1 g, g1 = g1, g - q * g1 return u, v, g ``` 내 코드에서 gcd 값도 반환해주고 싶다면 a, b를 백업해두고 결과로 나오는 s,t 에 곱해서 sa+bt를 계산하면 된다.

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    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.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully