POJ1845 Sumdiv 递归

该博客介绍了如何使用分治法解决POJ1845 Sumdiv问题。博主分析了将A分解质因数后,AB的约数之和的计算方法,并针对奇偶性提出了不同的求和公式。最后,提供了AC代码实现。

题目链接

http://poj.org/problem?id=1845

分析

AAA 分解质因数为 p1c1∗p2c2∗...∗pncnp_1 ^ {c_1} * p_2 ^ {c_2} * ... * p_n ^ {c_n}p1c1p2c2...pncn

ABA ^ BABp1B∗c1∗p2B∗c2∗...∗pnB∗cnp_1 ^ {B * c_1} * p_2 ^ {B * c_2} * ... * p_n ^ {B * c_n}p1Bc1p2Bc2...pnBcn

其约数之和为 (1+p1+p12+...+p1B∗c1)∗(1+p2+p22+...+p2B∗c2)∗...∗(1+pn+pn2+...+pnB∗cn)(1 + p_1 + p_1 ^ 2 + ... + p_1 ^ {B * c_1}) * (1 + p_2 + p_2 ^ 2 + ... + p_2 ^ {B * c_2}) * ... * (1 + p_n + p_n ^ 2 + ... + p_n ^ {B * c_n})(1+p1+p12+...+p1Bc1)(1+p2+p22+...+p2Bc2)...(1+pn+pn2+...+pnBcn)

接下来是如何快速求 1+p+p2+...+pc1 + p + p ^ 2 + ... + p ^ c1+p+p2+...+pc,显然使用等比数列求和公式是行不通的,可以利用分治思想:

ccc 为奇数,则其等于 ((1+pc+12)∗(1+p+p2+...+pc−12)((1 + p ^ {\frac{c + 1}{2}}) * (1 + p + p ^ 2 + ... + p ^ {\frac{c - 1}{2}})((1+p2c+1)(1+p+p2+...+p2c1)

ccc 为偶数,则其等于 (1+pc2)∗(1+p+p2+pc2−1)+pc(1 + p ^ {\frac{c}{2}}) * (1 + p + p ^ 2 + p ^ {\frac{c}{2} - 1}) + p ^ c(1+p2c)(1+p+p2+p2c1)+pc

AC代码

#include <cstdio>

inline int read() {
	int num = 0;
	char c = getchar();
	while (c < '0' || c > '9') c = getchar();
	while (c >= '0' && c <= '9')
		num = num * 10 + c - '0', c = getchar();
	return num;
}

const int mod = 9901;

inline int qpow(int a, int b) {
	int ans = 1, x = a % mod;
	while (b) {
		if (b & 1) ans = ans * x % mod;
		x = x * x % mod, b >>= 1;
	}
	return ans;
}

inline int sum(int p, int q) {
	if (!q) return 1;
	if (q & 1) return (1 + qpow(p, q / 2 + 1)) * sum(p, q / 2) % mod;
	else return (sum(p, q - 1) + qpow(p, q)) % mod;
}

int main() {
	int a = read(), b = read(), ans = 1;
	if (a < 2) printf("%d", a);
	else {
		for (int i = 2; i * i <= a; ++i)
			if (a % i == 0) {
				int cnt = 0;
				while (a % i == 0) a /= i, ++cnt;
				ans = ans * sum(i, cnt * b) % mod;
			}
		if (a > 1) ans = ans * sum(a, b) % mod;
		printf("%d", ans);
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值