首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网络技术 > 网络基础 >

Raising Modulo Numbers 高速幂取模基础

2012-09-09 
Raising Modulo Numbers 快速幂取模基础#include stdio.h#include cmathlong long quick_mod(long lon

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;}

热点排行