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
#include
#include
#include
#include
#include
#include
#define maxx 10050
#define mod 9901
using namespace std;

int a,b;
char pri[maxx];
vector<int> prime;
void init()
{
    for(int i=2;iif(!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;icout<return 0;
}

你可能感兴趣的:(素数)