算法基础之快速幂求逆元

快速幂求逆元

  • 核心思想: 逆元:在这里插入图片描述

    • 逆元 == ap-2 mod p

    •   #include<iostream>
        #include<algorithm>
        
        using namespace std;
        typedef long long LL;
        
        LL pmi(int a,int b,int c)
        {
             
            LL res = 1;
            while(b)
            {
             
                if(b & 1) res = res * a %c;
                b >>= 1;
                a = (LL) a * a %c;
            }
            
            return res;
        }
        
        int main()
        {
             
            int n;
            cin>>n;
            while(n--)
            {
             
                int a,b;
                cin>>a>>b;
                if(a % b) cout<<pmi(a,b-2,b)<<endl;  //若a%b ==0 说明不存在逆元
                else puts("impossible");
            }
        }
      

相关推荐

  1. 快速-C语言

    2023-12-21 23:52:02       41 阅读
  2. 算法】数论---快速

    2023-12-21 23:52:02       61 阅读
  3. 快速算法详解

    2023-12-21 23:52:02       41 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-21 23:52:02       91 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-21 23:52:02       97 阅读
  3. 在Django里面运行非项目文件

    2023-12-21 23:52:02       78 阅读
  4. Python语言-面向对象

    2023-12-21 23:52:02       88 阅读

热门阅读

  1. hive(2)

    2023-12-21 23:52:02       43 阅读
  2. 自定义ORM(mybatis)源码(二)-解析mapper.xml

    2023-12-21 23:52:02       57 阅读
  3. Linux多线程

    2023-12-21 23:52:02       75 阅读
  4. 指针的进阶

    2023-12-21 23:52:02       46 阅读