pku 1845 Sumdiv(约数和+快速幂取模+逆元)

#include <stdio.h>

 

typedef long long LL;

#define MAXN 100
#define MOD 9901

LL A,B;
const LL phiN=9900;

struct PNode
{
    LL p;
    LL n;
}pri[MAXN];
int idx;

void findPri(LL n)
{
    idx=0;
    for(LL i = 2; i*i <= n; i++)
    {
        if(n % i == 0)
        {
            pri[idx].p = i;
            pri[idx].n = 1;
            n /= i;
            while(n % i == 0)
            {
                pri[idx].n++;
                n /= i;
            }
       //     pri[idx].n *= B;
       //     pri[idx].n++;
            idx++;
        }
    }
    if(n > 1)
    {
        pri[idx].p = n;
   //     pri[idx].n = B+1;
        pri[idx].n = 1;
        idx++;
    }
}

inline LL mod_exp(LL a,LL b)
{
  /*  LL s=1;
    while(b)
    {
        if(b&1) s = s * a % MOD;
        a = a * a % MOD;
        b >>= 1;
    }
    return s;*/

    bool bit[64];
    int i=0;
    while(b)
    {
        bit[i++] = b & 1;
        b >>= 1;
    }
    LL s=1;
    for(i--; i >= 0; i--)
    {
        s = s * s % MOD;
        if(bit[i]) s = s * a % MOD;
    }
    return s;
}

void solve()
{
    findPri(A);

    LL p,n,t,a=1,b=1;
    for(int i=0;i<idx;i++)
    {
        p = pri[i].p;
        n = pri[i].n * B + 1;

        t = mod_exp(p,n);
        if(t == 1)
        {
            a = (a * n) % MOD;
        }
        else
        {
            a = ( a * (t-1) ) % MOD;
            b = (b * (p-1)) % MOD;
        }
    }
    if(a < 0) a += MOD;
    b = mod_exp(b,phiN-1);
    a = a*b % MOD;
    printf("%I64d\n",a);
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("tdata.txt","r",stdin);
#endif
    while(scanf("%I64d %I64d",&A,&B)!=EOF)
    {
        solve();
    }
    return 0;
}

posted @ 2010-09-15 20:49  菜到不得鸟  阅读(260)  评论(0)    收藏  举报