【因数之和】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)
相关推荐
chao_78921 分钟前
二分查找篇——搜索旋转排序数组【LeetCode】一次二分查找
数据结构·python·算法·leetcode·二分查找
烛阴31 分钟前
Python装饰器解除:如何让被装饰的函数重获自由?
前端·python
Boilermaker199243 分钟前
【Java EE】Mybatis-Plus
java·开发语言·java-ee
今日热点1 小时前
小程序主体变更全攻略:流程、资料与异常处理方案
经验分享·微信·小程序·企业微信·微信公众平台·微信开放平台
aramae1 小时前
C++ -- STL -- vector
开发语言·c++·笔记·后端·visual studio
Tony小周1 小时前
实现一个点击输入框可以弹出的数字软键盘控件 qt 5.12
开发语言·数据库·qt
noravinsc1 小时前
django 一个表中包括id和parentid,如何通过parentid找到全部父爷id
python·django·sqlite
lixzest1 小时前
C++ Lambda 表达式详解
服务器·开发语言·c++·算法
ajassi20001 小时前
开源 python 应用 开发(三)python语法介绍
linux·python·开源·自动化
fen_fen1 小时前
学习笔记(32):matplotlib绘制简单图表-数据分布图
笔记·学习·matplotlib