Poj 1845 Sumdiv (basic arithmetic theorem for finding a number factor and)
Evaluate all the factors of a number and the basic arithmetic theorem that can be used. The following are two important applications of a number:
(1) A positive integer N greater than 1, if its standard factorization formula is: N = (P1 ^ a1) * (P2 ^ a2 )...... (Pn ^)
The number of positive factors is (1 + a1) (1 + a2)... (1 + ).
(2) The sum of all its positive factors is d (N) = (1 + p1 +... p1 ^ an) (1 + p2 +... p2 ^ a2 )... (1 + pn +... + pn ^)
The method is similar to the method for finding a positive factor.
All prime numbers within sqrt (n) can be typed first (or not), because only one element of n is greater than sqrt (n) (I have proved this in my previous questions ),
So you can finally process it. For each item is an equals sequence, it is easy to sum.
The following uses poj 1845 Sumdiv as an example.
Evaluate n ^ m % p, and p is a prime number of 9901.
1) Because n and m are very large, all prime factors of n can be processed, and ^ m is the x m of each factor index. This question can directly decompose the prime factor without preprocessing all prime numbers. It is acceptable for the time complexity of this question;
2) Fast Power Operation is used.
3) There are two ways to calculate the proportional sequence and the modulo: 1. the sum formula of proportional series: S = a1 * (q ^ N-1)/(q-1), to use (q-1) model 9901 inverse element, can be obtained by Euler's theorem or Extended Euclidean, however, because mod is a prime number, the inverse element of a is a ^ (mod-2) % mod, which is obtained by using a fast power. 2. Binary of recursive form
For inverse element supplement: Fermat Theory is an important theorem in number Theory. Its content is: if p is a prime number, and Gcd (a, p) = 1, then a (p-1) 1_1 (mod p) is: if a is an integer, p is a prime number, and a and p are mutually qualitative (that is, the two have only one common approx. 1 ), then the remainder of the power of a (1) divided by p is invariably equal to 1.
// Inverse element method
# Include
# Include
# Include
# Include
# Include
# Define maxn 10000 # define ll long longusing namespace std; const ll MOD = 9901; ll prime [maxn], num, flag [maxn + 5]; ll pow_mod (ll, ll B, ll p) {// fast power ll ret = 1; while (B) {if (B & 1) ret = (ret * a) % p; a = (a * a) % p; B >>= 1;} return ret;} void get_prime () {// sieve prime memset (flag, 0, sizeof (flag); num = 0; for (ll I = 2; I
1 & (n-1) % MOD = 0) ans = ans * (pow_mod (n, m + 1, MOD * (n-1)/(n-1) % MOD; // if (n-1) % mod = 0, the reverse element else if (n> 1) ans = ans * get_sum (n, m) % MOD cannot be obtained; printf ("% lld \ n", ans); // I didn't add mod wa several times at the beginning, because this method ans may be negative} return 0 ;}
// Resolve the problem by using the binary method
# Include
# Include
# Include
# Include
# Include
# Define maxn 10000 # define ll long longusing namespace std; const ll MOD = 9901; ll prime [maxn], num, flag [maxn + 5]; ll pow_mod (ll, ll B, ll p) {// fast power ll ret = 1; while (B) {if (B & 1) ret = (ret * a) % p; a = (a * a) % p; B >>= 1;} return ret;} void get_prime () {// sieve prime memset (flag, 0, sizeof (flag); num = 0; for (ll I = 2; I
1) ans = ans * get_sum (n, m) % MOD; printf ("% lld \ n", ans);} return 0 ;}