mtmatt
    • 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
    • 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 Versions and GitHub Sync Note Insights 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
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    --- tags: 程式設計,APCS與競賽入門 slideOptions: transition: 'fade' # 換頁動畫 --- # 常用問題解決方法 --- ## 目錄 - 前綴和 - 差分 - 快速冪 - for迴圈 --- ## 前綴和 ---- ### 靜態求取區間和 | `index` | 1 | 2 | 3 | 4 | 5 | 6 | 7 | | ------- | --- | --- | --- | --- | --- | --- | --- | | `value` | 5 | 6 | 2 | 8 | 10 | 1 | 4 | - `2~5 = 26` - `1~7 = 36` ---- 共 $N$ 個元素,進行 $Q$ 次 $(N,Q \le 5 \times 10^5)$ - 暴力最差 $\to O(N \times Q)$ - $(5 \times 10^5)^2 \approx 2.5 \times10^{11}$ - $TLE$ $\to$ $sad$ :cry: ---- ### 如何改善 ---- 新增一個數列,第 $k$ 個存 `1~k` 的總和 | `index` | 1 | 2 | 3 | 4 | 5 | 6 | 7 | | --------------- | --- | --- | --- | --- | --- | --- | --- | | `value(V)` | 5 | 6 | 2 | 8 | 10 | 1 | 4 | | `prefix sum(P)` | 5 | 11 | 13 | 21 | 31 | 32 | 36 | 區間 `[l,r]` 總合為 $P_r-P_{l-1}$ ,特別定義 $P_0=0$ ---- code ```cpp= [1-9|7-9] const int N=500010; int n; int v[N],psum[N]; // 建構 prefix sum 陣列 for(int i=1;i<=n;++i){ psum[i]=psum[i-1]+v[i]; } ``` --- ## 差分 ---- 差分是跟前綴和相反的操作,每一項都存與前一項的差 | `index` | 1 | 2 | 3 | 4 | 5 | 6 | 7 | | --------------- | --- | --- | --- | --- | --- | --- | --- | | `value(V)` | 5 | 6 | 2 | 8 | 10 | 1 | 4 | | `Difference(D)` | 5 | 1 | -4 | 6 | 2 | -9 | 3 | ---- ### 應用:區間加值,單次詢問 [APCS 2021/11 3.生產線](https://zerojudge.tw/ShowProblem?problemid=g597) --- ## 複雜度比較 ---- | 技巧 | 單點修改 | 區間修改 | 區間查詢 | | ------ |:---------------- |:----------------- | ---------------- | | 前綴和 | $O(n)$ | $O(n)$ | $O(1)$ | | 差分 | $O(1)$ | $O(1)$ | $O(n)$ | | BIT | $O(\log_2{(n)})$ | $O(n\log_2{(n)})$ | $O(\log_2{(n)})$ | --- ## 快速冪 ---- - 計算 $a^x$ - 暴力 $\to O(x)$ - 如果 $x>10^8 \to TLE \to sad$ :cry: - 數字過大,因此每次都要取餘數 $(a=a \times a \; \% \; b)$ ---- ### 解法 $1$ $:$ 遞迴 1. $a^{13} = a^6 \times a^6 \times a$ 2. $a^6 = a^3 \times a^3$ 3. $a^3 = a \times a \times a$ ---- 歸納出 - $if \; x==0 \to return \; 1$ - $if \; x==1 \to return \; a$ - $if \; x \% 2 == 1 \to return \; a^{\frac{x}{2}} \times a^{\frac{x}{2}} \times a$ - $if \; x \% 2 == 0 \to return \; a^{\frac{x}{2}} \times a^{\frac{x}{2}}$ ---- first code ```cpp= int POW(int a,int x){ if(x==0) return 1; if(x==1) return a; if(x%2==1) return POW(a,x/2)*POW(a,x/2)*a; // x%2==0 return POW(a,x/2)*POW(a,x/2); } ``` ---- 分析複雜度 $($ 或讓電腦跑跑看 $)$ - 奇怪,怎麼還是一樣慢 ```cpp= if(x%2==1) return POW(a,x/2)*POW(a,x/2)*a; // x%2==0 return POW(a,x/2)*POW(a,x/2); ``` - 看看這兩行,是不是有多餘的步驟 ---- nice code ```cpp= int POW(int a,int x){ if(x==0) return 1; if(x==1) return a; int t=POW(a,x/2); if(x%2==1) return t*t*a; // x%2==0 return t*t; } ``` 複雜度 $O(\log_2(x))$ ---- 其實沒有那麼複雜 - 將奇數次方 $x$ 分解成 $a^{x-1} \times a$ 簡化版 ```cpp= int POW(int a,int x){ if(x==0) return 1; if(x%2==1) return POW(a,x-1)*a; // x%2==0 int t=POW(a,x/2); return t*t; } ``` ---- ### 解法 $2$ $:$ 迴圈建構 ---- 想像將 $a^x$ 中的 $x$ 以二進位表示 - $Ex :$ $13_{(10)} = 00001101_{(2)}$ - $a^{(2^k)}$ 可以在 $O(k) = O(\log_2{2^k})$ 內完成 - 將所需的次數乘進去 - $13 = 8 + 4 + 1 \to a^{13} = a^8 \times a^4 \times a$ ---- while 迴圈實作 ```cpp= using ll=long long; const ll MOD=1e9+7; ll POW(ll a,ll x){ ll ret=1; while(x>0){ if(x%2==1) ret=ret*a%MOD; a=a*a%MOD; x/=2; } return ret; } ``` --- ## C++11</br>for 迴圈進階用法 ---- ```cpp= vector<int> v(1000); // 這樣也可以用來輸入 for(int &i:v){ cin>>i; } for(int a:v){ cout<<a<<" "; } ``` --- ## Struct ---- 舉個有趣的例子(除了對...而言) ```cpp= struct Friend{ string type,name;// ... // 建構式 Friend(string _name,string tp="cf"){ name=_name; type=tp; } friend operator<(Friend a,Friend b){ if(a.type=="gf"){ if(b.type=="gf"){ cout<<"你完蛋喽!"<<"\n"; } return false; }else if(a.type=="bf"){ if(b.type=="bf"){ cout<<"妳完蛋喽!"<<"\n"; } return false; }else{ return a.name<b.name; } } } struct Friends{ vector<Friend> fs; void add_friend(string name,string type="cf"){ fs.emplace_back(name,type); } bool find_by_name(string name){ return count(fs.begin(),fs.end(),name); } bool find_gf(){ for(auto f:fs){ if(f.type=="gf"){ return true; } } return false; } bool find_bf(){ for(auto f:fs){ if(f.type=="bf"){ return true; } } return false; } void check(){ int a=0; for(auto f:fs){ if(f.type=="gf" || f.type=="bf"){ a++; } } if(a>1){ cout<<"你準備要去世喽!"; } } void sortFriends(){ sort(fs.begin(),fs.end()); } void printFriends(){ for(auto f:fs){ cout<<setw(20)<<f.name<<f.type<<"\n"; } } } int main(){ Friends mtmatt; mtmatt.add_friend("dws(戴偉璿)"); mtmatt.add_friend("lju(李卓岳)"); mtmatt.add_friend("lj(羅傑)","ac");// acquaintance 點頭之交 mtmatt.add_friend("cpp","tool"); mtmatt.sortFriends(); mtmatt.printFriends(); } ``` --- ## 大家加油 ![](https://cdn.fbsbx.com/v/t59.2708-21/271841384_664153871386781_1603587410658112198_n.gif?_nc_cat=102&ccb=1-5&_nc_sid=041f46&_nc_ohc=Nofwb69mSBYAX8z_Ny2&_nc_oc=AQlaqvFxP_hTxgIyq7KhXSYh1ApA9Lv11UVEsAVZAjW9Q-CabbAgO_JwKStj6VTnd8ZyByn2OlHfy6wz715XFEj4&_nc_ht=cdn.fbsbx.com&oh=03_AVK6a4EkhN-BHOb9uRUmUAMlRFAglnK432_u9xb1y-RQ_Q&oe=626319CE) ---- ## 大家加油 ![](https://c.tenor.com/BI5IrlWrkTMAAAAM/bunny-too-cute.gif)

    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