Sumdiv POJ - 1845(分治+素数筛+快速幂)

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).
评价:因为取模比较小,所以我都用了int,但是此题坑点是,我忘记了会有大素数,导致的相乘溢出,细节原因,换用long long即可。
AC

#include<iostream>
#include<cstdio>
#include<stack>
#include<algorithm>
#include<queue>
#include<cstring>
#include<cmath>
#define maxx 10050
#define mod 9901
using namespace std;

int a,b;
char pri[maxx];
vector<int> prime;
void init()
{
    for(int i=2;i<maxx;i++)
    {
        if(!pri[i])
            prime.push_back(i);
        for(int j=i*i;j<=maxx;j+=i)
            pri[j]=1;
    }
}
int fac[100];
int e[100];
int cnt;
void getFac(int x)
{
    cnt=0;
    for(int i=0;prime[i]*prime[i]<=x;i++)
    {
        if(x%prime[i]==0)
        {
            fac[cnt]=prime[i];
            while(x%prime[i]==0)
            {
                e[cnt]++;
                x/=prime[i];
            }
            cnt++;
        }
    }
    if(x>1)
    {
        fac[cnt]=x;
        e[cnt++]=1;
    }
}
long long _p(long long a,long long b)
{
    long long ans=1;
    while(b)
    {
        if(b&1)
            ans=ans*a%mod;
        a=a*a%mod;
        b>>=1;
    }
    return ans;
}
long long get(long long p,long long x)
{
    if(x==0)
        return 1;
    if(x==1)
        return (p+1)%mod;
    int ans=(_p(p,x/2+1)+1)*get(p,x-x/2-1);
    if(x%2)
      return ans%mod;
    else
        return (ans+_p(p,x>>1))%mod;
}
int main()
{
    init();
    cin>>a>>b;
    getFac(a);
    long long ans=1;
    for(int i=0;i<cnt;i++)
        ans=ans*get(fac[i],e[i]*b)%mod;
    cout<<ans<<endl;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值