求逆元的板子
Posted caibingxu
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了求逆元的板子相关的知识,希望对你有一定的参考价值。
#include <bits/stdc++.h>
#include<unordered_map>
using namespace std;
typedef double dou;
typedef long long ll;
typedef pair<int, int> pii;
typedef map<int, int> mii;
#define pai acos(-1.0)
#define M 400050
#define inf 0x3f3f3f3f
#define Lnf 1LL<<60
#define mod 1000000007
#define IN inline
#define W(a) while(a)
#define lowbit(a) a&(-a)
#define left k<<1
#define right k<<1|1
#define lson L, mid, left
#define rson mid + 1, R, right
#define ms(a,b) memset(a,b,sizeof(a))
#define Abs(a) (a ^ (a >> 31)) - (a >> 31)
#define random(a,b) (rand()%(b+1-a)+a)
#define false_stdio ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
void exgcd(ll a, ll b, ll &x, ll &y) {//扩展欧几里得
if (b == 0) {
x = 1;
y = 0;
return;
}
exgcd(b, a%b, y, x);
y -= a / b * x;
}
ll POW10(ll base, ll sup) {//十进制快速幂
ll sum = 1LL;
W(sup) {
int tmp = sup % 10;
for (int i = 1; i <= tmp; i++)sum = sum * base%mod;
ll A = base * base%mod;
ll B = A * A%mod;
ll C = B * B%mod;
base = A * C%mod;
sup /= 10;
}
return sum;
}
ll POW2(ll base, ll sup) {//二进制快速幂
ll sum = 1;
W(sup) {
if (sup & 1)sum = sum * base%mod;
sup >>= 1;
base = base * base%mod;
}
return sum;
}
ll solve(ll a,ll p) {//a是要求的数,p是模数
ll x, y;
//exgcd(a, p, x, y); //扩展欧几里得求逆元,x为逆元
//费马小定理求逆元
//x = POW10(a, mod - 2);//十进制快速幂
//x = POW2(a, mod - 2);//二进制快速幂
x = (x + mod) % mod;
return x;
}
int main() {
false_stdio;
return 0;
}
以上是关于求逆元的板子的主要内容,如果未能解决你的问题,请参考以下文章