ZOJ2150 Raising Modulo Numbers 快速幂

                                           ZOJ2150

快速幂,但是用递归式的好像会栈溢出。

#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<cmath>
using namespace std;
long long  M,i;
#define LL long long 
int _work(LL a,LL n)
{
    LL ans=1;
    while(n){
     if(n&1){
       ans=(ans*a)%M;
       n--;
     }
     else{
n/=2; a=a*a%M;
} } return ans; } int main() { LL T,a,b,ans,n; scanf("%lld",&T); while(T--){ scanf("%lld",&M); scanf("%lld",&n); ans=0; for(i=1;i<=n;i++) { scanf("%lld%lld",&a,&b); ans=(_work(a,b)+ans)%M; } printf("%lld ",ans); } return 0; }


原文地址:https://www.cnblogs.com/hua-dong/p/7603940.html