快速幂取模

Given a b and p, output (a^b) % p   (2<=a<=100, 0<=b<=1000000000,  3<=p<=10000)

 

 

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4.  
  5. int quick_power(int a,int b,int p){
  6. int temp;
  7. if(b ==0){
  8. return1;
  9. }
  10. temp = quick_power(a, b /2, p);
  11. if(b &1){
  12. return(((temp * temp)% p)*(a % p))% p;
  13. }else{
  14. return(temp * temp)% p;
  15. }
  16. }
  17.  
  18. int main(int argc,char*argv[]){
  19. int a, b, p;
  20.  
  21. while(~scanf("%d%d%d",&a,&b,&p)){
  22. printf("%d\n", quick_power(a, b, p));
  23. }
  24. return0;
  25. }
posted on 2014-04-18 22:33  左手代码右手诗  阅读(112)  评论(0编辑  收藏  举报