逆元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 ...
随机推荐
- CocoaPods 的安装和使用介绍
CocoaPods 的安装和使用介绍 安装 安装方式异常简单 , Mac 下都自带 ruby,使用 ruby 的 gem 命令即可下载安装: 1 2 $ sudo gem install cocoap ...
- Hadoop生态系统学习路线
主要介绍Hadoop家族产品,经常使用的项目包含Hadoop, Hive, Pig, HBase, Sqoop, Mahout, Zookeeper, Avro, Ambari, Chukwa.新添加 ...
- TextWatcher基本用法
editText.addTextChangedListener(new TextWatcher() { /** * 内容改变前调用 * 原有的文本s中,从start开始的count个字符将会被一个新的 ...
- Protected vs protected internal (Again) in c#
http://stackoverflow.com/questions/22940317/protected-vs-protected-internal-again-in-c-sharp protect ...
- POJ 1663:Number Steps
Number Steps Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 13758 Accepted: 7430 Des ...
- 软-RAID 5组建
图文版raid5组建之软RAID [复制链接] 发表于 2007-3-6 09:19 | 来自 51CTO网页 [只看他] 楼主 硬件raid5的组建和使用,基本上说完 ...
- la3713
2-sat...求解2-sat方案直接每个变量枚举就行了,lrj的代码很靠谱... #include<bits/stdc++.h> using namespace std; ; struc ...
- 02_jni_hello_c函数介绍
介绍NDK平台都有哪些工具.通过NDK这套工具做安卓下的JNI开发. 可能有一些需求更适合通过C去做,有一些功能要通过C去实现.一个安卓程序,它本身还是一个Java应用.有一些功能/方法不通过Java ...
- js的时间展示
<script type="text/javascript">$(function() { //方法调用 showtime(); //默认加载首页 ...
- 03-vue实例生命周期和vue-resource
vue实例的生命周期 什么是生命周期:从Vue实例创建.运行.到销毁期间,总是伴随着各种各样的事件,这些事件,统称为生命周期! 生命周期钩子:就是生命周期事件的别名而已: 生命周期钩子 = 生命周期函 ...