【數論】【poj1845】Sumdiv

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 9901
using 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;
}   
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章