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

快速幂模板

时间:2016-07-23 12:00:47      阅读:111      评论:0      收藏:0      [点我收藏+]

标签:

快速幂求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 }

 

快速幂模板

标签:

原文地址:http://www.cnblogs.com/yexiaozi/p/5698023.html

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