poj 1845 Sumdiv

本文介绍了一个算法问题,即计算A^B的所有自然约数之和并对9901取模。通过分解质因数并利用等比数列求和公式,结合快速幂运算和扩展欧几里得算法求逆元的方法,实现了高效求解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Sumdiv
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 19529 Accepted: 4916

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


题目大意:求A^B所有约数之和

思路: 
由于乘性函数的性质 
题目等价于求(1+a1+a1^2+a1^3+a1^4+.....+a1^(b1k)) *(1+a2+a2^2+a2^3+a2^4+......+a2^(b2k))+......
=s1*s2*s3......*sn
根据等比数列求和易知
(1-an^(bnk+1))/1-an
an为约数,bn为该约数的个数 

由于出现除法取模,需要求逆元,记得讨论逆元不存在,逆元存在当且仅当gcd(x,MOD)==1

代码如下:

#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long LL;
const LL maxn=100005;
const LL MOD=9901;
LL a[maxn],b[maxn];
LL quickpow(LL n,LL m)
{
	n%=MOD;
	if(n==0)return MOD-1;
	if(n==1)return 0;
	LL ans=1;
	while(m)
	{
		if(m&1)ans*=n,ans%=MOD;
		m>>=1;
		n*=n,n%=MOD;
	}
	ans=(ans%MOD+MOD)%MOD;
	return ans-1;
}
LL getfac(LL n)
{
	LL index=0;
	for(int i=2;i*i<=n;i++)
	{
		while(n%i==0&&n)b[index]++,n/=i;
		if(b[index])a[index++]=i;
	}
	if(n>1)b[index]++,a[index]=n,index++;
	return index;
}
LL x,y;
long long exgcd(long long a, long long b, long long &x, long long &y)
{
	if (a == 0){
		x = 0;
		y = 1;
		return b;
	}
	else
	{
		long long tx, ty;
		long long d = exgcd(b%a, a, tx, ty);
		x = ty - (b / a)*tx;
		y = tx;
		return d;
	}
}
int main()
{
	LL A,B;
	LL index=0;
	while(cin>>A>>B)
	{
		memset(b,0,sizeof(b));
		LL cnt=getfac(A),ans=1;
		for(int i=0;i<cnt;i++)
		{
			//if(a[i]%MOD==0)continue;
			if(a[i]%MOD==1){ans=ans*(b[i]*B+1),ans%=MOD;continue;}
			LL L=quickpow(a[i],b[i]*B+1);
			exgcd(a[i]-1,MOD,x,y);
			LL R=x;
			L=((L%MOD)+MOD)%MOD;
			R=((R%MOD)+MOD)%MOD;
			ans*=((L*R)%MOD+MOD)%MOD,ans%=MOD;
		}
		ans=((ans%MOD)+MOD)%MOD;
		cout<<ans<<endl;
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值