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

poj1995 Raising Modulo Numbers (快速幂,挑战有模版,纪念一下A的第一道快速幂)

时间:2015-02-21 18:52:03      阅读:143      评论:0      收藏:0      [点我收藏+]

标签:

/*快速幂,时间复杂度,数据范围*/
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
long long a, b;
int n, m;
int sum, res;
typedef long long ll;

ll mod_pow(ll x, ll y, int z)
{
    res = 1;
    while(y > 0){
        if(y & 1)
            res = res * x % z;
        x = x * x % z;
        y >>= 1;
    }
    return res;
}

int main()
{
    int t;
    scanf("%d", &t);
    while(t--){
        sum = 0;
        scanf("%d%d", &m, &n);
        for(int i = 0; i < n; i++){
            scanf("%l64d%l64d", &a, &b);
            sum = (sum + mod_pow(a, b, m)) % m;
        }
       printf("%d\n", sum);
    }
    return 0;
}

 

poj1995 Raising Modulo Numbers (快速幂,挑战有模版,纪念一下A的第一道快速幂)

标签:

原文地址:http://www.cnblogs.com/Joe962850924/p/4297101.html

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