math函数库中的pow函数效率低,如代码所展示的多种幂运算算法,可显著降低时间复杂度,提高运行效率
快速幂算法,递归与循环int main(){ unsigned long long qick_pow1(unsigned long long a, unsigned long long n, unsigned long long p); unsigned long long qick_pow2(unsigned long long a, unsigned long long n, unsigned long long p); unsigned long long qick_pow3(unsigned long long a, unsigned long long n, unsigned long long p); unsigned long long a, n, p; scanf("%llu %llu %llu", &a, &n, &p); printf("%llu\n", qick_pow1(a, n, p)); printf("%llu\n", qick_pow2(a, n, p)); printf("%llu\n", qick_pow3(a, n, p)); printf("%llu\n", (unsigned long long)pow(a, n) % p);}
评论