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---18、v-for指令的四种使用方式
黑马vue---18.v-for指令的四种使用方式 一.总结 一句话总结: (item, i) in list:什么in什么的形式,前面是各种参数 1.v-for循环普通数组? <p v-for ...
- python 牛顿迭代法
使用牛顿迭代法求方程 在x附近的一个实根. 赋值X,即迭代初值:用初值x代入方程中计算此时的f(x)=(a * x * x * x + b * x * x + c * x + d)和f’(x)=(3 ...
- instanceof 实现
A instanceof B // 实现 instanceof function instance(a, b) { const proto = a.__proto__; // eslint-disab ...
- go命令行参数
go命令行参数例子: package main import( "fmt" "os" "strings" ) func main(){ wh ...
- ART模式
ART模式是Android RunTime的简称,自动对程序进行代码预读取编译,让程序直接编译成机器语言,免去了Dalvik模式要时时转换代码,实现高效率.省电.占用更低的系统内存.手机运行流畅.但凡 ...
- *Scala API - 并发编程
- Spring Boot启动的报错 Stopping service [Tomcat]
我遇到的问题是项目中使用java_websocket的WebSocketClient,由于性能要求,需要再Controller直接继承WebSocketClient, 在项目启动过程中调试进入spri ...
- 自动化部署 jenkins 插件简介
一.什么是持续集成? (1)Continuous integration(CI) 持续集成是一种软件开发实践,即团队开发成员经常集成他们的工作,通常每个成员至少集成一次,也就意味着每天可能会发生多次集 ...
- 配置lumen的log为daily模式
1.首先添加服务提供者类LogServiceProvider <?php namespace App\Providers; use Illuminate\Support\ServiceProvi ...
- three.js 之 透明物体不能正常显示
这几天遇到一个需求,类似这个案例:http://www.hightopo.com/demo/FaultDetection/ 就是这个透明渐变呼吸光柱,看到之后就想着写个shader材质实现透明度渐变, ...