算法 {约数}

算法 {约数}

约数

定義

對於整數a, 他的約數集合為: 正整數集合裡 滿足a % ? == 0的數的集合;
. 比如-4的约数集合为{1,2,4}; 0的约数集合为所有正整数; 2的約數集合為{1,2};

性質

#int a; a的约数个数 的最大值为1536#
当a等于1745944200/ 2113511400时, 他的约数个数为1536; 此时a的质因数分解为2^3 * 3^3 * 5^2 * 7 * 11 * 13 * 17 * (19/23);

@DELI;

#1是任何正整數的約數#;

算法

求一個數的所有约数之和 (O(1))

代碼
divisor_sum = 1;
for( [prime,power] : `a的質因數分解`)){
	divisor_sum *= (1 + prime + `prime^2` + ... + `prime^power`);
}

求一個數的约数的个数 (O(1))

代码
divisor_count = 1;
for( [prime,power] : `a的質因數分解`)){
	divisor_count *= (power + 1);
}

求一個數的所有约数

预处理 O ( K ) + O ( ? ) O(K) + O(?) O(K)+O(?)

+ O ( K ) O(K) O(K) where K K K is the Divisor-Count(at most < 1600 < 1600 <1600 if the number is int)
+ O ( ? ) O(?) O(?) means the process of Prime-Factorization for the number;
+ If you wanna all divisors are sorted, add sort( divisors, divisors + divisor_count); in @Location_0;

//{ All_Divisors-Declaration
class All_Divisors{ //< @Singleton
public:
    using Type_ = @Unfinished; //< @Unfinished
    //< @Name(Type_) is the type of the target-number (i.e., the number represented by @Var(prime_factorization)), and is also the type of the Prime-Factors;
    //-- definition ends
    const Type_ * Divisors;
    //-- variable ends
    All_Divisors( const pair< Type_, int> *);
    int Get_divisorCount() const;
    void Initialize( int);
private:
    static constexpr int divisor_MaxCount_ = @Unfinished ; //< @Unfinished is `1600` if $(@Name = int));
    //-- definition ends
    const pair< Type_, int> * prime_factorization;
    Type_ divisors[ divisor_MaxCount_];
    int divisor_count;
    int range;
    //-- variable ends
    void dfs( int, Type_);
};
//} All_Divisors-Declaration

//{ All_Divisors-Implementation
All_Divisors::All_Divisors( const pair< Type_, int> * _primeFactorization)
        :
        prime_factorization( _primeFactorization){
    //{ Singleton-Check
    static bool __singleton_flag = true;
    ASSERT_( __singleton_flag);
     __singleton_flag = false;
    //} Singleton-Check
    Divisors = divisors;
}
int All_Divisors::Get_divisorCount() const{ return divisor_count;}
void All_Divisors::Initialize( int _range){
//< + @Name(_range): the Array-Length of @Var(prime_factorization)
    range = _range;
    //--
    divisor_count = 0;
    dfs( 0, 1);
    //>< @Location_0
}
void All_Divisors::dfs( int _ind, Type_ _num){
    if( _ind >= range){
        ASSERT_( divisor_count < divisor_MaxCount_);
        divisors[ divisor_count ++] = _num;
        return;
    }
    //--
    dfs( _ind + 1, _num);
    for( int p = 1; p <= prime_factorization[ _ind].second; ++p){
        _num *= prime_factorization[ _ind].first;
        dfs( _ind + 1, _num);
    }
}
//} All_Divisors-Implementation
一次性 O ( a ) O(\sqrt{a}) O(a )
{ // 求一個數`a`的所有約數;
    auto a = ?;
    ASSERT_( a >= 1);
    using T_ = decltype(a);
    vector<T_> divisors; // `a`的所有約數 (不是遞增的 但所有元素一定互不相同);
    divisors.push_back( 1);
    if( a != 1){
        divisors.push_back( a);
    }
    for( int i = 2; i <= a / i; ++i){
        if( a % i == 0){
            divisors.push_back( i);
            if( i != a/i){
                divisors.push_back( a/i);
            }
        }
    }
} // 求一個數`a`的所有約數;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值