您的位置:首页 > 其它

1046 A^B Mod C

2016-04-03 00:14 417 查看
1046 A^B Mod C

基准时间限制:1 秒 空间限制:131072 KB

给出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

--------------
快速幂
*/
import java.util.Scanner;
public class Main1 {
static long PowerMod(long a,long b,long c){
long ans=1;
a%=c;
while(b>0){
if((b&1)==1)
ans=(ans*a)%c;
a=(a*a)%c;
b>>=1;

}
return ans;
}

public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
while(sc.hasNext()){
long a=sc.nextLong();
long b=sc.nextLong();
long c=sc.nextLong();
System.out.println(PowerMod(a,b,c));
}
sc.close();
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: