博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
P3811 【模板】乘法逆元
阅读量:4671 次
发布时间:2019-06-09

本文共 1472 字,大约阅读时间需要 4 分钟。

回首望月一波之前\(logN\)求逆元的扩展欧几里得算法

(求解\(a * x \equiv 1(Mod\ p)\) \(\Leftrightarrow\) 求解 \(a * x + p * y = 1\)

LL exgcd(LL a,LL b,LL &x,LL &y){//注意引用    if(!b){x = 1,y = 0;return a;}    int d = exgcd(b,a % b,x,y);    LL temp = x;x = y;y = temp - y * (a / b);    return d;    }    int main(){    a = RD();p = RD();    LL d = exgcd(a,p,x,y);    printf("%d\n",x);    return 0;    }

P3811 【模板】乘法逆元

题目背景

这是一道模板题

题目描述

给定n,p求1~n中所有整数在模p意义下的乘法逆元。

输入输出格式

输入格式:

一行n,p

输出格式:

n行,第i行表示i在模p意义下的逆元。


这次要求求出一串数的逆元之前的算法是\(nlogN\)的复杂度

但很显然承受不了,引入\(O(N)\)求一串逆元的方法(模数\(P\)为质数)

\[inv[1] = 1\]

\[inv[{i}] = ({P - P / i}) * inv[P\; \%\;i]\;\% P\]

Code

#include
#include
#include
#include
#include
#include
typedef long long LL;using namespace std;int RD(){ int out = 0,flag = 1;char c = getchar(); while(c < '0' || c >'9'){if(c == '-')flag = -1;c = getchar();} while(c >= '0' && c <= '9'){out = out * 10 + c - '0';c = getchar();} return flag * out; }const int maxn = 10000019;int a,p;LL x,y;LL exgcd(LL a,LL b,LL &x,LL &y){ if(!b){x = 1,y = 0;return a;} int d = exgcd(b,a % b,x,y); LL temp = x;x = y;y = temp - y * (a / b); return d; }//放个扩欧一起好了(才不是什么第一次扩欧TLE了呢)int inv[maxn];int main(){ a = RD();p = RD(); inv[1] = 1;printf("1\n"); for(int i = 2;i <= a;i++){ inv[i] = (LL)(p - p / i) * inv[p % i] % p; printf("%d\n",inv[i]); } return 0; }

转载于:https://www.cnblogs.com/Tony-Double-Sky/p/9285572.html

你可能感兴趣的文章
LeetCode 540. 有序数组中的单一元素(Single Element in a Sorted Array) 42
查看>>
codevs 5958 无
查看>>
htaccess 实现网址缩短
查看>>
第四周作业&&结对编程
查看>>
12. 构造代码块
查看>>
指针函数与函数指针的区别
查看>>
HDOJ 4734 数位DP
查看>>
我的第一个python web开发框架(15)——公司介绍编辑功能
查看>>
win10
查看>>
JS DOM操作基础
查看>>
DataSet.GetBookMark内存泄漏
查看>>
get请求中params参数的使用
查看>>
[LeetCode] 617. Merge Two Binary Trees
查看>>
[LeetCode] 538. Convert BST to Greater Tree
查看>>
Django中的form模块的高级处理
查看>>
[js]DOM 篇
查看>>
C# 观察者模式
查看>>
SQLite(二)高级操作
查看>>
iOS开发之oc(二十)--Foundation(5)NSDictionary
查看>>
初入RFID技术
查看>>