逆元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 ...
随机推荐
- JXLS-----JXLS导出Excel
- 解析java中volatilekeyword
在java多线程编程中常常volatile,有时候这个keyword和synchronized 或者lock常常有人混淆.详细解析例如以下: 在多线程的环境中会存在成员变量可见性问题: java的每一 ...
- linux下误删数据文件恢复
linux下文件被删除能够用非常多工具进行恢复.比如undelete(适合ext2,ext3).giis(不能恢复安装giis之前的文件).ext3grep(仅限ext3).R-linux(支持ext ...
- Longest Increasing Subsequence HDU - 6284
/* 首先预处理好f g数组 fi :以a[i]为结尾的 最长上升子序列的长度 gi :以a[i]为开始的 最长上升子序列的长度 mxx : 最长上升子序列的长度 线段树优化 nlogn (不包含a[ ...
- 安卓dex 文件结构简要说明
#ifndef _DEX_FILE_HELPER_ #define _DEX_FILE_HELPER_ //此文件仅仅是起帮助作用,帮助不太了解DexFile结构的了解一下DexFile相关结构,想更 ...
- Netlink通信机制【转】
本文转载自:http://www.cnblogs.com/wenqiang/p/6306727.html 一.什么是Netlink通信机制 Netlink套接字是用以实现用户进程与内核进程通信的一种 ...
- YTU 2706: 编写一个函数求最大的n值
2706: 编写一个函数求最大的n 值. 时间限制: 1 Sec 内存限制: 128 MB 提交: 341 解决: 132 题目描述 编写一个函数求满足以下条件的最大的n.:12+22+32+-+ ...
- 转 一个SMD 0805的LED的电流,电压,亮度关系表
Vf就是led的导通电压. 一个SMD 0805的LED的电流,电压,亮度关系表Vf If(算) 亮度1.74v 0.46mA 做指示灯不刺眼刚 ...
- bzoj 1601 灌水
题目大意: 决定把水灌到n块农田,农田被数字1到n标记 把一块土地进行灌水有两种方法,从其他农田饮水,或者这块土地建造水库 建造一个水库需要花费wi,连接两块土地需要花费Pij. 计算所需的最少代价 ...
- [luogu_U15116]珈百璃堕落的开始
https://www.zybuluo.com/ysner/note/1239458 题面 给定\(n\)个二元组\((x,y)\),问有多少种方案,使得选出其中几个后,\(\sum x=\sum y ...