一些所有分频器的除数的总和

问题描述 投票:1回答:1

下面的方法是here。我的很好的解释是不能写在这里,由于格式问题。

// C ++程序找到一个自然数的所有约数的约数的总和。

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

// Returns sum of divisors of all the divisors 
// of n 
int sumDivisorsOfDivisors(int n) 
{ 
    // Calculating powers of prime factors and 
    // storing them in a map mp[]. 
    map<int, int> mp; 
    for (int j=2; j<=sqrt(n); j++) 
    { 
        int count = 0; 
        while (n%j == 0) 
        { 
            n /= j; 
            count++; 
        } 

        if (count) 
            mp[j] = count; 
    } 

    // If n is a prime number 
    if (n != 1) 
        mp[n] = 1; 

    // For each prime factor, calculating (p^(a+1)-1)/(p-1) 
    // and adding it to answer. 
    int ans = 1; 
    for (auto it : mp) 
    { 
        int pw = 1; 
        int sum = 0; 

        for (int i=it.second+1; i>=1; i--) 
        { 
            sum += (i*pw); 
            pw *= it.first; 
        } 
        ans *= sum; 
    } 

    return ans; 
} 

// Driven Program 
int main() 
{ 
    int n = 10; 
    cout << sumDivisorsOfDivisors(n); 
    return 0; 
} 

我没有得到什么在这个循环中发生,而不是增加他们乘以总和,它们是如何计算(p^(a+1)-1)/(p-1) ANS和这ans.can任何人帮助我这个循环背后的直觉。

我得到这个从here

for (auto it : mp) 
{ 
    int pw = 1; 
    int sum = 0; 

    for (int i=it.second+1; i>=1; i--) 
    { 
        sum += (i*pw); 
        pw *= it.first; 
    } 
    ans *= sum; 
}
c++ algorithm discrete-mathematics number-theory
1个回答
1
投票

首先考虑这样的语句:

(P10 + P11 + ... + p1k1)*(P20 + P21 + ... + p2k2)

现在,任何PA的除数,对于p担任总理,为P0,P1,......,PA和diviors的总和将是:

((P10)+(P10 + P11)+ ... +(P10 + P11 + ... + PK1))*((P20)+(P20 + P21)+(P20 + P21 + P22)+ .. (P20 + P21 + P22 + .. + p2k2))

你可以考虑上面的语句等同于吼叫声明:

[[P10 *(K1 + 1)+ P11 * K1 + P12 *(K1 - 1)+ .... +(p1k1 * 1)]] * [[P20 *(K2 + 1)+ P21 *(K2) + P22 *(K2 - 1)+ ... +(p2k2 * 1)]在您在帖子写的代码,最后一条语句实施。

例如,如果你考虑N = 54 = 33 * 21, 俺们计算格式如下:

年=(20 * 2 + 21 * 1)*(30 * 4 + 31 * 3 + 32 * 2 + 33 * 1)= 4 * 58 = 232

© www.soinside.com 2019 - 2024. All rights reserved.