C语言 | Leetcode C语言题解之第372题超级次方

题目:

题解:

cpp 复制代码
//计算a的b次方
int mypow(int a, int b){
    a = a % 1337; // 防止a过大超出规模
    int ret = 1;
    for(int i = 0; i < b; i++){
        ret *= a;
        ret = ret % 1337; //防止超出规模
    }
    return ret;
}
//整体计算
int superPow(int a, int* b, int bSize){
    if(a == 1) return 1;
    int sum = 1;
    for(int i = 0; i < bSize; i++){
        //由例子可知(x的10次方的次数)为(bSize-1),所以写在最开始,使得多出来的一次(循环次数为bSize)为计算1的十次方
        sum = mypow(sum, 10); 
        sum *= mypow(a, b[i]);
        sum = sum % 1337; //防止超出规模
    }


    return sum;
}
相关推荐
练习时长一年21 小时前
Leetcode热题100(跳跃游戏 II)
算法·leetcode·游戏
小白菜又菜1 天前
Leetcode 3432. Count Partitions with Even Sum Difference
算法·leetcode
wuhen_n1 天前
LeetCode -- 15. 三数之和(中等)
前端·javascript·算法·leetcode
sin_hielo1 天前
leetcode 2483
数据结构·算法·leetcode
sevenez1 天前
Vibe Coding 实战笔记:从“修好了C坏了AB”到企业级数据库架构重构
c语言·笔记·数据库架构
一路往蓝-Anbo1 天前
【第20期】延时的艺术:HAL_Delay vs vTaskDelay
c语言·数据结构·stm32·单片机·嵌入式硬件
wuhen_n1 天前
LeetCode -- 1:两数之和(简单)
javascript·算法·leetcode·职场和发展
就不掉头发1 天前
I/O复用
运维·服务器·c语言·开发语言
Jeremy爱编码1 天前
leetcode课程表
算法·leetcode·职场和发展
努力学算法的蒟蒻1 天前
day46(12.27)——leetcode面试经典150
算法·leetcode·面试