Poj 1845-sumdiv (number theory + unique decomposition theorem + Rapid power modulo)

Source: Internet
Author: User

Poj 1845-sumdiv (number theory + unique decomposition theorem + Rapid power modulo)

This is a good question of number theory and requires a good mathematical foundation.

Given a and B, calculate the sum of all the factors of a ^ B, and then mod 9901

Analysis:

A considerable part of the number theory knowledge is used here.

A. Unique Decomposition Theorem

Any integer can be decomposed into the product of several prime numbers.

A = (P1 ^ Q1 + p2 ^ Q2 +... + PN ^ qn) P is a prime number

A ^ B = (P1 ^ (Q1 * B) +P2 ^ (Q2 * B) +... + PN ^ (qN * B))

B. Appointment and Formula

Sum (A) = (1 + p1 + p1 ^ 2 + .... + p1 ^ Q1) * (1 + p2 + p2 ^ 2 + .... + p2 ^ q2 )*...... * (1 + Pn + PN ^ 2 + ..... + PN ^ qn)

Sum (a ^ B) = (1 + p1 + p1 ^ 2 + .... + p1 ^ (Q1 * B) * (1 + p2 + p2 ^ 2 + .... + p2 ^ (Q2 * B ))*...... * (1 + Pn + PN ^ 2 + ..... + PN ^ (qN * B ))

C. Fast Power modulo (omitted)

D. Simple Model Calculation Formula

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

E. recursively solve the first N values of the proportional series (or use the multiplication inverse element to obtain them. If not, add them immediately)

(1 + p1 + p1 ^ 2 +... + p1 ^ N) ParityFor discussion:

1 .)N is an odd number (N & 1) and has an even number in total. Assume n = 5.

(1 + p1 + p1 ^ 2 + p1 ^ 3 + p1 ^ 4 + p1 ^ 5) = (1 + p1 + p1 ^ 2 + p1 ^ 3 (1 + p1 + p1 ^ 2 ))

= (1 + p1 + p1 ^ 2 + P ^ (5/2 + 1) (1 + p1 + p1 ^ 2 ))

= (1 + P ^ (5/2 + 1 ))*(1 + p1 + p1 ^ 2)

The conversion to the general formula should be

(1 + p1 + p1 ^ 2 +... + p1 ^ N)

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

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

= (1 + fast_mod (P1, n/2 + 1) * sum (P1, n/2)

2 .)N is an even number with an odd number in total. Assume n = 6.

(1 + p1 + p1 ^ 2 + p1 ^ 3 + p1 ^ 4 + p1 ^ 5 + p1 ^ 6) = (1 + p1 + p1 ^ 2 + p1 ^ 3 + p1 ^ 4 (1 + p1 + p1 ^ 2 ))

= (1 + p1 + p2 + p1 ^ 4 (1 + p1 + p2) + p1 ^ 3)

= (1 + P ^ 4)(1 + p1 + p2) + p1 ^ 3)

Convert to the normal expression

(1 + p1 + p1 ^ 2 +... + p1 ^ N)

= (1 +P1 ^ (n/2 + 1 ))*(1 + p1 + p2 +... + P ^ (n/2-1) + p1 ^ (n/2)

= (1 + fast_mod (P1, n/2 + 1) * sum (P1, n/2-1) + fast_mod (P1, n/2)

F. The multiplication is against the RMB. The weak dishes are too weak. If not, add them immediately.

First, we need to know the first n terms and

Divide by (1-Q) and convert it into a multiplication inverse element.

Finally, the Code is provided.

/************************************ * *********** ID: whiteblock63lang: G ++ prog: poj-1845 sumdivdate: 15:56:09 ************************************** * **********/# include # include # include # include # include # define CLR (, b) memset (a, B, sizeof (A) using namespace STD; typedef long ll; typedef unsigned long ull; # define maxn 10000 # define mod 9901ll a, B; // fast_pow_modll fast_mod (ll a, LL B) {ll ans = 1; A = A % MOD; while (B) {If (B & 1) ans = ans * A % MOD; A = A * A % MOD; B >>= 1 ;} return ans ;} ll factors [maxn] [2]; int FN; // decomposition prime factor void gaoji (ll x) {fn = 0; For (ll I = 2; I * I <= x; ++ I) {If (X % I) continue; factors [++ FN] [0] = I; factors [FN] [1] = 0; while (X % I = 0) {factors [FN] [1] ++; X/= I ;}} if (X! = 1) {factors [++ FN] [0] = x; factors [FN] [1] = 1 ;}// recursively solves the proportional sequence ll sum (ll p, ll N) {If (n = 0) return 1; if (P = 0) return 0; If (N & 1) Return (sum (p, n/2) * (1 + fast_mod (p, n/2 + 1) % MOD; else return (sum (p, n/2-1) * (1 + fast_mod (p, n/2 + 1) + fast_mod (p, n/2) % MOD;} void orz () {scanf ("% LLD", &, & B); gaoji (a); LL ans = 1; for (INT I = 1; I <= FN; ++ I) {ans = ans * (sum (factors [I] [0], B * factors [I] [1]) % MOD;} printf ("% LLD \ n ", ans) ;}int main () {orz (); Return 0 ;}
Code Jun

Poj 1845-sumdiv (number theory + unique decomposition theorem + Rapid power modulo)

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.