poj 1845 Sumdiv

Sumdiv
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 19160 Accepted: 4814

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

Source

提示

题意:

给出a,b(0<=a,b<=50000000),求出q为a,n为b的等比数列和。例如:1+2+2^2+2^3+......

  • formula

思路:

之前我还以为直接上公式再取余就完了,之后就惨不忍睹。

真正的做法是:

1.先把a分解成小数,比如36^100=2^200*3^200(至于为什么一定要分解就不清楚了,我直接递归求和是WA)

2.递归求和

n为奇数时:(1+p+p^2...p^(n/2))*(1+p^(n/2+1))

n为偶数时:(1+p+p^2...p^(n/2-1))*(1+p^(n/2+1))+p^(n/2)

这个式子可以在纸上推出来,黑色部分做递归就可以了,当n=0时就是递归的终点。

对于乘方需要快速幂,这里不给予叙述,详情请咨询百度

示例程序

Source Code

Problem: 1845		Code Length: 866B
Memory: 388K		Time: 32MS
Language: GCC		Result: Accepted
#include <stdio.h>
long long Pow(int x,int n)				//快速幂
{
    long long t=x,num=1;
    while(n!=0)
    {
        if(n%2==1)
        {
            num=(num*t)%9901;
        }
        t=(t*t)%9901;
        n=n/2;
    }
    return num;
}
long long Sum(int p,long long n)			//递归求和
{
    if(n==0)
    {
        return 1;
    }
    else if(n%2==1)
    {
        return Sum(p,n/2)*(1+Pow(p,n/2+1))%9901;
    }
    else
    {
        return (Sum(p,n/2-1)*(1+Pow(p,n/2+1))%9901+Pow(p,n/2))%9901;
    }
}
long long f(int a,int b)
{
    long long sum=1;
    int i,k;
    for(i=2;a>=i*i;i++)			//只需判断到i*i就行了,这和试除法是一样的道理
    {
        k=0;
        while(a%i==0)			//因式分解
        {
            a=a/i;
            k++;
        }
        sum=sum*Sum(i,b*k)%9901;			//分解出来去计算
    }
    if(a!=1)
    {
        sum=sum*Sum(a,b)%9901;				//如果还没有分解完全还需要再计算一次
    }
    return sum;
}
int main()
{
    int a,b;
    scanf("%d %d",&a,&b);
    printf("%lld",f(a,b));
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值