“算法精粹:快速幂技术及其在逆元求解中的应用“

快速幂

快速幂:快速求ab % p的问题,时间复杂度:O(logb),若对于n组数据,那么时间复杂度为O(n∗logb)

给定 𝑛 组 𝑎𝑖,𝑏𝑖,𝑝𝑖,对于每组数据,求出𝑎𝑖𝑏𝑖mod𝑝𝑖 的值。

输入格式

第一行包含整数 𝑛。

接下来 𝑛 行,每行包含三个整数 𝑎𝑖,𝑏𝑖,𝑝𝑖。

输出格式

对于每组数据,输出一个结果,表示 𝑎𝑖𝑏𝑖mod𝑝𝑖 的值。

每个结果占一行。

数据范围

1≤𝑛≤100000,
1≤𝑎𝑖,𝑏𝑖,𝑝𝑖≤2×109

输入样例:
2
3 2 5
4 3 9
输出样例:
4
1

一.暴力解法 O(n∗b)会TLE

基本思路:对于n组数据,分别循环b次求出ab mod p

#include<iostream>
using namespace std;
int main()
{
    int n;
    cin>>n;
    while(n--)
    {
        int a,b,p;
        long long res=1;
        cin>>a>>b>>p;
        while(b--)
            res = res * a %p;
        cout<<res<<endl;
    }
}

二.快速幂解法 O(n∗logb)

基本思路:


注意:

  • b&1就是判断b的二进制表示中第0位上的数是否为1,若为1,b&1=true,反之b&1=false
  • b&1也可以用来判断奇数和偶数,b&1=true时为奇数,反之b&1=false时为偶数

快速幂之迭代版 O(n∗logb)

#include<iostream>
using namespace std;
long long qmi(long long a,int b,int p)
{
    long long res=1;
    while(b)//对b进行二进制化,从低位到高位
    {
        //如果b的二进制表示的第0位为1,则乘上当前的a
        if(b&1) res = res *a %p;
        //b右移一位
        b>>=1;
        //更新a,a依次为a^{2^0},a^{2^1},a^{2^2},....,a^{2^logb}
        a=a*a%p;
    }
    return res;
}
int main()
{
    int n;
    cin>>n;
    while(n--)
    {
        cin.tie(0);
        ios::sync_with_stdio(false);
        int a,b,p;
        long long res=1;
        cin>>a>>b>>p;
        res = qmi(a,b,p);
        cout<<res<<endl;
    }
    return 0;
}

快速幂之递归版 O(n∗logb)

#include<iostream>
using namespace std;
#define ull unsigned long long
ull quick_pow(ull a,ull b,ull p)
{
    if(b==0) return 1;
    a%=p;
    ull res=quick_pow(a,b>>1,p);
    if(b&1) return res*res%p*a%p;
    return res*res%p;
}
int main()
{
    int n;
    cin>>n;
    while(n--)
    {
        int a,b,p;
        cin.tie(0);
        ios::sync_with_stdio(false);
        cin>>a>>b>>p;
        cout<<quick_pow(a,b,p)<<endl;
    }
    return 0;
}

费马小定理 - 快速幂求逆元

给定 𝑛 组 𝑎𝑖,𝑝𝑖,其中 𝑝𝑖 是质数,求 𝑎𝑖 模 𝑝𝑖 的乘法逆元,若逆元不存在则输出 impossible

注意:请返回在 0∼𝑝−1 之间的逆元。

乘法逆元的定义

若整数𝑏,𝑚 互质,并且对于任意的整数 𝑎,如果满足𝑏|𝑎,则存在一个整数 𝑥,使得 𝑎/𝑏≡𝑎×𝑥(mod𝑚),则称 𝑥 为 𝑏 的模 𝑚 乘法逆元,记为 𝑏−1(mod𝑚)。

𝑏 存在乘法逆元的充要条件是 𝑏 与模数 𝑚 互质。当模数 𝑚 为质数时,bm−2𝑏𝑚−2 即为 𝑏 的乘法逆元。

输入格式

第一行包含整数 𝑛。

接下来 𝑛 行,每行包含一个数组 𝑎𝑖,𝑝𝑖,数据保证 𝑝𝑖 是质数。

输出格式

输出共 𝑛 行,每组数据输出一个结果,每个结果占一行。

若 𝑎𝑖 模 𝑝𝑖 的乘法逆元存在,则输出一个整数,表示逆元,否则输出 impossible

数据范围

1≤𝑛≤105,
1≤𝑎𝑖,𝑝𝑖≤2∗109

输入样例:
3
4 3
8 5
6 3
输出样例:
1
2
impossible

import java.io.*;
class Main{
    static BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
    public static int qmi(int a, int k, int p){
        long res = 1;
        while(k > 0){
            if((k & 1) != 0) res = res * a % p;
            a = (int)((long) a * a % p);
            k >>= 1;
        }
        return (int)res;
    }
    public static void main(String[] args) throws Exception{
        int n = Integer.valueOf(read.readLine());
        while(n -- > 0){
            String[] ss= read.readLine().split(" ");
            int a = Integer.valueOf(ss[0]);
            int m = Integer.valueOf(ss[1]);
            int qmi = qmi(a, m - 2, m);
            if(a % m != 0) System.out.println(qmi);
            else System.out.println("impossible");
        }
    }
}

当n为质数时,可以用快速幂求逆元:
a / b ≡ a * x (mod n)
两边同乘b可得 a ≡ a * b * x (mod n)
即 1 ≡ b * x (mod n)
同 b * x ≡ 1 (mod n)
由费马小定理可知,当n为质数时
b ^ (n - 1) ≡ 1 (mod n)
拆一个b出来可得 b * b ^ (n - 2) ≡ 1 (mod n)
故当n为质数时,b的乘法逆元 x = b ^ (n - 2)

当n不是质数时,可以用扩展欧几里得算法求逆元:
a有逆元的充要条件是a与p互质,所以gcd(a, p) = 1
假设a的逆元为x,那么有a * x ≡ 1 (mod p)
等价:ax + py = 1
exgcd(a, p, x, y)

快速幂求逆元

#include <iostream>
using namespace std;
typedef long long LL;

LL qmi(int a, int b, int p)
{
    LL res = 1;
    while(b){
        if(b & 1) res = res * a % p;
        a = (LL)a * a % p;
        b >>= 1;
    }
    return res;
}

int main()
{
    int n; cin >> n;
    while(n --){
        int a, p;
        cin >> a >> p;
        if(a % p == 0) puts("impossible");
        else cout << qmi(a, p - 2, p) << endl;
    }
    return 0;
}

扩展欧几里得算法求逆元

#include <iostream>
using namespace std;
typedef long long LL;
int n;

int exgcd(int a, int b, int &x, int &y)
{
    if (!b) {
        x = 1, y = 0;
        return a;
    }
    int d = exgcd(b, a % b, y, x);
    y -= a / b * x;
    return d;
}


int main()
{
    cin >> n;
    while (n --)
    {
        int a, p, x, y;
        // if (a < p) swap(a, p);
        cin >>  a >> p;
        int d = exgcd(a, p, x, y);
        if (d == 1) cout << ((LL)x + p) % p << endl;//保证x是正数
        else puts("impossible");

    }
    return 0;

}
  • 14
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值