POJ 1845 质因数分解+等比数列求和

题意:给出A,B,求A^B的所有约数之和

思路:首先给A分解质因数,若A = C1 ^ e1 * ... * Cn ^ en,则A ^ B = C1 ^ (e1 * B) * ... * Cn ^ (en * B),

           容易知道,其所有约数之和为:

           Ans = [C1 ^ 0 + C1 ^ 1 + ... + C1 ^ (e1 * B)] * [C2 ^ 0  + ... + C2 ^ (e2 * B)] * ... * [Cn ^ 0 + ... + Cn ^ (en * B)]

           每一个因式都是一个等比数列,二分即可

代码:

#include <cstdio>

const int Mod = 9901;
typedef long long LL;

LL Powmod(LL x, LL exp){
	LL s = 1;
	while(exp){
		if(exp & 1)s = s * x % Mod;
		x = x * x % Mod;
		exp >>= 1;
	}
	return s;
}

LL c[Mod], e[Mod], cnt, A, B;

void GetFacts(LL n){
	for(LL i = 2;i * i <= n;i ++)
		if(n % i == 0){
			n /= i, c[++cnt] = i, e[cnt] = 1;
			while(n % i == 0) n /= i, e[cnt] ++;
		}
	if(n > 1)c[++cnt] = n, e[cnt] = 1;
}

LL solve(LL x, LL exp){
	if(!exp)return 1;
	LL s = solve(x, (exp - 1) >> 1) * (1 + Powmod(x, (exp + 1) >> 1)) % Mod;
	if(exp % 2 == 0) s = (s + Powmod(x, exp)) % Mod;
	return s;
}

int main(){
	scanf("%lld%lld", &A, &B);
	if(!A){
		puts("0");return 0;
	}
	GetFacts(A);
	LL Ans = 1;
	for(int i = 1;i <= cnt; i++)
		Ans = Ans * solve(c[i], e[i] * B) % Mod;
	printf("%lld\n", Ans);
	return 0;
}




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值