读书人

Raising Modulo Numbers 高速幂取模基

发布时间: 2012-09-09 09:27:54 作者: rapoo

Raising Modulo Numbers 快速幂取模基础

#include <stdio.h>#include <cmath>long long quick_mod(long long a,long long b,int m){    long long ans=1;    while(b)    {        if(b&1)        {            ans=(ans*a)%m;            b--;        }        b/=2;        a=a*a%m;    }    return ans;}int main(){    int t,n,m;    scanf("%d",&t);    long long a,b;    while(t--)    {        scanf("%d",&m);        scanf("%d",&n);        int sum=0;        while(n--)        {            scanf("%lld%lld",&a,&b);            sum+=quick_mod(a,b,m);        }        printf("%d\n",sum%m);    }    return 0;}

读书人网 >网络基础

热点推荐