POJ1845 Sumdiv 递归

题目链接http://poj.org/problem?id=1845分析将 AAA 分解质因数为 p1c1∗p2c2∗...∗pncnp_1 ^ {c_1} * p_2 ^ {c_2} * ... * p_n ^ {c_n}p1c1​​∗p2c2​​∗...∗pncn​​,则 ABA ^ BAB 为 p1B∗c1∗p2B∗c2∗...∗pnB∗cnp_1 ^ {B * c_1} * p_2...
摘要由CSDN通过智能技术生成

题目链接

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

分析

A A A 分解质因数为 p 1 c 1 ∗ p 2 c 2 ∗ . . . ∗ p n c n p_1 ^ {c_1} * p_2 ^ {c_2} * ... * p_n ^ {c_n} p1c1p2c2...pncn

A B A ^ B AB p 1 B ∗ c 1 ∗ p 2 B ∗ c 2 ∗ . . . ∗ p n B ∗ c n p_1 ^ {B * c_1} * p_2 ^ {B * c_2} * ... * p_n ^ {B * c_n} p1Bc1p2Bc2...pnBcn

其约数之和为 ( 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 + 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 + p 2 + . . . + p c 1 + p + p ^ 2 + ... + p ^ c 1+p+p2+...+pc,显然使用等比数列求和公式是行不通的,可以利用分治思想:

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

c c c 为偶数,则其等于 ( 1 + p c 2 ) ∗ ( 1 + p + p 2 + p c 2 − 1 ) + p c (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;
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值