您的位置:首页 > 其它

POJ 1845 Sumdiv(逆元)

2015-07-11 12:44 393 查看
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=p1^a1*p2*a2.....那么A^B=(p1^(a1*B)*p2^(a2*B).....
那么对于求和操作来说sum=(1+p1+p1^2...p1^(a1*B)(1+p2............
所以就是等比数列求和,那么sum=(pi^(ai*B+1)-1)/(pi-1)%mod;
那么a/b%mod=a%(b*mod)/b;
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<string>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<set>
using namespace std;
#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )
#define CLEAR( a , x ) memset ( a , x , sizeof a )
const int INF=0x3f3f3f3f;
typedef long long LL;
const int maxn=10010;
const LL mod=9901;
LL A,B;
int prim[maxn+10];
int vis[maxn+10];
int cnt;
void get_prim()
{
    for(int i=2;i*i<=maxn;i++)
    {
        if(!vis[i])
            for(int j=i*i;j<=maxn;j+=i)
               vis[j]=1;
    }
    for(int i=2;i<=maxn;i++)
        if(!vis[i]) prim[cnt++]=i;
}
//A^B%mod
LL quick_add(LL a,LL b,LL MOD)
{
    LL ans=0;
    while(b)
    {
        if(b&1)
            ans=(ans+a)%MOD;
        b>>=1;
        a=(a+a)%MOD;
    }
    return ans;
}
LL quick_mod(LL a,LL b,LL MOD)
{
    LL ans=1;
    while(b)
    {
        if(b&1)
            ans=quick_add(ans,a,MOD);
        b>>=1;
        a=quick_add(a,a,MOD);
    }
    return ans;
}
void solve()
{
    LL ans=1;
    for(int i=0;i<cnt&&prim[i]*prim[i]<=A;i++)
    {
        if(A%prim[i]==0)
        {
            int num=0;
            while(A%prim[i]==0)
            {
                A/=prim[i];
                num++;
            }
            LL MOD=(prim[i]-1)*mod;
            ans*=(quick_mod(prim[i],num*B+1,MOD)-1+MOD)/(prim[i]-1);
            ans%=mod;
        }
    }
    if(A>1)
    {
        LL MOD=(A-1)*mod;
        ans*=(quick_mod(A,B+1,MOD)-1+MOD)/(A-1);
        ans%=mod;
    }
    printf("%I64d\n",ans);
}
int main()
{
    get_prim();
    while(~scanf("%I64d%I64d",&A,&B))
       solve();
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: