winliu
    • 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
    • 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 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
    CSES Problem Set — Common Divisors 題解 === 題目 --- 給你一個陣列裝有 $n$ 個正整數,你的任務是找到其中兩個正整數使得它們的最大公因數是最大的。 ### 輸入 - 第一行輸入一個整數 $n$,代表陣列的長度。($2 \le n \le 2 \cdot 10^5$) - 第二行有 $n$ 個正整數 $x_i$,代表陣列的內容。($1 \le x_i \le 10^6$) ### 輸出 輸出最大的最大公因數。 範例測資 --- ``` Input : 5 3 14 15 7 9 Output : 7 ``` 最大的最大公因數是 $14$ 跟 $7$ 的最大公因數 $\gcd(14, 7) = 7$。 想法:統計題目範圍的正整數是陣列中多少個數字的正因數 --- 我們可以用一個列表統計題目範圍的各個正整數 $a$ 是陣列中多少個 $x_i$ 的正因數,如果一個正整數 $a$ 是陣列中兩個以上的 $x_i$ 的正因數,那麼它就是公因數。我們只要找最大的正整數 $a$ 使得它被統計到兩個以上,那麼這個 $a$ 就會是最大的最大公因數。 要做這個統計,我們跑遍陣列裡的各個 $x_i$,並各自嘗試除以題目範圍的每個正整數 $a$。如果可以除盡(餘數為 $0$),那 $a$ 就是 $x_i$ 的正因數,我們就在統計列表中為 $a$ 加一。 我們可以觀察到,如果 $a$ 是 $x_i$ 的正因數,那麼 $\dfrac{x_i}{a}$ 也會是 $x_i$ 的正因數。正因數都是能兩個兩個配對成 $\bigl(a, \dfrac{x_i}{a}\bigr)$ 一組的,除了當 $x_i$ 是完全平方數的時候會有一組 $\bigl(\sqrt{x_i}, \sqrt{x_i}\bigr)$ 是兩個相等的正整數。 總而言之,我們其實嘗試那些不大於 $\sqrt{x_i}$ 的正整數就好。如果找到了正因數 $a < \sqrt{x_i}$,那麼我們就一次找到了兩個相異的正因數 $a$ 與 $\dfrac{x_i}{a}$;而如果找到了正因數 $a = \sqrt{x_i}$ 時,我們就只有找到一個正因數。 因為我們要找的是統計列表中被記錄大於等於 $2$ 最大位置,從有可能的公因數 $\max(x_i)$ 開始往下找第一個大於等於 $2$ 的位置就好。 這個算法的時間複雜度會是 $O(n \cdot \sqrt{\max(x_i)})$。 ### 範例程式碼 ```cpp= #include <iostream> using namespace std; #define N 1000001 int count[N] = {0}; void countDivisors(int x) { for (int a = 1; a*a <= x; a++) { if (x % a != 0) continue; count[a] += 1; if (x / a != a) count[x / a] += 1; } } int main() { int n, x, m=0; cin >> n; for (int i = 0; i < n; i++) { cin >> x; countDivisors(x); if (x > m) m = x; } for (int a = m; a > 0; a--) { if (count[a] >= 2) { cout << a; break; } } } ``` 想法:在題目範圍的正整數中看各自有多少倍數在陣列裡 --- 我們也可以反過來看題目範圍的所有正整數 $a$ 有多少倍數出現在陣列裡面,如果正整數 $a$ 有兩個以上的倍數出現在陣列裡,那麼 $a$ 就是公因數。我們只要找最大的正整數 $a$ 使得它有兩個以上的倍數出現在陣列裡,那麼這個 $a$ 就會是最大的最大公因數。 我們可以把每個數字出現在陣列的次數先儲存在一個列表中。這麼做就不需要一個個試除,而是直接從因數的角度把它所有倍數出現次數加總。 而且因為我們要找的是最大的正整數 $a$ 使得它有兩個以上的倍數出現在陣列裡,從有可能的公因數 $\max(x_i)$ 開始往下找第一個滿足條件的數字就好。 這個算法的時間複雜度會是 $O(N \log N)$,其中 $N = \max(x_i)$。 > 計算這個複雜度時要注意到調和級數是呈對數增長的:$\displaystyle \sum_{k=1}^N \frac{1}{k} \approx \log N$。 ### 範例程式碼 ```cpp= #include <iostream> using namespace std; #define N 1000001 int count[N] = {0}; int main() { int n, x, m=0, cnt; cin >> n; for (int i = 0; i < n; i++) { cin >> x; count[x] += 1; if (x > m) m = x; } for (int a = m; a > 0; a--) { cnt = 0; for (int ka = a; ka <= m; ka += a) { cnt += count[ka]; } if (cnt >= 2) { cout << a; break; } } } ```

    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