upd--7.6 原线性求逆元代码有另一种更优写法

  • 题目链接:

    https://www.luogu.org/problemnew/show/P3811

  • 概念:

    一想到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 【【模板】乘法逆元】的更多相关文章

  1. 【洛谷P3811】[模板]乘法逆元

    乘法逆元 题目链接 求逆元的三种方式: 1.扩欧 i*x≡1 (mod p) 可以化为:x*i+y*p=1 exgcd求x即可 inline void exgcd(int a,int b,int &a ...

  2. 【ZOJ 3609】Modular Inverse 最小乘法逆元

    The modular modular multiplicative inverse of an integer a modulo m is an integer x such that a-1≡x  ...

  3. 题解 P3811 【【模板】乘法逆元】

    P3811 [模板]乘法逆元 一个刚学数论的萌新,总结了一下这题的大部分做法 //一.费马小定理+快速幂 O(nlogn) 64分 #include<cstdio> using names ...

  4. 逆元-P3811 【模板】乘法逆元-洛谷luogu

    https://www.cnblogs.com/zjp-shadow/p/7773566.html -------------------------------------------------- ...

  5. luogu P3811 【模板】乘法逆元

    题目背景 这是一道模板题 题目描述 给定n,p求1~n中所有整数在模p意义下的乘法逆元. 输入输出格式 输入格式: 一行n,p 输出格式: n行,第i行表示i在模p意义下的逆元. 输入输出样例 输入样 ...

  6. P3811 【模板】乘法逆元

    P3811 [模板]乘法逆元 线性递推逆元模板 #include<iostream> #include<cstdio> #include<cstring> #def ...

  7. [洛谷P3811]【模板】乘法逆元

    P3811 [模板]乘法逆元 题意 求1-n所有整数在模p意义下的逆元. 分析 逆元 如果x满足\(ax=1(\%p)\)(其中a p是给定的数)那么称\(x\)是在\(%p\)意义下\(a\)的逆元 ...

  8. 模板【洛谷P3811】 【模板】乘法逆元

    P3811 [模板]乘法逆元 给定n,p求1~n中所有整数在模p意义下的乘法逆元. T两个点的费马小定理求法: code: #include <iostream> #include < ...

  9. 洛谷 P3811 【模板】乘法逆元

    P3811 [模板]乘法逆元 题目背景 这是一道模板题 题目描述 给定n,p求1~n中所有整数在模p意义下的乘法逆元. 输入输出格式 输入格式: 一行n,p 输出格式: n行,第i行表示i在模p意义下 ...

随机推荐

  1. linux向文件中添加数据,数据被追加到了上一行的行尾

    当我们在windows上新建了文件,并且要上传到linux上时,一定要在文件后加上空格,否则就会出现标题上讲的,数据被追加到了上一行的行尾,而不是新起一行,但是如果是在linux上新建的文件,则没有这 ...

  2. 订阅发布模式eventEmiter

    // 订阅发布模式 class EventEmitter { constructor() { this._events = {}; } on(name, callback) { if (this._e ...

  3. Winform运行外部控制台程序,并在程序结束后执行其他动作

    ProcessStartInfo psi = new ProcessStartInfo(); psi.FileName = @"程序名"; psi.Arguments = @&qu ...

  4. 配置 admin 页面

    创建 blog 的管理后台 首先是 blog 这个 App,其中定义了 3个 Model,分别是 Category.Post 和 Tag.先创建 admin 页面,其代码需要写到 blog/admin ...

  5. UNITY3D 添加预制的方法

    预制文件(Prefabs)的做法 我这里需要的图片 在hierarchy 视图下新建一个2D object->sprite 然后设置这个sprite的背景为需要的图片 新建一个prefabs目录 ...

  6. Linux学习—mysql安装配置及远程连接

    安装前准备 1.检查是否已经安装过mysql 执行命令 [root@localhost /]# rpm -qa | grep mysql  从执行结果,可以看出我们已经安装了mysql-libs-5. ...

  7. docker 导出多个镜像合并成一个tar

    导出单个镜像 docker save [images] > [name.tar] 倒出多个镜像合并成一个tar包 docker save [images] [images] > [name ...

  8. python-Web-django-路由保护

    from django.shortcuts import redirect,HttpResponse from app01.models import * import re def ddff(mod ...

  9. C学习笔记-gdb

    gdb即GNU debugger,用来调试程序 gdb使用前提 要使用gdb,则需要在编译源代码时候使用-g参数 gcc -g –o test test.c 启动gdb gdb 程序名 [corefi ...

  10. mysql数据库之索引与慢查询优化

    索引与慢查询优化 知识回顾:数据都是存在硬盘上的,那查询数据不可避免的需要进行IO操作 索引在MySQL中也叫做“键”,是存储引擎用于快速找到记录的一种数据结构. primary key unique ...