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).
#include
#include
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

using namespace std;

const int h = 10001;
int n,m;
struct node
{
int x;
int y;
}q[h];

long long int power(long long int pn,long long int pm) ///反复平方法来求A^B 省时间
{
long long int sq = 1;
while(pm>0)
{
if(pm%2)
{
sq = (sq*pn)%9901;
}
pm = pm / 2;
pn = pn * pn % 9901;
}
return sq;
}

long long int updata(long long int pn,long long int pm) ///递归二分求等比数列的和
{
if(pm == 0)
{
return 1;
}
if(pm%2)
{
return (updata(pn,pm/2)(1+power(pn,pm/2+1)))%9901; /// 当pm为奇数时,有公式来求等比数列的和 (1 + p + p^2 +…+ p^(n/2)) * (1 + p^(n/2+1))
}
else
{
return (updata(pn,pm/2-1)
(1+power(pn,pm/2+1)) + power(pn,pm/2))%9901; ///当pm为偶数时,有公式来求等比数列的和 (1 + p + p^2 +…+ p^(n/2-1)) * (1+p^(n/2+1)) + p^(n/2);
}
}

int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
int k = 0;
for(int i=2;ii<=n;) ///寻找质因子,一个很好的方法
{
if(n%i == 0)
{
q[k].x = i;
q[k].y = 0;
while(n%i == 0)
{
q[k].y++;
n /= i;
}
k++;
}
if(i == 2)
{
i++;
}
else
{
i = i + 2;
}
}
if(n!=1)
{
q[k].x = n;
q[k].y = 1;
k++;
}
int ans = 1;
for(int i=0;i<k;i++)
{
ans = (ans
(updata(q[i].x,q[i].y*m)%9901)%9901);
}
printf("%d\n",ans);
}
return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

理想不闪火

你的鼓励将是我最大的动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值