【因数之和】python求解方法

输入两个整数A和B,求A的B次方的因子和,结果对1000000007取模。

python 复制代码
def mod_exp(base, exp, mod):
    result = 1
    while exp > 0:
        if exp % 2 == 1:
            result = (result * base) % mod
        base = (base * base) % mod
        exp //= 2
    return result

def sum_of_factors(n):
    total = 0
    limit = int(n**0.5)
    for i in range(1, limit + 1):
        if n % i == 0:
            total += i
            if i != n // i:  # 如果是不同的因子,添加两个因子
                total += n // i
    return total

def main(A, B):
    MOD = 1000000007
    power_value = mod_exp(A, B, MOD)
    factors_sum = sum_of_factors(power_value) % MOD
    return factors_sum

# 示例
A = 2
B = 3
result = main(A, B)
print(result)
相关推荐
iAm_Ike2 小时前
Go 中自定义类型与基础类型间的显式类型转换详解
jvm·数据库·python
iuvtsrt2 小时前
Golang怎么实现方法集与接口的匹配_Golang如何理解值类型和指针类型实现接口的区别【详解】
jvm·数据库·python
chao1898443 小时前
基于 SPEA2 的多目标优化算法 MATLAB 实现
开发语言·算法·matlab
赏金术士3 小时前
Kotlin 习题集 · 高级篇
android·开发语言·kotlin
旦莫3 小时前
AI驱动的纯视觉自动化测试:知识库里应该积累什么知识内容
人工智能·python·测试开发·pytest·ai测试
Cat_Rocky4 小时前
k8s-持久化存储,粗浅学习
java·学习·kubernetes
楼兰公子4 小时前
buildroot 在编译rust时裁剪平台类型数量的方法
开发语言·后端·rust
知识领航员4 小时前
蘑兔AI音乐深度实测:功能拆解、实测表现与适用场景
java·c语言·c++·人工智能·python·算法·github
AOwhisky4 小时前
虚拟化技术学习笔记
linux·运维·笔记·学习·虚拟化技术
吴声子夜歌4 小时前
Go——并发编程
开发语言·后端·golang