POJ-1845-Sumdiv (唯一分解定理、快速幂、费马小定理)

原题链接:
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的因子之和。
题解:
利用唯一分解定理,将a标准分解,然后每一个素数的指数都扩大b倍,最后利用公式求和,等比数列求和时,分母可以通过费马小定理求逆元,然后一同求余。
附上AC代码:

#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstring>
using namespace std;
const int mod=9901;
const int N=sqrt(5e7)+5;
int prime[N],cnt=0;
bool vis[N];
void el()//素数打表
{
    memset(vis,0,sizeof(vis));
    for(int i=2;i<=N;i++)
    {
        if(!vis[i])
        {
            prime[cnt++]=i;
            for(int j=i*2;j<=N;j+=i)
            {
                vis[j]=1;
            }
        }
    }
}
int quickpow(int a,int b)//快速幂
{
    int ans=1;
    while(b!=0)
    {
        if(b&1)
            ans=(ans*a)%mod;
        a=(a*a)%mod;
        b>>=1;
    }
    return ans;
}
int main()
{
    el();
    int a,b;
    scanf("%d%d",&a,&b);
    int sum=1;
    for(int i=0;i<cnt&&prime[i]<=a;i++)//唯一分解定理求出a的分解后各个素数的指数
    {
        if(a%prime[i]==0)
        {
            int e=0;
            while(a%prime[i]==0)
            {
                a/=prime[i];
                e++;
            }
            //利用快速幂计算出因子的和(等比数列求和)以及费马小定理求出逆元
            int x=(((quickpow(prime[i],(b*e+1)))-1)*quickpow(prime[i]-1,mod-2))%mod;
            sum=(sum*x)%mod;//求和
        }
    }
    printf("%d\n",sum);
    return 0;
}

欢迎评论!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值