快速幂模板

快速幂求n^n;

 1 int f1(int a,int b)
 2 {
 3     int t=1;
 4     while(b)
 5     {
 6         if(b % 2 != 0)
 7         {
 8             t*=a;
 9             b--;            
10         }
11         a*=a;
12         b/=2;
13     }
14     return t;
15 }

快速幂求n^n后y位;

 1 int f2(int a,int b)
 2 {
 3     int t=1;
 4     while(b)
 5     {
 6         if(b % 2 != 0)
 7         {
 8             t=(t*a)%x;    //x控制要求的位数 
 9             b--;
10         }
11         a=(a*a)%x;
12         b/=2;
13     }
14     return t;
15 }

 

posted @ 2016-07-23 10:40  野小子&  阅读(793)  评论(0编辑  收藏  举报