Satisfactory Pairs HackerRank - pairs-again——預先處理+不定長數組應用__初級數論

來源:互聯網
上載者:User

Think:
1題意:輸入n, 求(a, b)滿足a < b 且存在x, y使得x*a + y*b = n的二元組個數,要求n, a, b, x, y皆為正整數
2方法:先預先處理小於n的數的約數,然後暴力試探+剪枝,試探符合題意的二元組個數,時間複雜度 1/x 從1到n積分,為nln(n)

vjudge題目連結

建議參考部落格1
建議參考部落格2

以下為Accepted代碼

#include <bits/stdc++.h>using namespace std;const int N = 3e5 + 4;vector <int> y_b[N];vector <int> :: iterator b;int book[N];int main(){    int n, i, j, a, x, t, ans;    scanf("%d", &n);    for(i = 1; i <= n; i++){        for(j = 1; j*j <= i; j++){            if(i%j == 0){                y_b[i].push_back(j);                if(i*i != j){                    t = i / j;                    y_b[i].push_back(t);                }            }        }        sort(y_b[i].begin(), y_b[i].end(), greater<int>());    }    ans = 0;    for(a = 1; a < n; a++){        for(x = 1; x*a < n; x++){            t = n - x*a;            for(b = y_b[t].begin(); b != y_b[t].end(); b++){                if((*b) <= a)                    break;                if(book[*b] != a){                    ans++;                    book[*b] = a;                }            }        }    }    printf("%d\n", ans);    return 0;}

以下為建議參考代碼

#include <bits/stdc++.h>using namespace std;const int N = 3e5 + 4;vector <int> y_b[N];/*記錄當前元素的約數*/vector <int> :: iterator b;int book[N];/*實數型全域變數初始化預設為0*/bool cmp(int a, int b){    return a > b;}int main(){    int n, i, j, a, x, t, ans;    scanf("%d", &n);    for(i = 1; i <= n; i++){        for(j = 1; j*j <= i; j++){            if(i%j == 0){                y_b[i].push_back(j);                if(i*i != j){                    t = i / j;                    y_b[i].push_back(t);                }            }        }        sort(y_b[i].begin(), y_b[i].end(), greater<int>());    }    ans = 0;    for(a = 1; a < n; a++){        for(x = 1; x*a < n; x++){            t = n - x*a;            for(b = y_b[t].begin(); b != y_b[t].end(); b++){                if((*b) <= a)/*題意:a < b*/                    break;                if(book[*b] != a){/*標記判重*/                    ans++;                    book[*b] = a;                }            }        }    }    printf("%d\n", ans);    return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.