您的位置:首页 > 其它

POJ 1845 Sumdiv

2014-11-04 11:23 169 查看
快速幂+等比数列求和。。。。

                                                                                                 Sumdiv
Time Limit: 1000MSMemory Limit: 30000K
Total Submissions: 12599Accepted: 3057
DescriptionConsider 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).InputThe only line contains the two natural numbers A and B, (0 <= A,B <= 50000000)separated by blanks.OutputThe only line of the output will contain S modulo 9901.Sample Input2 3Sample Output15Hint2^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). 
SourceRomania OI 2002

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;
const int MOD=9901;
typedef long long int LL;

int p[10000],n[10000],k,A,B;

LL power(LL p,LL n)  ///p^n
{
    LL ans=1;
    while(n>0)
    {
        if(n&1)
            ans=(ans*p)%MOD;
        n>>=1;
        p=(p*p)%MOD;
    }
    return ans%MOD;
}

LL Spower(LL p,LL n) ///1+p^1+p^2+p^3+....+p^n-1+p^n
{
    if(n==0) return 1;
    if(n&1)
        return ((Spower(p,n/2)%MOD)*((1+power(p,n/2+1))%MOD))%MOD;
    else
        return (((Spower(p,n/2-1)%MOD)*(1+power(p,n/2+1))%MOD)%MOD+power(p,n/2)%MOD)%MOD;
}

int main()
{

    while(scanf("%d%d",&A,&B)!=EOF)
    {
        k=0;
        for(int i=2;i*i<=A;)
        {
            if(A%i==0) p[k]=i,n[k]=0,k++;
            while(A%i==0)
            {
                n[k-1]++;
                A/=i;
            }
            if(i==2) i++;
            else i+=2;
        }
        if(A!=1)
        {
            p[k]=A;
            n[k++]=1;
        }
        LL ans=1;
        for(int i=0;i<k;i++)
        {
            ans=(ans*Spower(p,B*n[i]))%MOD;
        }
        printf("%I64d\n",ans);
    }
    return 0;
}
[/i]
* This source code was highlighted by YcdoiT. ( style: Codeblocks )
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: