luogu题解 P3811 【【模板】乘法逆元】
upd--7.6 原线性求逆元代码有另一种更优写法
题目链接:
概念:
一想到JXOI 2018考场上忘记逆元怎么打就觉得好笑,白白让T1落得如此下场.
推荐阅读:https://www.luogu.org/blog/zjp-shadow/cheng-fa-ni-yuan
我们可能经常遇到这样的情况(如JXOI2018 T1)
计算一个类似\(a/b\)%\(p\)的式子,根据同余性质我们是不能$a \(%p\) /b \(%p\) $这样计算的
但如果在普通算式中,显然\(a/b\)=\(a*b^-1\)。
在\(\pmod p\)中的意义下,我们不妨也构造一个\(b^-1\),使得\(b*b^{-1} \equiv 1 \pmod{p}\);
那么\(a/b \pmod p \equiv a*b^{-1} \pmod p\),对于一开始的问题,我们可以放心地将\(a\)%\(p\),然后乘上\(b\)在\(\pmod p\)意义下的逆元,太方便了
这就引出了逆元的定义:
若\(b*x \equiv 1 \pmod{p}\)且\(b,p\)互质,则\(x\)为\(b\)在\(\pmod p\)意义下的逆元 ,记作\(b^-1\)
思路:
算逆元一般有三种算法
拓展欧几里得
根据定义,若要求\(b\)在\(\pmod p\)意义下的逆元,就是求\(b*x \equiv 1 \pmod{p}\)中的x,转化成线性同余方程\(b*x+p*y=1\),接着就用拓欧跑一遍就好
费马小定理
若p是质数,则对于任意正整数b,有\(b^{p} \equiv b \pmod{p}\)
所以\(b^{p-1} \equiv 1 \pmod{p}\) 把b继续拆得到
\(b*b^{p-2} \equiv 1 \pmod{p}\)
对照上面逆元定义,我们只用求\(b^{p-2} \pmod p\)
线性递推
设\(p=k*i+r\) 所以\(k=\left \lfloor \frac{p}{i} \right \rfloor,r=p \mod i\)
所以\(k*i+r \equiv 0 \pmod p\) 同时乘以\(i^{-1},r^{-1}\)
\(k*r^{-1}+i^{-1} \equiv 0 \pmod p\)移项
\(i^{-1} \equiv -k*r^{-1} \pmod p\)
\(i^{-1} \equiv -\left \lfloor \frac{p}{i} \right \rfloor*{(p \mod i)}^{-1}\pmod p\)
我们把所有的逆元存在inv[]数组里,那么
inv[i]=-((p/i)*inv[p%i]%p);
while(inv[i]<0)inv[i]+=p;
或者更优的写法
inv[i]=(ll)(p-(p/i))*inv[p%i]%p;
当然inv[1]=1;
代码:
- 拓欧+费马小定理
include
include
include
include
include
define LL long long
using namespace std;
LL mod(LL a,LL b)
{
if(a<b)return a;
if(a==b)return 0;
else return a-a/b*b;
}
LL pow_mod(LL a, LL b, LL p){//a的b次方求余p
LL ret = 1;
while(b){
if(b & 1) ret = mod((ret * a) ,p);
a =mod((a * a) ,p);
b >>= 1;
}
return ret;
}
LL Fermat(LL a, LL p){//费马求a关于b的逆元
return pow_mod(a, p-2, p);
}
void ex_gcd(LL a, LL b, LL &x, LL &y, LL &d){
if (!b) {d = a, x = 1, y = 0;}
else{
ex_gcd(b, mod(a,b), y, x, d);
y -= x * (a / b);
}
}
LL inv(LL t, LL p){//如果不存在,返回-1
LL d, x, y;
ex_gcd(t, p, x, y, d);
return d == 1 ? (mod(x,p) + p) % p : -1;
}
int main()
{
int op;
LL a,p;
cin>>a>>p;
for(int i=1;i<=a;i++)cout<<inv(i,p)<<endl;
return 0;
}
```
- 线性递推
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cctype>
#include <cmath>
#deinfe ll long long
using namespace std;
const int maxn=3000005;
int n,p;
ll inv[maxn];
template <class T>inline void read(T &x){
x=0;int ne=0;char c;
while(!isdigit(c=getchar()))ne=c=='-';
x=c-48;
while(isdigit(c=getchar()))x=(x<<3)+(x<<1)+c-48;
x=ne?-x:x;
return ;
}
inline void make_inv(){
printf("1\n"); //inv[1];
for(register int i=2;i<=n;i++){
inv[i]=-((p/i)*inv[p%i]%p);
while(inv[i]<0)inv[i]+=p;
printf("%lld\n",inv[i]);
}
return ;
}
int main()
{
read(n),read(p);
inv[1]=1;
make_inv();
return 0;
}
luogu题解 P3811 【【模板】乘法逆元】的更多相关文章
- 【洛谷P3811】[模板]乘法逆元
乘法逆元 题目链接 求逆元的三种方式: 1.扩欧 i*x≡1 (mod p) 可以化为:x*i+y*p=1 exgcd求x即可 inline void exgcd(int a,int b,int &a ...
- 【ZOJ 3609】Modular Inverse 最小乘法逆元
The modular modular multiplicative inverse of an integer a modulo m is an integer x such that a-1≡x ...
- 题解 P3811 【【模板】乘法逆元】
P3811 [模板]乘法逆元 一个刚学数论的萌新,总结了一下这题的大部分做法 //一.费马小定理+快速幂 O(nlogn) 64分 #include<cstdio> using names ...
- 逆元-P3811 【模板】乘法逆元-洛谷luogu
https://www.cnblogs.com/zjp-shadow/p/7773566.html -------------------------------------------------- ...
- luogu P3811 【模板】乘法逆元
题目背景 这是一道模板题 题目描述 给定n,p求1~n中所有整数在模p意义下的乘法逆元. 输入输出格式 输入格式: 一行n,p 输出格式: n行,第i行表示i在模p意义下的逆元. 输入输出样例 输入样 ...
- P3811 【模板】乘法逆元
P3811 [模板]乘法逆元 线性递推逆元模板 #include<iostream> #include<cstdio> #include<cstring> #def ...
- [洛谷P3811]【模板】乘法逆元
P3811 [模板]乘法逆元 题意 求1-n所有整数在模p意义下的逆元. 分析 逆元 如果x满足\(ax=1(\%p)\)(其中a p是给定的数)那么称\(x\)是在\(%p\)意义下\(a\)的逆元 ...
- 模板【洛谷P3811】 【模板】乘法逆元
P3811 [模板]乘法逆元 给定n,p求1~n中所有整数在模p意义下的乘法逆元. T两个点的费马小定理求法: code: #include <iostream> #include < ...
- 洛谷 P3811 【模板】乘法逆元
P3811 [模板]乘法逆元 题目背景 这是一道模板题 题目描述 给定n,p求1~n中所有整数在模p意义下的乘法逆元. 输入输出格式 输入格式: 一行n,p 输出格式: n行,第i行表示i在模p意义下 ...
随机推荐
- 黑马vue---17、vue中通过属性绑定绑定style行内样式
黑马vue---17.vue中通过属性绑定绑定style行内样式 一.总结 一句话总结: 如果属性名中带有短线必须加引号,比如: h1StyleObj: { color: 'red', 'font-s ...
- Arcgis python输出当前窗口
import arcpy mxd = arcpy.mapping.MapDocument("CURRENT") mxdfile=arcpy.GetParameterAsText(0 ...
- idea svn 主干分支切换
使用快捷键ctrl+t或者点击工具栏中的 vcs update 出现如下窗口:点击复选框,在框2中进行选择你要切换的分支的地址,点击OK即可 : 查看是否切换成功可以再version control ...
- LC 738. Monotone Increasing Digits
Given a non-negative integer N, find the largest number that is less than or equal to N with monoton ...
- SEO中常用的301永久重定向代码大全
301是永久重定向的意思,表示请求的网页已永久移动到新位置,服务器返回此响应(对 GET 或 HEAD 请求的响应)时,会自动将请求者转到新位置.其实301重定向在SEO中被广泛应用,也是被广泛认为比 ...
- spring cloud之Eureka不能注销docker部署的实例
1 起因 事件的起因是这样的,我们在微服务改造的过程中,选择将服务注册到eureka中,开发的时候还好,使用场景是这样的: 在idea中启动服务,成功注册到eureka,关闭服务,eureka成功注销 ...
- 一首好听的摇滚歌曲(Ever Dream),以及优美的译作
送上一首好听的摇滚歌曲,以及优美的译作.祝大家新年快乐.happy new year! [ti:Ever Dream][ar:Nightwish][al:Century Child][by:吖光] ...
- 小D课堂 - 新版本微服务springcloud+Docker教程_3-06 服务注册和发现之Eureka Client搭建商品服务实战
笔记 6.服务注册和发现之Eureka Client搭建商品服务实战 简介:搭建用商品服务,并将服务注册到注册中心 1.创建一个SpirngBoot应用,增加服务注册和发现依赖 2.模 ...
- [Mac]安装pyspider的大坑
1.切记这是一个大坑. 2.我在用mac电脑安装pyspider的时候,原以为pip install pyspider 就万事大吉,合家欢乐了,but the question 比较多. 第一个问题: ...
- mysql查看系统参数
show global variables like ‘innodb_buffer_pool_size’: 查看buffer相关参数 show global variables like 'buffe ...