Sumdiv(唯一分解定理、逆元)

该博客介绍了如何利用编程解决数学问题,具体是求两个自然数A和B的所有因子之和,并计算该和对9901取模的结果。博主通过快速幂和质因数分解的方法推导出解决方案,展示了如何处理可能的除法取模问题,以及在编程中使用逆元的概念。此外,还提供了具体的C++代码实现作为示例。
摘要由CSDN通过智能技术生成

传送门
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).
大致翻译:
题意:求AB所有因子的和
考虑两个自然数A和B。让我们是所有自然因子的和。确定S模9901(S除以9901的其余部分)。

输入

唯一一行包含两个自然数A和B,(0<=A,B<=50000000),用空格隔开。

输出

输出的唯一一行将包含S模9901。

样本输入

2 3

样本输出

15

提示

2^3=8。

8的自然因子为:1,2,4,8,它们的和是15。

15模9901是15(应该是输出)。
思路:
在这里插入图片描述
公式推导举例:
在这里插入图片描述
由最后得到的公式我们发现由于公式中有除法所以需要运用逆元求解;
逆元(实际上就是求倒数)一些解释:
在这里插入图片描述

#include <stdio.h>
#include<math.h> 
#include<stdlib.h>
#include<cstring>
#include <iostream>
#include<vector>
using namespace std;
typedef long long ll;
const int mod=9901;
std::vector<ll>prime;//存质数
std::vector<ll>times;//存质数的次数,即几次方
inline void divide(ll x){//查找组成x的质数和质数的幂
	for(ll i=2;i*i<=x;i++){
		if(x%i==0){
			prime.push_back(i);
			ll cnt=0;//记录次数
			while(x%i==0){
				x/=i;
				cnt++; 
			}
			times.push_back(cnt);
		}
	}
	if(x>1){//若不为1说明此时x为质数且它的幂为1,记录下来
		prime.push_back(x);
		times.push_back(1);
	}
} 
inline ll qpow(ll a,ll b){//快速幂
	ll ans=1;
	while(b){
		if(b&1) ans=ans*a%mod;
		a=a*a%mod;
		b>>=1;
	}
	return ans;
} 
int main(){
	ll a,b;
	scanf("%lld %lld",&a,&b);
	if(a==0){//特判,若a为0说明此时a无因子
		cout<<'0'<<endl;return 0;
	}
	divide(a);
	ll ans=1;
	for(int i=0,t=prime.size();i<t;i++){
		times[i]*=b;//prime[i]的幂
		if((prime[i]-1)%mod==0) ans=ans*(times[i]+1)%mod;//公式的分母%mod不能为0,否则没意义(运用逆元需满足prime[i]-1不能为mod的倍数,否则逆元不成立)
		else ans=ans*((qpow(prime[i],times[i]+1)-1+mod)*qpow(prime[i]-1,mod-2)%mod)%mod;//逆元,除法取模为了防止负数情况所以最好加上mod,养成一个好习惯~
	}
	printf("%lld\n",ans);
    return 0;
}
  • 9
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值