[POJ] [1995] Raising Modulo Numbers

Problem Description

Loading...

Problem Analysis

The gist of the problem:

Some people love watching porn, some love building atomic bombs in basements, some love using Windows; I love computing (A1B1+A2B2+ … +AHBH)mod M

As the problem states, just compute it directly with fast exponentiation
and take the modulo according to the congruence theorem

PS: how can such people even exist in this world -.-

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include<stdio.h>
int pow(int a,int b,int m) {
    __int64 r=1,t=a;
    while(b) {
        if(b&1)
            r=r*t%m;
        t=t*t%m;
        b>>=1;
    }
    return (int)r;
}
int main() {
    int T;
    scanf("%d",&T);
    while(T--) {
        int M;
        scanf("%d",&M);
        int n;
        scanf("%d",&n);
        __int64 sum=0;
        while(n--) {
            int a,b;
            scanf("%d %d",&a,&b);
            sum=(sum+pow(a,b,M))%M;
        }
        printf("%lld\n",sum);
    }
    return 0;
}

Loading...