Returns the sum of all factors of a number.

Source: Internet
Author: User
For example, 6 has a factor of 1, 2, 3, and 6, and their sum is 12.
Divide the sum of all factors of N ^ m by the remainder of a number S. # Include < Cassert >
# Include < Vector >
Using   Namespace STD;

Static Vector < Int > Primes;
Bool Isprime ( Int N)
{
For (Size_t I =   0 ; Primes [I] * Primes [I] <= N; I ++ )
{
If(N%Primes [I]= 0)
Return False;
}
Return   True ;
}

Int Getprime (size_t index)
{
Assert (Index <   10000 );

If (Index > = Primes. Size ())
{
If (Primes. Size () =   0 )
{
Primes. Reserve ( 10000 );
Primes. push_back ( 2 );
Primes. push_back ( 3 );
Primes. push_back ( 5 );
Primes. push_back ( 7 );
}
For ( Int T = Primes. Back () +   2 ; Index > = Primes. Size (); t + =   2 )
{
If(Isprime (t ))
Primes. push_back (t );
}
}

Return Primes [Index];
}

_ Int64 powermod (_ int64 N, _ int64 M, _ int64 S)
{
_ Int64 T;
For (T =   1 ; M >>=   1 )
{
If (M &   1 )
T = T * N % S;
N = N * N % S;
}
Return T;
}

Int Sumfactormod ( Int N, Int M, Int S)
{
Int T;
Int Sum =   1 ;
For (T =   0 ; N >   1 ; T ++ )
{
Int P = Getprime (t );
Int Q =   0 ;
While (N % P =   0 )
N /= P, q ++ ;
If (Q)
{
/**/ /*
If n = PI {PI ^ Qi} pi is a prime number, sum can be expressed as pi {f (PI, Qi )}
Where F (p, q) = P ^ 0 + P ^ 1 + P ^ 2 + P ^ q = (P ^ (q + 1)-1)/P-1)
Therefore, the required value sum % s = PI {f (PI, Qi) % s} % s
F (p, q) % s = (P ^ (q + 1)-1)/(p-1) % s
Obtained by x/y % z = x % (y * Z)/y
F (p, q) % s = (P ^ (q + 1)-1) % (S * (PM)/(p-1)
= (P ^ (q + 1) % (S * (p-1) + (S * (PM)-1) % S * (p-1)/(p-1)
Among them, P ^ (q + 1) % (S * (PM) can be quickly obtained using powermod
*/
Q * = M;
_ Int64 sp_1 = _ Int64 (s) * (P -   1 );
_ Int64 t = Powermod (p, q + 1 , Sp_1 );
T + = Sp_1 -   1 ;
Sum * = T % Sp_1 / (P -   1 );
Sum % = S;
}
}
Return Sum % S;
}

Int Sumfactormod ( Int N, Int S)
{
ReturnSumfactormod (n,1, S );
}

Supplement: add the comment. Someone adds that if n = PI {PI ^ Qi} pi is a prime number, sum can be expressed as pi {f (PI, Qi )} "I don't understand. The following is a rough deduction:
N = PI {PI ^ Qi} pi is a prime number
Then the factor of N can be listed as follows: A1, A2,... a (PI {QI + 1:
A1 = 1 aq0 + 1 = p1... A (PI {QI + 1}-q0 + 1) = p1 ^ Q1 * P2 ^ Q2 *... * P0 ^ 0
A2 = P0 aq0 + 2 = p1 * P0... A (PI {QI + 1}-q0 + 2) = p1 ^ Q1 * P2 ^ Q2 *... * P0 ^ 1
A3 = P0 ^ 2 aq0 + 3 = p1 * P0 ^ 2... A (PI {QI + 1}-q0 + 3) = p1 ^ Q1 * P2 ^ Q2 *... * P0 ^ 2
.........
Aq0 = P0 ^ q0 aq0 + q0 = p1 * P0 ^ q0... A (PI {QI + 1}) = p1 ^ Q1 * P2 ^ Q2 *... * P0 ^ q0
We can see from the multiplication principle:
Among these factors, there are 0 PI factor factors: PI {QI + 1}/(QI + 1)
One PI factor and one pi {QI + 1}/(QI + 1)
...
There are Qi PI factors and PI {QI + 1}/(QI + 1)
This set of qi + 1 PI {QI + 1}/(QI + 1) numbers is exactly the same if the PI factor is removed separately.
This set is the factor set of the number N/PI ^ Qi after N removes the factor pi.
So g (n) = (PI ^ 0 + PI ^ 1 +... + PI ^ Qi) * g (N/PI ^ Qi)
That is, g (n) = PI {PI ^ 0 + PI ^ 1 +... + PI ^ Qi}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.