#include \nint mod(int a, int b, int p) {\n int result = 1;\n while (b > 0) {\n if (b % 2 == 1) {\n result = (result * a) % p;\n }\n a = (a * a) % p;\n b /= 2;\n }\n return result;\n}\nint main() {\n int a, b, p;\n std::cout << "Enter the values of a, b, and p: ";\n std::cin >> a >> b >> p;\n int result = mod(a, b, p);\n std::cout << "a^b % p = " << result << std::endl;\n return 0;\n}\n这段代码中,mod函数使用快速幂算法计算a^b % p的值。main函数用于获取输入的a、b和p的值,并调用mod函数计算结果。最后,将结果输出到控制台。

C++ 代码:快速幂算法求解 a^b % p

原文地址: https://www.cveoy.top/t/topic/pGKC 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录