[Blue Bridge Cup] ADV-204 fast power

Subject description:

Given A, B, P , seeking (A ^ B) MOD P .

Enter a description:

START input total ⼀ ⾏. ⾏ three first frame number, N, M, P. (A, B is a long long range comes in handy negative integer, P is int comes in handy in a negative integer).

Output Description:

An integer representing the remaining space of the box. 

Sample input:

2 5 3

Sample output:

2

Problem-solving ideas:

Fast integer powers of the time complexity is {\color{Green} O(log_{2}n)}, to prevent TLE, there are two kinds of non-recursive and recursive algorithms, I understand is to look at the "algorithm notes," Ching God.

AC Code:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

ll quickPow(ll x,ll n,ll m)    //整数快速幂,计算x^n%m
{
    ll ans = 1;
    x %= m;
    while(n)
    {
        if(n&1)   //奇数
        {
            ans = ans*x%m;
        }
        x = x*x%m;
        n >>= 1;
    }
    return ans;
}

ll binaryPow(ll x,ll n,ll m)    //整数快速幂,计算x^n%m递归求解
{
    if(n == 0) return 1;    //若n为0,则x^0=1
    else if(n&2)    //若n为奇数,转换为n-1
    {
        return n*binaryPow(x,n-1,m)%m;
    }
    else    //若n为偶数,转换为n/2
    {
        ll ans = binaryPow(x,n/2,m);
        return ans*ans%m;
    }
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    ll a,b,m;
    cin >> a >> b >> m;
    cout << quickPow(a,b,m) << endl;
    return 0;
}

 

Published 415 original articles · won praise 118 · Views 400,000 +

Guess you like

Origin blog.csdn.net/weixin_42449444/article/details/102989593