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

幂取模

时间:2016-03-08 18:07:12      阅读:122      评论:0      收藏:0      [点我收藏+]

标签:

输入正整数 a,n,m 输出 a^n %m 的值

#include<iostream>
#include<string>
#include<string>
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
int pow_mod(int a,int n,int m){
    if(n==0) return 1;
    int x=pow_mod(a,n/2,m);
    long long ans=(long long ) x*x %m;
    if(n%2==1) ans=ans*a%m;
    return (int) ans;
 }
int main(){
  int n,m,a;
  while(cin>>n>>m>>a){
        cout<<pow_mod(a,m,n)<<endl;

  }

   return 0;
}

 

幂取模

标签:

原文地址:http://www.cnblogs.com/wintersong/p/5254900.html

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