【数论】【poj1845】Sumdiv

来源:互联网 发布:7位铁通卡用什么网络 编辑:程序博客网 时间:2024/06/06 00:56

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

——————中文版题目大意——————

给定A,B,求A^B的所有因数的和,再MOD 9901

思路

这题我们首先看,求的是A^B的因子之和
我们首先分解A
因为任何一个数都可以分解为素数的幂次方相乘:
所以:A=(p1^k1)*(p2^k2)………;
其中p是素数,k是这个数最多能被除几次(幂的次数);
因子个数就是:
(1+2+……..k1)(1+2+……….k2) ……个
那么因子之和就是
(1+p1^1+…..p1^k1)(1+p2^2+…..p2^k2) ………..;
a^b=(p1^k1* b) * (p2^k2*b)…..;
1+p1^1+…..p1^k1这就是一个等比数列
我们分奇和偶讨论 提出他们的通项以减少次方数求解;

代码

#include <cstdio>#include <cstring>#include <algorithm>#include <iostream>#include <cmath>#define ll long long#define mo 9901using namespace std;inline int read(){    int ret=0,f=1;char c=getchar();    for(;!isdigit(c);c=getchar())if(c=='-')f=-1;    for(;isdigit(c);c=getchar())ret=ret*10+c-'0';    return ret*f;}inline ll ksm(ll x,ll y){    ll ans=1ll;    x%=mo;    while(y){        if(y&1)(ans*=x)%=mo;        (x*=x)%=mo;        y>>=1;    }    return ans%mo;}ll sum(ll x,ll y){    if(!y)return 1;    else if(y&1)return (sum(x,y/2)*(1+ksm(x,y/2+1))%mo);    else return ((sum(x,y/2-1)*(1+ksm(x,y/2+1)))%mo+ksm(x,y/2)%mo)%mo;}ll n,m,a;int main(){    while(scanf("%lld%lld",&a,&m)==2){        if(m==0||a<=1){printf("1\n");continue;}        ll ans=1;        ll n=sqrt(a+0.5);        for(int i=2;i<=n;++i){            if(!(a%i)){                ll cnt=0ll;                while(!(a%i)){                    ++cnt;                    a/=i;                }                (ans*=sum(i,cnt*m))%=mo;            }        }        if(a>1)(ans*=sum(a,m))%=mo;        printf("%lld\n",ans);    }    return 0;}