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

其实这道题kuangbin有模板,利用了约数和定理:f(n)=(p1^0+p1^1+p1^2+…p1^a1)(p2^0+p2^1+p2^2+…p2^a2)…(pk^0+pk^1+pk^2+…pk^ak) 

#include<iostream>
#include<cstring>
#include<algorithm>
typedef long long ll;
using namespace std;
const int maxn=10000;
const int p=9901;
int prime[maxn+1];
void getprime()
{
	memset(prime,0,sizeof(prime));
	for(int i=2;i<=maxn;i++)
	{
		if(!prime[i])
			prime[++prime[0]]=i;
		for(int j=1;j<=prime[0]&&prime[j]<=maxn/i;j++)
		{
			prime[prime[j]*i]=1;
			if(i%prime[j]==0)break; 
		}
	}
}
ll factor[100][2];
int fatcnt;
int getfactors(ll x)
{
	fatcnt=0;
	ll temp=x;
	for(int i=1;prime[i]<=temp/prime[i];i++)
	{
		factor[fatcnt][1]=0;
		if(temp%prime[i]==0)
		{
			factor[fatcnt][0]=prime[i];
			while(temp%prime[i]==0)
			{
				factor[fatcnt][1]++;
				temp/=prime[i];
			}
			fatcnt++;
		}
	}
	if(temp!=1)
	{
		factor[fatcnt][0]=temp;
		factor[fatcnt++][1]=1;	
	} 
	return fatcnt;
}
ll pow_m(ll a,ll n)
{
	ll ret=1;
	ll temp=a%p;
	while(n)
	{
		if(n&1)ret=(ret*temp)%p;
		n>>=1;
		temp=temp*temp;
		temp%=p;
	}
	return ret;
}
ll sum(ll m,ll n)
{
	if(m==0)return 0;
	if(n==0)return 1;
	if(n&1)
		return ((1+pow_m(m,n/2+1))%p*sum(m,n/2)%p)%p;
	else return ((1+pow_m(m,n/2+1))%p*sum(m,n/2-1)+pow_m(m,n/2)%p)%p;
}
ll solve(ll a,ll b)
{
	getfactors(a);
	ll ans=1;
	for(int i=0;i<fatcnt;i++)
	{
		ans*=(sum(factor[i][0],b*factor[i][1])%p);
		ans%=p;
	}
	return ans;
}
int main()
{
	getprime();
	ll a,b;
	cin>>a>>b;
	cout<<solve(a,b)<<endl;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值