#include<iostream> #include<algorithm> using namespace std; #define ll long long int qmi(ll a, ll b, ll mod) { //求 m^n%p,时间复杂度 O(logn) ll flag = 1; while (b) { if (b & 1)flag = (flag*a)%mod; a = (a*a) % mod; b = b >> 1; } return flag%mod; } int qji(ll a, ll b, ll mod) { //求 m*n%p,时间复杂度 O(logn) ll flag = 0; while (b) { if (b & 1)flag = (flag + a) % mod; a = (a << 1) % mod; b = b >> 1; } return flag%mod; } int main() { ll a, b, c; cin >> a >> b >> c; cout << a << "^" << b << " mod " << c << "=" << qmi(a, b, c) << "\n"; cout << a << "*" << b << " mod " << c << "=" << qji(a, b, c) << "\n"; return 0; }
原文地址:https://www.cnblogs.com/52dxer/p/10421595.html
时间: 2024-11-03 13:03:49