zoukankan      html  css  js  c++  java
  • 约数

    约数

    (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 = '
    ';
    //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 = '
    ';
    
    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;
    }
    
    
  • 相关阅读:
    js基本
    js循环语句
    常用的css
    Linux使用host-only的方式上网
    Struts2的文件上传及错误国际化显示
    ThinkPHP3.2.2 大写子母的用法及含义
    php中验证码不显示问题
    AOP中的ASPECTJ
    CGLIB和JDK代理
    mavne的创建
  • 原文地址:https://www.cnblogs.com/xiaoran991/p/14401368.html
Copyright © 2011-2022 走看看