Poj 1845 sumdiv (Fast Power + prime factor + approx. And formula + same modulus)

Source: Internet
Author: User
Tags modulus
Sumdiv
Time limit:1000 ms   Memory limit:30000 K
Total submissions:16109   Accepted:3992

Description

Consider two natural numbers a and B. Let s be the sum of all natural divisors of a ^ B. Determine s modulo 9901 (the rest of the division of S by 9901 ).

Input

The only line contains the two natural numbers A and B, (0 <= A, B <= 50000000) separated by blanks.

Output

The only line of the output will contain in S modulo 9901.

Sample Input

2 3

Sample output

15

Hint

2 ^ 3 = 8.
The natural divisors of 8 are: 1, 2, 4, 8. Their sum is 15.

15 modulo 9901 is 15 (that shoshould be output ).



Calculate the sum of approx.

Thought: It was quite simple, but it was a data range that made this question instantly taller.

Some basic knowledge is required for this question. 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 modulus formula:

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

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

 

1: prime factor decomposition for

Method for decomposing:

A first constantly modulo the first prime number 2. When a % 2 = 0, record the number of occurrences of 2 + 1, A/= 2;

When a % 2! When the value is 0, a constantly modulo the next continuous prime number 3...

And so on until a = 1.

 

Note that when a itself is a prime number, it cannot be decomposed. It itself is its own prime number decomposition formula.

 

Finally, a = p1 ^ K1 * P2 ^ K2 * P3 ^ K3 *... * PN ^ kN.
Therefore, a ^ B = p1 ^ (K1 * B) * P2 ^ (K2 * B) *... * PN ^ (kN * B );


2: sum of all the approx. values of a ^ B:

Sum = [1 + p1 + p1 ^ 2 +... + p1 ^ (A1 * B)] * [1 + p2 + p2 ^ 2 +... + p2 ^ (A2 * B)] *... * [1 + Pn + PN ^ 2 +... + PN ^ (an * B)].


3: Calculate the proportional sequence 1 + PI ^ 2 + PI ^ 3 +... + PI ^ N using recursive binary:

(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 ))


Such a big data cannot be directly calculated, so it is the sum of power-related values. 1) According to the unique decomposition theorem of an integer, a number can be divided into the form of Power Multiplication of a prime factor, record the prime factor and number of occurrences.

2) sum according to the divisor and formula, and use this formula to solve the sum problem of the Equality formula. Binary recursion is used for calculation. At the same time, it performs a rapid power in binary recursion.



#include<iostream>#include<cstdio>#include<string.h>#include<string>#include<cmath>#include<queue>#define mod 9901#define LL long longusing namespace std;LL primeper[2500000],cnt[2500000];LL pow1(LL a,LL b){    LL r=1,ba=a;    while(b!=0)    {        if(b&1)            r=(r*ba)%mod;        ba=(ba*ba)%mod;        b>>=1;    }    return r;}LL so(LL x,LL y){    if(!y)    return 1;    else if(y&1)        return ( (1+pow1(x,y/2+1))*so(x,y/2) )%mod;    else        return ( (1+pow1(x,y/2+1))*so(x,y/2-1)+pow1(x,y/2) )%mod;}int main(){    LL i,j,k;LL n,m;    while(scanf("%lld%lld",&n,&m)!=EOF)    {        memset(cnt,0,sizeof(cnt));        for(k=0,i=2;i*i<=n;i++)        {            if(n%i==0)            {                primeper[k]=i;                while(n%i==0)                {                    cnt[k]++;                    n/=i;                }                k++;            }        }        if(n!=1)        {            primeper[k]=n;            cnt[k]++;            k++;        }        LL ans=1;        for(i=0;i<k;i++)            ans=(ans*(so(primeper[i],cnt[i]*m))%mod )%mod;        printf("%lld\n",ans);    }    return 0;}


Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Poj 1845 sumdiv (Fast Power + prime factor + approx. And formula + same modulus)

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.