51Nod 1046 A^B Mod C

来源:互联网 发布:中国地名数据库下载 编辑:程序博客网 时间:2024/06/05 10:57
给出3个正整数A B C,求A^B Mod C。
例如,3 5 8,3^5 Mod 8 = 3。
Input

3个正整数A B C,中间用空格分隔。(1 <= A,B,C <= 10^9)

Output

输出计算结果

Input示例

3 5 8

Output示例

3

题意:中文题。。。

思路:快速幂模板。。注意范围就可;

下面附上代码:

#include<bits/stdc++.h>using namespace std;typedef long long ll;ll KSM(ll a,ll b,ll c){ll ans=1;a=a%c;while(b){if(b&1) ans=(ans*a)%c;b/=2;a=(a*a)%c;}return ans;}int main(){ll a,b,c;scanf("%lld %lld %lld",&a,&b,&c);printf("%lld\n",KSM(a,b,c));return 0; } 


原创粉丝点击