POJ 1995 - Raising Modulo Numbers(数学`快速幂运算)

来源:互联网 发布:qq for linux ubuntu 编辑:程序博客网 时间:2024/05/16 02:31

题目:

http://poj.org/problem?id=1995

思路:

快速幂运算,模版题。

CODE:

#include <iostream>#include <cstdio>#include <cmath>using namespace std;typedef long long ll;int m;ll cal(ll a, ll b){    ll s = 1;    while(b > 0) {        if(b & 1) s = s * a % m;        a = a * a % m;        b >>= 1;    }    return s;}int main(){//freopen("in", "r", stdin);    int T;    scanf("%d", &T);    while(T--) {        int n;        ll a, b, ans = 0;        scanf("%d %d", &m, &n);        while(n--) {            scanf("%lld %lld", &a, &b);            ans = (ans + cal(a, b)) % m;        }        printf("%I64d\n", ans);    }    return 0;}


0 0
原创粉丝点击