poj 1845(等比数列前n项和及快速幂)

本文针对Sumdiv问题进行了详细的解析,该问题是求两个自然数A和B的所有自然数因子之和S,并计算S对9901取模的结果。输入包含两个自然数A和B,输出为S对9901取模后的结果。文章提供了AC代码,采用快速幂运算及等比数列求和的方法来高效解决此问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Sumdiv
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 13959 Accepted: 3433

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

Romania OI 2002

思路看:

http://hi.baidu.com/necsinmyway/item/9f10b6d96c5068fbb2f77740

AC代码:

#include<iostream>
using namespace std;
#define LL long long
LL pow_mod(LL a,LL n,int mod){         //快速幂
    LL r=1;
    LL base=a;
    while(n){
        if(n&1)
            r=r*base%mod;
        base=base*base%mod;
        n>>=1;
    }
    return r%9901;
}
LL sum(LL a,LL b,LL mod){             //二分求等比数列前N项和
    if(b==0)
        return 1;
    if(b%2==1)
        return (sum(a,b/2,mod)*(pow_mod(a,b/2+1,mod)+1))%mod;
    else
        return (sum(a,b-1,mod)+pow_mod(a,b,mod))%mod;
}
int main(){
    LL a,b;
    LL ans;
    while(cin>>a>>b){
        ans=1;
        for(LL i=2;i*i<=a;i++){           //将a分解为质数的乘积
            if(a%i==0){
                LL s=0;
                while(a%i==0){
                    s++;
                    a/=i;
                }
                ans=ans*sum(i%9901,b*s,9901)%9901;
            }
        }
        if(a>=2){
            ans=ans*sum(a%9901,b,9901)%9901;
        }
        cout<<ans<<endl;
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值