推理:

假如当前计算的是x在%p意义下的逆元,设$p=kx+y$,则

$\Large kx+y\equiv 0(mod\ p)$

两边同时乘上$x^{-1}y^{-1}$(这里代表逆元)

则方程变为$\Large k*y^{-1}+x^{-1}\equiv 0(mod\ p)$

化简得$\Large x^{-1}\equiv -k*y^{-1}(mod\ p)$

$\Large x^{-1}\equiv -\biggl\lfloor\frac{p}{x}\biggr\rfloor *(p\ mod\ x)^{-1}(mod\ p)$

结果为

$\Large x^{-1}\equiv (p-\biggl\lfloor\frac{p}{x}\biggr\rfloor )*(p\ mod\ x)^{-1}(mod\ p)$

除了1,p mod x一定小于x,它的逆元已经算过,所以可以线性求出逆元

void Inverse(int p,int a[],int n){//线性求<=n的数%p意义下的逆元
a[]=;
for(int i=;i<=n;i++){
a[i]=1ll*(p-p/i)*a[p%i]%p;
}
}

线性求所有数模p的乘法逆元的更多相关文章

  1. Hdu 1452 Happy 2004(除数和函数,快速幂乘(模),乘法逆元)

    Problem Description Considera positive integer X,and let S be the sum of all positive integer diviso ...

  2. 【模板】求1~n的整数的乘法逆元

    洛谷3811 先用n!p-2求出n!的乘法逆元 因为有(i-1)!-1=i!-1*i (mod p),于是我们可以O(n)求出i!-1 再用i!-1*(i-1)!=i-1 (mod p)即是答案 #i ...

  3. A. On The Way to Lucky Plaza 概率 乘法逆元

    A. On The Way to Lucky Plaza time limit per test 1.0 s memory limit per test 256 MB input standard i ...

  4. Light OJ 1067 Combinations (乘法逆元)

    Description Given n different objects, you want to take k of them. How many ways to can do it? For e ...

  5. hdu_1452_Happy 2004 (乘法逆元

    Consider a positive integer X,and let S be the sum of all positive integer divisors of 2004^X. Your ...

  6. 乘法逆元__C++

    在开始之前我们先介绍3个定理: 1.乘法逆元(在维基百科中也叫倒数,当然是 mod p后的,其实就是倒数不是吗?): 如果ax≡1 (mod p),且gcd(a,p)=1(a与p互质),则称a关于模p ...

  7. 题解报告:hdu 1576 A/B(exgcd、乘法逆元+整数快速幂)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1576 Problem Description 要求(A/B)%9973,但由于A很大,我们只给出n(n ...

  8. HDU6608-Fansblog(Miller_Rabbin素数判定,威尔逊定理应用,乘法逆元)

    Problem Description Farmer John keeps a website called ‘FansBlog’ .Everyday , there are many people ...

  9. 简记乘法逆元(费马小定理+扩展Euclid)

    乘法逆元 什么是乘法逆元? 若整数 \(b,m\) 互质,并且\(b|a\) ,则存在一个整数\(x\) ,使得 \(\frac{a}{b}\equiv ax\mod m\) . 称\(x\) 是\( ...

随机推荐

  1. HYNB Round 15: PKU Campus 2019

    HYNB Round 15: PKU Campus 2019 C. Parade 题意 将平面上n*2个点安排在长度为n的两行上. 做法 首先可以忽略每个点之间的影响,只用考虑匹配即可 然后把所以点归 ...

  2. minifilter 算是总结吧

    FltRegisterFilter 注册过滤器 FltStartFiltering 开始过滤 InstatanceSetupCallback 实例安装回调 .当一个微过滤器加载的时候,每个存在的卷都会 ...

  3. JS规则 我或你都可以 (逻辑或操作符)||逻辑或操作符,相当于生活中的“或者”,当两个条件中有任一个条件满足,“逻辑或”的运算结果就为“真”

    我或你都可以 (逻辑或操作符) "||"逻辑或操作符,相当于生活中的"或者",当两个条件中有任一个条件满足,"逻辑或"的运算结果就为&quo ...

  4. RuntimeError: Python is not installed as a framework. The Mac OS X backend will not be able to function correctly if Python is not installed as a framework. See the Python documentation for more infor

    在virtualenv环境下使用matplotlib绘图时遇到了这样的问题: >>> import matplotlib.pyplot as pltTraceback (most r ...

  5. web.xml中多个Servlet执行顺序的问题!

    1.两个servlet或者两个servlet-mapping,其中的servlet-name名称不能存在相同. 2.所有的servlet-mapping标签下,url-pattern中包含的文本不能相 ...

  6. csp-s模拟65Simple,Walk, Travel,棋盘题解

    题面:https://www.cnblogs.com/Juve/articles/11639923.html simple: 考试时只想到的暴力exgcd判断 考虑n,m互质的情况: 我们枚举y,对于 ...

  7. LUOGU P4163 [SCOI2007]排列

    传送门 解题思路 首先我们发现这道题s的长度很小,所以考虑点暴力的做法,状压dp或搜索.本蒟蒻搜索永远调不对,所以就写了个状压dp.因为所有s里的数都要出现一次,并且最后的答案是要求整除,那么我们设d ...

  8. Xcode导航栏功能简介

    1.Xcode 1.1.AboutXcode 1.2.Preferences General  Accounts   Behaviors1 Behavior2    Navigation Fonts& ...

  9. codeforces 1195D2-Submarine in the Rybinsk Sea

    传送门:QAQQAQ 题意:自己看 思路:就是一个类似于数位DP的东西... 统计a[i]数位分解的数在每一位出现的个数,即分两种讨论: 1.位数小于当前j,则j会出现在q+i,而且计算顺序互换会计算 ...

  10. hibernate使用truncate清空表 截断表

    public void truncateTable(Session session, String tableNameInDb) { String sql = " truncate tabl ...