poj1845 Sumdiv(数论,因数和,等比数列,快速幂)

来源:互联网 发布:java round up 编辑:程序博客网 时间:2024/05/16 13:02
Sumdiv
Time Limit: 1000MS Memory Limit: 30000KTotal Submissions: 13333 Accepted: 3264

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 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 should be output). 

求a^b的所有因数和模余9901

#include<stdio.h>#define m 9901__int64 pow(__int64 p,__int64 r){//快速幂(***)    __int64 s=1;    while(r)    {        if(r%2)            s=s*p%m;        r/=2;        p=p*p%m;    }    return s;}__int64 sum(__int64 p,__int64 r){//等比数列求和,取前一半做递归    if(!r) return 1;    if(r%2)//数列个数为偶数,取数列前一半,乘法分配律        return (sum(p,r/2)*(1+pow(p,r/2+1)))%m;    else//奇数个,中间一个单独加,其余与偶数个同理        return (sum(p,r/2-1)*(1+pow(p,r/2+1))+pow(p,r/2))%m;}int main(){    int a,b,i;    int p[10000],r[10000];    while(scanf("%d%d",&a,&b)!=EOF)    {        int k=0;        if(a%2==0)        {            p[k]=2;            r[k]=0;            while(a%2==0)            {                r[k]++;                a/=2;            }            k++;        }//将分解质因数,形如p[0]^r[0]*p[1]^r[1]*...*p[k-1]^r[k-1];        for(i=3; i*i<=a; i+=2)//质因子2单列出,后不再有偶质数            if(a%i==0)            {                p[k]=i;                r[k]=0;                while(a%i==0)                {                    r[k]++;                    a/=i;                }                k++;            }        if(a!=1)        {            p[k]=a;            r[k++]=1;        }//剩余的大质数提出        int ans=1;        for(i=0; i<k; i++)            ans=((ans*(sum(p[i],r[i]*b)%m)))%m;        /*            求所有因数的和:            生成函数            ∏(i=0,1,2...k-1)=∑[p[i]^j(0,1,2...r[i]*b)];        */        printf("%d\n",ans);    }    return 0;}


0 0