poj 1845 Sumdiv(所有约数的和)

                                                                                                                                  Sumdiv
                                                                                                                                    点击打开题目链接
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 14756 Accepted: 3631

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

Source

题意很简单,给出A,B,求A^B 的所有约数的和,由于数太大了,所以最后结果mod 9901后再输出;


考虑对A进行质因子分解,,则(都是质数);用函数代表n的所有约数的和的函数;

对于一个质因子分解后的数,其所有约数的和为即:


还等于:

如果直接暴力求解等比数列可能会超时,所以,我选择了,对于等比数列求和后,再用逆元,但是此时有一点要注意:可能是9901,的倍数那么将不存在逆元,会导致WA,那么之前对于的情形又出现了,如果,那么:;这里就是能应用的这个公式,用到快速幂取模,在快速幂取模中一步乘法可能会溢出long long ,因此有添加了一个对于大数a*b (mod m)的函数;

具体见代码:

#include <iostream>
#include <vector>
#include <queue>
#include <set>
#include <math.h>
#include <map>
#include <stack>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#define max(a,b) (a>b?a:b)
#define N 10010
#define mod 9901
#define min(a,b) (a<b?a:b)
using namespace std;
typedef long long LL;
struct node
{
    LL p,cnt;
    node()
    {
        p=0,cnt=0;
    }
} pc[N];
LL num;
LL mul_mod(LL a,LL b,LL m)///大数乘法取模防止爆long long 
{
    LL s=0;
    while(b)
    {
        if(b&1)
            s=(s+a)%m;
        a=(a*2)%m;
        b=b>>1;
    }
    return s<0?s+m:s;
}
LL pow_mod(LL a,LL b,LL m)///快速幂取模
{
    LL res,t;
    res=1%m;
    t=a%m;
    while(b)
    {
        if(b&1)
        {
            res=mul_mod(res,t,m);
        }
        t=mul_mod(t,t,m);
        b>>=1;
    }
    return res<0?(res+m):res;
}
LL slove(LL x1,LL y1)
{
    LL ans=1;
    LL i=0;
    for(i=0; i<num; i++)
    {
        LL b=((pc[i].cnt)*(y1));
        LL q=pow_mod(pc[i].p,b+1,mod*(pc[i].p-1));///套入公式
        ans*=(q-1)/(pc[i].p-1);
        ans%=mod;//这个地方要注意,ans可能大于9901,要在这里再取一次模,悲剧的因为这WA好多次
    }
    return ans;
}
void init(int n)
{
    for(int i=0; i<num; i++)
    {
        pc[i].cnt=0;
        pc[i].p=0;
    }
}
int main()
{
    LL a,b,i;
    while(~scanf("%I64d%I64d",&a,&b))
    {
        num=0;
        LL k=(LL) sqrt(a+0.0);
        LL n=a;
        for(i=2; i<=k; i++)///质因子分解
        {
            if(a%i==0)
            {
                pc[num++].p=i;
                while(a%i==0)
                {
                    pc[num-1].cnt++;
                    a/=i;
                }
            }
        }
        if(a>1)//如果a为素数的情况
        {
            pc[num++].p=a;
            pc[num-1].cnt++;
        }
        LL ans;
        ans=slove(n,b);
        printf("%I64d\n",ans);
        init(num);
    }
    return 0;
}

对了,这题网上也有用二分求等比数列的和,然后过了的,不会二分。T^T。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值