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

Sumdiv
Time Limit: 1000MS Memory Limit: 30000K
Total 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
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值