码迷,mamicode.com
首页 > 其他好文 > 详细

快速幂

时间:2020-01-13 21:48:19      阅读:61      评论:0      收藏:0      [点我收藏+]

标签:sam   put   代码   quic   结果   inpu   return   ret   static   

给出3个正整数A B C,求A^B Mod C。

例如,3 5 8,3^5 Mod 8 = 3。

Input3个正整数A B C,中间用空格分隔。(1 <= A,B,C <= 10^9)Output输出计算结果Sample Input

3 5 8

Sample Output

3

代码:
     public static long quick_pow(long a,long b,long mod){
               long ans=1;
               while(b>0){
                     if((b&1)==1) ans=ans*a%mod;
                     a=a*a%mod;
                     b/=2;
               }
               return ans;
       }

快速幂

标签:sam   put   代码   quic   结果   inpu   return   ret   static   

原文地址:https://www.cnblogs.com/qdu-lkc/p/12189286.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!