Sumdiv&&http://poj.org/problem?id=1845&&约数和问题

本文解析了一道数学算法题,题目要求计算给定数的B次方的所有自然数除数之和并对9901取模。文章提供了解题思路与AC代码,包括寻找素因子、使用乘法逆元或二分幂法求和的方法。

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

Time Limit:1000MS Memory Limit:30000K
Total Submissions:9761 Accepted:2274

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).
这是一道数学性比较强的题,求的是给定数的所有约数和。
思路:首先找到该数的所有素因子及个数,然后求和,有两种方法:一乘法逆元,二,二分幂法,其中第一种方法有局限,只有当存在逆元时,才可以用。
AC代码:
#include<iostream>
#include<string.h>
#include<cstdio>
#include<cmath>
#define M 9901
#define N 10000
#define CLR(arr,val) memset(arr,val,sizeof(arr))
using namespace std;
typedef long long  L;
int prim[N];
int sum[N];
int res;
L pow( L p,L n)
{
	L res=1;
	while(n)
	{
		if(1&n) res=(res*p)%M;
		 p=(p%M*p%M)%M;
		 n=n>>1;
	}
	return res;

}
L _pow(L  n,L  m)
{
	if(n==0) return 0;
	else if(n==1||m==0) return 1;
	else
	{
		if(m&1) return (_pow(n,m/2)%M*(1+pow(n,m/2+1))%M)%M;
		else   return( _pow(n,m/2-1)%M*(1+pow(n,m/2+1))%M)%M+pow(n,m/2)%M;
	}
}
int main()
{
	int  a,b;
	scanf("%d%d",&a,&b);
	CLR(prim,0);
	CLR(sum,0);
	int count=0;
	for(int i=2;i*i<=a;++i)
	{
		if(a%i==0)
		{
			prim[++count]=i;
			while(a%i==0)
			{
				sum[count]++;
				a/=i;
			}
		}
	}
	if(a!=1) prim[++count]=a,sum[count]=1;      
	L ans=1;
	for(int i=1;i<=count;++i)
		 if(sum[i])  ans=ans*_pow(prim[i],sum[i]*b)%M;
		 printf("%d\n",ans);
	 return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值