快速幂(提速度)+快速乘(防爆精度)

https://ac.nowcoder.com/acm/contest/3800/A

题意:求A的B次方模P

1T10~3,1≤A,B,P≤10~18

//#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <stdio.h>
#include <queue>
#include <stack>;
#include <map>
#include <set>
#include <ctype.h>
#include <string.h>
#include <vector>
#define ME(x , y) memset(x , y , sizeof(x))
#define SF(n) scanf("%d" , &n)
#define rep(i , n) for(int i = 0 ; i < n ; i ++)
#define INF  0x3f3f3f3f
#define PI acos(-1)
using namespace std;
typedef long long ll ;
int a[10009];

ll mul(ll a , ll b , ll mod)
{
    ll ans = 0 ;
    while(b)
    {
        if(b&1)
        {
            ans = (ans + a) % mod ;
        }
        b >>= 1 ;
        a = (a + a) % mod;
    }
    return ans ;
}

ll quickpow(ll a,  ll b , ll mod)
{
    ll ans = 1 ;
    while(b)
    {
        if(b&1)
        {
            ans = mul(ans , a , mod) ;
        }
        b >>= 1 ;
        a = mul(a , a, mod) ;
    }
    return ans ;
}

int main()
{
    /*#ifdef ONLINE_JUDGE
    #else
        freopen("D:/c++/in.txt", "r", stdin);
        freopen("D:/c++/out.txt", "w", stdout);
    #endif*/
    int t ;
    scanf("%d" ,&t);
    while(t--)
    {
        ll a , b , p ;
        scanf("%lld%lld%lld" , &a , &b , &p);
        cout << quickpow(a , b , p) << endl;
    }


    return 0 ;
}

猜你喜欢

转载自www.cnblogs.com/nonames/p/12151607.html