POJ 1845 约数和+二分等比和+逆元

Sumdiv
Time Limit: 1000MS Memory Limit: 30000K
   

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的约数和mod 9901.

题解:分解质因子,a^b=p1^(k1*b)*p2^(k2*b)*...*pm(km*b)
和s=(1+p1+p1^2+...+p1^(k1*b))*(1+p2+p2^2+...+p2^(k2*b))*...*(1+pm+pm^2+...+pm^(km*b))

逆元法:
等比数列求和
s=(1+p1^(k1*b+1))/(p1-1)*(1+p2^(k2*b+1))/(p2-1)*...*(1+pm^(km*b+1))/(pm-1)
注意(a/b)%m  b是要和m互质才能求逆元
所以当b是m的倍数
因为9901是质数
所以这一项就可以看成9901这个质数的k*b次方
就可以直接乘(k*b+1)

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<stack>
#include<map>
using namespace std;
typedef long long ll;
stack<ll>sp;
map<ll,ll>sp1;
ll quickmod(ll a,ll k){
	ll ans=1;
	a=a%9901;
	while(k){
		if(k&1){
			ans=ans*a%9901;
		}
		k>>=1;
		a=a*a%9901;
	}
	return ans;
}
int main(){
	ll i,j,a,b;
	while(scanf("%lld%lld",&a,&b)!=EOF){
		sp1.clear();
		if(a==0){
			printf("0\n");
			continue;
		}
		if(a==1||b==0){
			printf("1\n");
			continue;
		}
		for(i=2;i*i<=a;i++){
			if(a%i==0){
				sp.push(i);
				while(a%i==0){
					a/=i;
					sp1[i]++;
				}
			}
			if(a==1)break;
		}
		if(a>1){
			sp.push(a);
			sp1[a]++;
		}
		ll ans=1;
		while(!sp.empty()){
			ll f=sp.top();
			sp.pop();
			ll t=(quickmod(f,sp1[f]*b+1)+9900)%9901;//防止quickmod出来是0  -1就变成了负数
			ll p=quickmod(f-1,(ll)(9901-2));
			if((f-1)%9901==0)ans=ans*(sp1[f]*b+1)%9901;//如果不互质
			else ans=ans*t*p%9901;
		}
		cout<<ans<<endl;
	}
	return 0;
}

等比数列二分求和

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<stack>
#include<map>
using namespace std;
typedef long long ll;
stack<ll>sp;
map<ll,ll>sp1;
ll power(ll a,ll k){
	ll ans=1;
	a%=9901;
	while(k){
		if(k&1){
			ans=ans*a%9901;
		}
		k>>=1;
		a=a*a%9901;
	}
	return ans;
}
ll sums(ll a,ll k){
	if(k==1)return a;
	ll t=sums(a,k/2);
	if(k&1){
		ll cur=power(a,k/2+1);
		t=(t+t*cur%9901)%9901;
		t=(t+cur)%9901;
	}
	else{
		ll cur=power(a,k/2);
		t=(t+t*cur%9901)%9901;
	}
	return t;
}
int main(){
	ll i,j,a,b;
	while(scanf("%lld%lld",&a,&b)!=EOF){
		sp1.clear();
		if(a==0){
			printf("0\n");
			continue;
		}
		if(a==1||b==0){
			printf("1\n");
			continue;
		}
		for(i=2;i*i<=a;i++){
			if(a%i==0){
				sp.push(i);
				while(a%i==0){
					a/=i;
					sp1[i]++;
				}
			}
			if(a==1)break;
		}
		if(a>1){
			sp.push(a);
			sp1[a]++;
		}
		ll ans=1;
		while(!sp.empty()){
			ll f=sp.top();
			sp.pop();
			ans=ans*(sums(f,sp1[f]*b)+1)%9901; 
		}
		cout<<ans<<endl;
	}
	return 0;
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值