POJ1845——Sumdiv

文章转自:http://blog.csdn.net/a15110103117


Sumdiv
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 19376 Accepted: 4876

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时就是递归的终点。

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


#include <iostream>
#include <cstdlib>
#include <cstdio>

using namespace std;

long long sort_pow(int a, int b)                 //二分求幂
{
    long long r = 1, base = a;
    while( b )
    {
        if( b%2==1 )
            r = ( r*base )%9901;
        base = ( base*base )%9901;
        b /= 2;
    }
    return r;
}

long long multi(int a, long long b)       //递归求和
{
    if( b==0 )
        return 1;
    else if( b%2==1 )
        return multi(a,b/2)*(1+sort_pow(a,b/2+1))%9901;
    else
        return (multi(a,b/2-1)*(1+sort_pow(a,b/2+1))%9901 + sort_pow(a,b/2))%9901;
}

long long dell( int a, int b )
{
    long long mul = 1;
    int cnt;
    for( int i = 2; i*i <= a;i++ )       //遍历到  sqrt(a)  就好,类似原始的素数求法
    {
        cnt = 0;
        while( a%i==0 )      //因式分解
        {
            a /= i;
            cnt++;
        }
        mul = mul * multi(i,b*cnt)%9901;
    }
    if( a!=1 )
    {
        mul = mul*multi(a,b)%9901;
    }
    return mul;
}

int main()
{
    int a, b;
    cin>>a>>b;
    cout<<dell(a,b)<<endl;

    return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值