zoukankan      html  css  js  c++  java
  • 【模板】逆元

    很基础的东西,但是不能马虎,有3种方法,下面一一列举。

    一.线性求逆元

    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<ctime>
    #include<queue>
    #include<algorithm>
    #include<cstring>
    using namespace std;
    #define duke(i,a,n) for(int i = a;i <= n;i++)
    #define lv(i,a,n) for(int i = a;i >= n;i--)
    #define clean(a) memset(a,0,sizeof(a))
    const int INF = 1 << 30;
    typedef long long ll;
    typedef double db;
    template <class T>
    void read(T &x)
    {
        char c;
        bool op = 0;
        while(c = getchar(), c < '0' || c > '9')
            if(c == '-') op = 1;
        x = c - '0';
        while(c = getchar(), c >= '0' && c <= '9')
            x = x * 10 + c - '0';
        if(op) x = -x;
    }
    template <class T>
    void write(T x)
    {
        if(x < 0) putchar('-'), x = -x;
        if(x >= 10) write(x / 10);
        putchar('0' + x % 10);
    }
    int n;
    ll p,inv[3000005];
    void work()
    {
        inv[1] = 1;
        duke(i,2,n)
        {
            inv[i] = (p - p / i) * inv[p % i] % p;
        }
        duke(i,1,n)
        {
            printf("%lld
    ",inv[i]);
        }
    }
    int main()
    {
        read(n);read(p);
        work();
        return 0;
    }

    二.费马小定理求逆元

    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<ctime>
    #include<queue>
    #include<algorithm>
    #include<cstring>
    using namespace std;
    #define duke(i,a,n) for(int i = a;i <= n;i++)
    #define lv(i,a,n) for(int i = a;i >= n;i--)
    #define clean(a) memset(a,0,sizeof(a))
    const int INF = 1 << 30;
    typedef long long ll;
    typedef double db;
    template <class T>
    void read(T &x)
    {
        char c;
        bool op = 0;
        while(c = getchar(), c < '0' || c > '9')
            if(c == '-') op = 1;
        x = c - '0';
        while(c = getchar(), c >= '0' && c <= '9')
            x = x * 10 + c - '0';
        if(op) x = -x;
    }
    template <class T>
    void write(T x)
    {
        if(x < 0) putchar('-'), x = -x;
        if(x >= 10) write(x / 10);
        putchar('0' + x % 10);
    }
    ll n,p;
    ll qpow(ll x,ll y)
    {
        ll tot = 1;
        while(y)
        {
            if((y & 1) != 0)
            {
                tot *= x;
            }
            x *= x;
            x %= p;
            tot %= p;
            y >>= 1;
        }
        return tot;
    }
    int main()
    {
        read(n);read(p);
        printf("%lld
    ",qpow(n,p - 2));
        return 0;
    }

    三.exgcd求逆元

    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<ctime>
    #include<queue>
    #include<algorithm>
    #include<cstring>
    using namespace std;
    #define duke(i,a,n) for(int i = a;i <= n;i++)
    #define lv(i,a,n) for(int i = a;i >= n;i--)
    #define clean(a) memset(a,0,sizeof(a))
    const int INF = 1 << 30;
    typedef long long ll;
    typedef double db;
    template <class T>
    void read(T &x)
    {
        char c;
        bool op = 0;
        while(c = getchar(), c < '0' || c > '9')
            if(c == '-') op = 1;
        x = c - '0';
        while(c = getchar(), c >= '0' && c <= '9')
            x = x * 10 + c - '0';
        if(op) x = -x;
    }
    template <class T>
    void write(T x)
    {
        if(x < 0) putchar('-'), x = -x;
        if(x >= 10) write(x / 10);
        putchar('0' + x % 10);
    }
    ll exgcd(ll a,ll b,ll &x,ll &y)
    {
        if(a == 1 && b == 0)
        {
            x = 1;
            y = 0;
            return 1;
        }
        ll t = exgcd(b,a % b,y,x);
        y -= a / b * x;
        return t;
    }
    int main()
    {
        ll n,p,x,y;
        read(n);read(p);
        ll t = exgcd(n,p,x,y);
        printf("%lld
    ",(x % p + p) % p);
        return 0;
    }
  • 相关阅读:
    apply 无循环拼接数组
    匿名函数自调的三种方法
    移动元素时,translate要比margin好
    Tesseract库原理分析
    [转]基于OCR的图片字符识别可行性研究
    [转]C&C++图形图像处理开源库
    [转]OCR识别的开源分析
    [转]众多OCR软件
    [转]常用OCR软件介绍
    [转]浅谈OCR之Tesseract
  • 原文地址:https://www.cnblogs.com/DukeLv/p/9748286.html
Copyright © 2011-2022 走看看