Link: poj 1845

Evaluate the sum of all the factors of a ^ B and the remainder of 9901

For example, the factors of 2 ^ 3 = are 1, 2, 4, 8, and the sum is 15. 15 is the result after the remainder is obtained.

There are three main application theorems:

(1) unique factorization theorem of integers:

Any positive integer has only one way to write the product expression of its prime factor.

A = (P1 ^ k1) * (P2 ^ K2) * (P3 ^ K3) *... * (PN ^ kN) where Pi is a prime number

(2) divisor and formula:

For the decomposed integer A = (P1 ^ k1) * (P2 ^ K2) * (P3 ^ K3) *... * (PN ^ kN)

Sum of all the factors of

S = (1 + p1 + p1 ^ 2 + p1 ^ 3 +... P1 ^ k1) * (1 + p2 + p2 ^ 2 + p2 ^ 3 + .... P2 ^ K2)

* (1 + P3 + P3 ^ 3 +... + P3 ^ K3) *... * (1 + Pn + PN ^ 2 + PN ^ 3 +... PN ^ kN)

(3) Same Remainder Theorem

(A + B) % m = (a % m + B % m) % m

(A * B) % m = (a % m * B % m) % m

The sum of all approx. A ^ B:

Sum = [1 + p1 + p1 ^ 2 +... + p1 ^ (K1 * B)] * [1 + p2 + p2 ^ 2 +... + p2 ^ (K2 * B)] *...

* [1 + Pn + PN ^ 2 +... + PN ^ (kN * B)]

We must first calculate the prime factor and number of A, and then sum them.

However, if both A and B reach 50 million, the sum will definitely time out. Let's take a look.

Use**Recursive binary**Returns the proportional sequence 1 + PI ^ 2 + PI ^ 3 +... + PI ^ N:

(1) If n is an odd number and there is a total of even numbers, then:

1 + P ^ 2 + P ^ 3 +... + P ^ n

= (1 + P ^ (n/2 + 1) + p * (1 + P ^ (n/2 + 1) +... + P ^ (n/2) * (1 + P ^ (n/2 + 1 ))

= (1 + P ^ 2 +... + P ^ (**N/2**) * (1 + P ^ (n/2 + 1 ))

(2) If n is an even number and there is a total of odd values, then:

1 + P ^ 2 + P ^ 3 +... + P ^ n

= (1 + P ^ (n/2 + 1) + p * (1 + P ^ (n/2 + 1) +... + P ^ (n/2-1) * (1 + P ^ (n/2 + 1) + P ^ (n/2)

= (1 + P ^ 2 +... + P ^ (**N/2-1**) * (1 + P ^ (n/2 + 1) + P ^ (n/2 );

We can see from the above formula that the first half is exactly half of the original formula and can be recursive binary solution.

The brute-force solution of P ^ K also times out and can be used to quickly obtain the remainder.

# Include <stdio. h> # include <string. h> # include <math. h> # define M 10000 # define n 9901int P [m], CNT [m] ;__ int64 powmod (_ int64 A ,__ int64 B) // a ^ B % N Saved power remainder {_ int64 ans = 1; A % = N; while (B) {If (B % 2) ans = ans * A % N; B/= 2; A = A * A % N;} return ans ;:int64 sum (INT P, int N) // recursive binary summation {If (n = 0) return 1; if (N % 2) return sum (p, n/2) * (1 + powmod (p, n/2 + 1) % N; // return (sum (p, n/2-1) * (1 + powmod (p, n/2 + 1) % N + powmod (p, n/2) % N; // even Time} int main () {int A, B, I, K; _ int64 ans; while (scanf ("% d", & A, & B )! = EOF) {memset (CNT, 0, sizeof (CNT); k = 1; for (I = 2; I * I <= A; I ++) {// calculate the element and number of A if (a % I = 0) {P [k] = I; while (a % I = 0) {CNT [k] ++; A/= I;} k ++ ;}} if (! = 1) {// remember to finally Judge P [k] = A; CNT [k ++] ++;} ans = 1; for (I = 1; I <K; I ++) ans = ans * (sum (P [I], CNT [I] * B) % N; printf ("% i64d \ n", ANS);} return 0 ;}

Poj 1845 sumdiv (same remainder theorem, rapid power remainder)