[POJ - 1995] Raising Modulo Numbers (fast power)

-->Raising Modulo Numbers

Descriptions:

Topic a lot, really useless, roughly meaning of problems

WITH

M

H

A1  B1

A2  B2

A 3   B 3

.........

A H   B H

The Z data set    requirements (A . 1 B . 1 + A 2 B 2 + ... + A H B H ) MOD M .

Sample Input

3
16
4
2 3
3 4
4 5
5 6
36123
1
2374859 3029382
17
1
3 18132

Sample Output

2
13195
13

Topic Link
https://vjudge.net/problem/POJ-1995

 

Fast power to direct

 

AC Code

#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#define IOS ios_base::sync_with_stdio(0); cin.tie(0);
#define Mod 1000000007
#define eps 1e-6
#defineLong Long LL
 #define INF 0x3f3f3f3f
 #define MEM (X, Y) Memset (X, Y, the sizeof (X))
 #define MAXN 30
 the using  namespace STD; 
LL MOD; 
LL ANS; 
int the Z, M, H; 
LL qpow ( a LL, LL n) // calculate% MOD n ^ a 
{ 
    LL Re = . 1 ;
     the while (n) 
    { 
        iF (& n . 1 ) // determines whether n is the last one. 1 
            Re = (Re * a)% MOD; 
        n >> = . 1 ; // rounding the last n 
        a = (a * a) MOD%; //将a平方
    }
    return re % mod;
}

int main()
{
    cin>>Z;
    while(Z--)
    {
        ans=0;
        cin>>M>>H;
        mod=M;
        for(int i=0;i<H;i++)
        {

            ll a,b;
            cin>>a>>b;
            ans+=qpow(a,b);
        }
        ans=qpow(ans,1);
        cout<<ans<<endl;
    }
}

 

Guess you like

Origin www.cnblogs.com/sky-stars/p/11210434.html