【POJ 1995】 Raising Modulo Numbers

【题目链接】

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

【算法】

          快速幂

【代码】

         

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cerrno>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <limits>
#include <list>
#include <map>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <utility>
#include <vector>
#include <cwchar>
#include <cwctype>
#include <stack>
#include <limits.h>
using namespace std;

long long a,b,h,ans,T,p;

template <typename T> inline void read(T &x)
{
    long long f = 1; x = 0;
    char c = getchar();
    for (; !isdigit(c); c = getchar()) { if (c == '-') f = -f; }
    for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + c - '0';
    x *= f;
}
template <typename T> inline void write(T x)
{
    if (x < 0)
    {
        putchar('-');
        x = -x;
    }
    if (x > 9) write(x/10);
    putchar(x%10+'0');
}
template <typename T> inline void writeln(T x)
{
    write(x);
    puts("");
}
inline long long power(long long a,long long n)
{
        long long ans = 1,b = a;
        while (n)
        {
                if (n & 1) ans = (ans * b) % p;
                b = (b * b) % p;
                n >>= 1;    
        }    
        return ans;
}

int main() 
{
        
        read(T);
        while (T--)
        {
                ans = 0;
                read(p); read(h);
                while (h--)
                {
                        read(a); read(b);
                        ans = (ans + power(a,b)) % p;
                }
                writeln(ans);
        }
        
        return 0;
    
}

 

posted @ 2018-06-27 13:44  evenbao  阅读(111)  评论(0编辑  收藏  举报