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;
}
相关推荐
文丑颜不良啊10 小时前
LeetCode2221~2240题目解析
java·leetcode
Navigator_Z11 小时前
LeetCode //C - 1317. Convert Integer to the Sum of Two No-Zero Integers
c语言·算法·leetcode
冻柠檬飞冰走茶12 小时前
字母移动游戏题解:贪心求最长可保留连续子串(附 C 实现)
c语言·开发语言·数据结构·算法·动态规划
文丑颜不良啊12 小时前
LeetCode2161~2180题目解析
java·leetcode
文丑颜不良啊14 小时前
LeetCode2141~2160题目解析
java·leetcode
Hhy_110716 小时前
《C++深度解构06》手撕 string —— 从底层内存管理到深浅拷贝彻底吃透
c语言·开发语言·c++·学习·visual studio
曦樂~1 天前
Cpp面筋
c语言·c++
Dream it possible!1 天前
LeetCode 面试经典 150_多维动态规划_交错字符串(97_C++_中等)
算法·leetcode·面试·动态规划
心抵鹊1 天前
算法之双指针系列(一)
算法·leetcode
Pniubi1 天前
力扣560.和为k的子数组
算法·leetcode·职场和发展