约数

约数

(1)试除法

时间复杂度:O(\(\sqrt{n}\))

vector<int> get_divisors(int n){
    vector<int> ret;
    for (int i = 1; i <= n / i; ++i){
        if (n % i == 0){
            ret.push_back(i);
            if (n / i != i) ret.push_back(n / i);
        }
    }
    return ret;
}

(2)约数个数

int范围内约数最多的那个整数的约数有1500个左右。

N = \(p_1\)^\(\alpha_1\) * \(p_2\)^\(\alpha_2\) * \(\ldots\) * \(p_k\)^\(\alpha_k\)

(\(\alpha_1\) + 1)(\(\alpha_2\) + 1) \(\ldots\) (\(\alpha_k\) + 1)

时间复杂度:O(\(\sqrt{n}\))

http://acm.hdu.edu.cn/showproblem.php?pid=1492

题意:求 n 的约数个数。

#include <bits/stdc++.h>
using namespace std;

typedef long long LL;

const char nl = '\n';
//const int N =

int main(){
    ios::sync_with_stdio(false);
//    cin.tie(nullptr);
    
    LL n;
    while (cin >> n, n){
        unordered_map<LL, LL> primes;

        for (LL i = 2; i <= n / i; ++i){
            while (n % i == 0){
                ++primes[i];
                n /= i;
            }
        }
        if (n > 1) ++primes[n];

        LL ret = 1;
        for (auto &prime : primes) ret = ret * (prime.second + 1);
        cout << ret << nl;
    }

    return 0;
}

(3)约数之和

N = \({p_1}^{\alpha_1}\) \({p_2}^{\alpha_2}\) \(\ldots\) \({p_k}^{\alpha_k}\)

(\({p_1}^{0}\) + \({p_1}^{1}\) + \(\ldots\) + \({p_1}^{\alpha(1)}\)) (\({p_2}^{0}\) + \({p_2}^{1}\) + \(\ldots\) + \({p_2}^{\alpha(2)}\)) \(\ldots\) (\({p_k}^{0}\) + \({p_1}^{1}\) + \(\ldots\) + \({p_k}^{\alpha(k)}\))

时间复杂度:O(\(\sqrt{n}\))

(4)欧几里得(辗转相除法)

时间复杂度:O(\(\log\)n)

#include <bits/stdc++.h>
using namespace std;

const char nl = '\n';

int gcd(int a, int b){
    return b ? gcd(b, a % b) : a;
}

int main(){
    ios::sync_with_stdio(false);
//    cin.tie(nullptr);

    int a, b;
    while (cin >> a >> b){
        cout << gcd(a, b) << nl;
    }
    return 0;
}

posted @ 2021-02-14 00:09  小燃、  阅读(204)  评论(0编辑  收藏  举报