逆元Inv(模板+应用)
逆元:
如果满足公式,则有a 是 b的逆元同时b也是a的逆元。
逆元的应用:
设c为b在对m取余的意义下的逆元;
在求解公式 (a / b) % m的时候,如果b可能会非常的大,所以会出现爆精度的问题,这个时候就需要将除法转换成乘法来做,即:
(a / b ) % m = (a * c)%m。
逆元的求法:
一、扩展欧几里得求逆元
复杂度:O(logn)(实际就是斐波那契数列)
将公式(b、p已知) a∗b≡1(mod p) 转换为 a∗b+k∗p=1 则有a为b对p取余意义下的逆元,且只有当a与p互质是逆元才存在。
注意:只要存在逆元就可以求,适用于逆元个数不多,但是mod很大的时候。
附一个百度百科的例子加深一下理解:
代码:
/*
Time:2018/8/31
Writer:Sykai
Function:利用扩展欧几里得求逆元
*/
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <set>
#include <queue>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = 1e6 + ;
const int MOD = 1e9 + ;
typedef long long ll;
typedef pair<int,int> P; //公式a∗b+k∗p=1中a即是a,p即是b
ll exgcd(ll a,ll b,ll& x,ll& y)
{
if(b == )
{
x = ;
y = ;
return a;
}
ll res = exgcd(b, a%b, y, x);
y -= a/b*x;
return res;
}
//公式a∗b+k∗p=1中a即是a,p即是mod
ll getInv(int a,int mod)//求a在mod下的逆元。如果不存在就返回-1
{
ll x,y;
ll res = exgcd(a,mod,x,y);
return res = ? (x%mod + mod)%mod:-;//return res = 1?(x + mod)%mod : -1;
} int main()
{
ll a = ;
printf("%lld\n",getInv(a,MOD));
return ;
}
二、费马小定理求逆元
复杂度:O(logn)
费马小定理: 假如p是质数,且gcd(a,p)=1,那么 a^(p-1)≡1(mod p)。
a*a^(p-2) ≡ 1 (mod p),则有a^(p-2)是a的对p取余时候的逆元
注意:当p是素数的时候一般选用费马小定理来求逆元。
代码:
/*
Time:2018/8/31
Writer:Sykai
Function:利用费马小定理求逆元
*/
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <set>
#include <queue>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = 1e6 + ;
const int MOD = 1e9 + ;
typedef long long ll;
typedef pair<int,int> P; ll qpow(ll a,ll b)//求a*a^(p-2) ≡ 1 (mod p)中a^(p-2)
{
ll res = ;
while(b)
{
if(b&)
res = res * a % MOD;
a = a * a % MOD;
b >>= ;
}
return res;
} ll getInv(ll a,ll mod)
{
return qpow(a,mod-);
} int main()
{
int a = ;
printf("%lld\n",getInv(a,MOD));
return ;
}
三、递推求逆元
复杂度:O(n)
注意:
1、mod需要是质数,求得是1~N关于mod的逆元。
2、适用于mod不是太大,且被多次调用。
3、程序开始前需要预处理打表。
代码:
/*
Time:2018/8/31
Writer:Sykai
Function:线性求逆元
*/
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <set>
#include <queue>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = 1e6 + ;
const int MOD = 1e9 + ;
typedef long long ll;
typedef pair<int,int> P;
ll inv[maxn];//数组的大小需要根据实际情况来调整 void getInv()
{
inv[] = ;
for(int i = ; i < maxn; i++)
inv[i] = (MOD-MOD/i)*inv[MOD%i]%MOD;
} int main()
{
getInv();
printf("%lld\n",inv[]);
return ;
}
四、递归求逆元
复杂度:O(logn)
注意:mod需要是素数(中国剩余定理中不太好用)
/*
Time:2018/8/31
Writer:Sykai
Function:递归求逆元
*/
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <set>
#include <queue>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = 1e6 + ;
const int MOD = 1e9 + ;
typedef long long ll;
typedef pair<int,int> P;
ll inv[maxn];//数组的大小需要根据实际情况来调整 ll getInv(ll x)
{
if(x == ) return ;
return (MOD-MOD/x)*getInv(MOD%x)%MOD;
} int main()
{
printf("%lld\n",getInv());
return ;
}
五、求阶乘的逆元
代码:(不确定怎么用)
/*
Time:2018/8/31
Writer:Sykai
Function:递归求逆元
*/
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <set>
#include <queue>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = 1e6 + ;
const int MOD = 1e9 + ;
typedef long long ll;
typedef pair<int,int> P;
ll inv[maxn];//数组的大小需要根据实际情况来调整
ll fac[maxn+];//阶乘数组
ll qpow(ll a,ll b)
{
ll res = ;
while(b)
{
if(b&)
res = res * a % MOD;
a = a * a % MOD;
b >>= ;
}
return res;
} int main()
{
inv[maxn] = qpow(fac[maxn],MOD-);
for(ll i = maxn-; i>=; i--)
{
inv[i] = (inv[i+]*(i+))%MOD;
}
return ;
}
参考博客:
https://blog.csdn.net/baidu_35643793/article/details/75268911
https://blog.csdn.net/xiaoming_p/article/details/79644386
逆元Inv(模板+应用)的更多相关文章
- 生成树计数 lighting 最终决定用这个模板! (有逆元的模板)
#include <cstdio> #include <cstring> #include <algorithm> #include <cmath> # ...
- 逆元(inv)
当求解公式:(a/b)%m 时,因b可能会过大,会出现爆精度的情况,所以需变除法为乘法: 设c是b的逆元,则有b*c≡1(mod m): 则(a/b)%m = (a/b)*1%m = (a/b)*b* ...
- 生成树计数模板 spoj 104 (不用逆元的模板)
/* 这种题,没理解,只是记一记如何做而已: 生成树的计数--Matrix-Tree定理 题目:SPOJ104(Highways) 题目大意: *一个有n座城市的组成国家,城市1至n编号,其中一些城市 ...
- 牛客多校第六场 C Generation I 组合数学 阶乘逆元模板
链接:https://www.nowcoder.com/acm/contest/144/C来源:牛客网 Oak is given N empty and non-repeatable sets whi ...
- 逆元知识普及(扫盲篇) —— from Judge
watch out 本文是博主的 csdn 上搬过来的,格式有点崩,看不下去的可以去 博主的 csdn上看(上面 格式会好很多,并且有些公式也用 $\LaTeX$ update 上去了) 最近有点颓 ...
- NOIP经典基础模板总结
date: 20180820 spj: 距离NOIP还有81天 目录 STL模板: priority_queue 的用法:重载<,struct cmpqueue 的用法 stack 的用法vec ...
- 【牛客小白月赛6】F 发电 - 树状数组&快速幂&逆元
题目地址:https://www.nowcoder.com/acm/contest/136/F 树状数组.快速幂.逆元的模板运用: #include<iostream> #include& ...
- poj 1006中国剩余定理模板
中国剩余定理(CRT)的表述如下 设正整数两两互素,则同余方程组 有整数解.并且在模下的解是唯一的,解为 其中,而为模的逆元. 模板: int crt(int a[],int m[],int n) { ...
- [OI笔记]三种逆元的求法
其实这篇博客只是搬运一下我之前(大概是NOIP那会)写在word里的笔记- 下面直接复制原话,题目是洛谷上求逆元的模板题:https://www.luogu.org/problemnew/show/P ...
随机推荐
- com.opensymphony.xwork2.inject.DependencyException: com.opensymphony.xwork2.inject.ContainerImpl$Mis
为什么会出现这个错误? 我使用了Maven配置Struts,所以改动Struts配置文件的默认路径,例如以下: <!-- 配置struts --> <filter> <f ...
- luogu1641 [SDOI2010]生成字符串
题目大意 把$n$个$1$和$m$个$0$组成字符串,在任意的前$k$个字符中,$1$的个数不能少于$0$的个数.求这样的字符串的个数.$1\leq m\leq n\leq 1000000$. 原始模 ...
- CSS自适应宽度的高级应用,一般人不会告诉你。
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAxgAAACdCAIAAAC4iknFAAAgAElEQVR4nO2d3VsTV7uH51/wqhKotn
- go语言笔记——go是有虚拟机runtime的,不然谁来做GC呢,总不会让用户自己来new和delete进行内存管理吧,还有反射!Go 的 runtime 嵌入到了每一个可执行文件当中
2.7 Go 运行时(runtime) 尽管 Go 编译器产生的是本地可执行代码,这些代码仍旧运行在 Go 的 runtime(这部分的代码可以在 runtime 包中找到)当中.这个 runtime ...
- [Codeforces Round49F] Session in BSU
[题目链接] http://codeforces.com/contest/1027/problem/F [算法] 二分图匹配 [代码] #include<bits/stdc++.h> #p ...
- linux下.a/.so/.la目标库区别
在linux平台上编译时,常会遇到目标库的疑问,有静态库也有动态库,单个理解都不太难,但是对复杂的工程而言,一旦混合到一起去,对整个工程的理解和调用,将会造成很大困扰,本文就汇总这几种常见编译结果文件 ...
- MSP430:管脚的第二功能选择
之前在使用PWM,AD时候用到过第二功能,不过都是copy没有注意过PXSEL究竟怎么设置,今天在设置晶振管脚时候遇到了麻烦,细致看了一下其实很简单,在SPEC的最后详细讲了每个管脚如何设置为其他功能 ...
- Linux文件属性相关补充及软硬连接
第1章 文件属性相关 1.1 文件的属性 1.1.1 扩展名 windows 通过扩展名区分不同的类型的文件 linux 扩展名是给人类看的 方便我们区分不同类型文件 .conf 配置文件 ...
- Atlantis(坐标离散化)
http://poj.org/problem?id=1151 题意:给出矩形的左上角坐标和右下角坐标(坐标的y轴是向下的),求出矩形面积的并.. 今天好困啊..迷迷糊糊的听会神给讲了讲,敲完之后调试了 ...
- 利用AXIS2传递JSON数据
Axis2是目前比较流行的WebService引擎.WebService被应用在很多不同的场景.例如,可以使用WebService来发布服务端 Java类的方法,以便使用不同的客户端进行调用.这样可以 ...