Poj1845 (number theory + binary fast modulo)

Source: Internet
Author: User

Evaluate the sum of all the factors of a ^ B and the modulo 9901, for example, 2 ^ 3 = 8 => 1 + 2 + 4 + 8 = 15. The final answer is 15% 9901 = 15.

Analysis: A ^ B is first decomposed into prime factor form: A ^ B = (P1 ^ k1) + (P2 ^ K2) + (P3 ^ K3) +...

Then the sum of all factors of a ^ B is: S = (1 + p1 ^ 1 + p1 ^ 2 + p1 ^ 3 +... + p1 ^ K) * (1 + p2 ^ 1 + p2 ^ 2 + p2 ^ 3 +... + p2 ^ K) * (1 + P3 ^ 1 + P3 ^ 2 + P3 ^ 3 +... + P3 ^ K )*(...

Computing 1 + P ^ 2 + P ^ 3 +... + P ^ K can be solved in binary mode;

If K is an even number, for example, K = 4, 1 + P ^ 2 + P ^ 3 + P ^ 4 = (1 + p) + P ^ 2*(1 + p * (1 + p ));

If K is an odd number, for example, K = 5, 1 + P ^ 2 + P ^ 3 + P ^ 4 + P ^ 5 = (1 + P ^ 2) + P ^ 3*(1 + P ^ 2 );

# Include <iostream> <br/> # include <cmath> <br/> using namespace STD; <br/> const int maxn = 10000; <br/> int, b; <br/> int mod = 9901; <br/>__ int64 POW (_ int64 x ,__ int64 N) // calculate P ^ n <br/>{< br/>__ int64 ret = 1, s = x; <br/> while (1) <br/> {<br/> If (N & 1) <br/> ret = (RET % mod) * (S % mod) % MOD; <br/> If (N >>= 1) <br/> S = (S % mod) * (S % mod) % MOD; <br/> else break; <br/>}< br/> return ret; <br/>}< br/>__ I Nt64 sum (_ int64 P, _ int64 N) // calculate 1 + P ^ 2 +... + P ^ N; <br/>{< br/> If (n = 0) <br/> return 1; <br/> If (N & 1) // n % 2 = 0; <br/> return (1 + POW (p, n/2 + 1) % mod) * (sum (p, n/2) % mod) % MOD; <br/> else <br/> return (1 + POW (p, n/2 + 1) % mod) * (sum (p, (n-1)/2) % mod) + POW (p, n/2) % mod) % MOD; <br/>}< br/> int main () <br/>{< br/>__ int64 S, ans; <br/> int I, P [maxn], c [maxn]; // P [I] indicates the I-th prime factor, and C [I] indicates its number. <Br/> while (scanf ("% d", & A, & B )! = EOF) <br/>{< br/> memset (p, 0, sizeof (p); <br/> memset (C, 0, sizeof (c )); <br/> // a ^ B = p1 ^ (C1 * B )*.... * PI ^ (CI * B) <br/> // ans = (1 + p1 + p1 ^ 2 +... + p1 ^ (C1 * B ))*()... * (1 + PI ^ 2 +... + PI ^ (CI * B) <br/> // decompose a into prime numbers <br/> for (I = 2; I * I <= A; I ++) // find that all prime factors of a exist in P [], <br/>{< br/> if (a % I = 0) <br/> {<br/> P [++ P [0] = I; <br/> while (a % I = 0) <br/>{< br/> A/= I; <br/> ++ C [p [0]; // number of corresponding prime factors. <Br/>}< br/> if (! = 1) // process the last number. <Br/> {<br/> P [++ P [0] = A; <br/> C [p [0] = 1; <br/>}< br/> ans = 1; <br/> for (I = 1; I <= P [0]; I ++) // sum = (P1 ^ (N1 * B + 1)-1)/(p1-1) * (P2 ^ (N2 * B + 1)-1)/(p2-2) *... <Br/> {<br/> ans = (ANS % mod) * (sum (P [I], C [I] * B) % mod) % MOD; <br/>}< br/> printf ("% i64d/N", ANS); <br/>}< br/> return 0; <br/>} 

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.