本文共 763 字,大约阅读时间需要 2 分钟。
a 的 b 次方对 p 取模的值。
输入格式
三个整数 a,b,p ,在同一行用空格隔开。输出格式
输出一个整数,表示a^b mod p的值。数据范围
0≤a,b,p≤109 数据保证 p≠0 输入样例: 3 2 7 输出样例: 2ll fast_pow(ll a,ll b, int p) { if (b==0) return 1; ll x = fast_pow(a, b/2, p); ll res = x*x %p; if ( b%2==1 ) res = res*a % p; return res;}
#includeusing namespace std;int main(void) { int a,b,p;cin>>a>>b>>p; int res = 1 % p; while (b) { if (b & 1) res = res*1ll * a % p; a = a*1ll * a % p; b>>=1; } cout << res << endl; return 0; }
long long int mod = 100;long long int fast_power(long long int x, long long int y){ if (y < 0) return 0; long long int ans = 1; while (y) { if (y & 1) ans = ans * x %mod; x = x * x %mod; y >>= 1; } return ans;}
转载地址:http://myyzi.baihongyu.com/