当前位置:网站首页>快速幂模板求逆元,逆元的作用以及例题【第20届上海大学程序设计联赛夏季赛】排列计数
快速幂模板求逆元,逆元的作用以及例题【第20届上海大学程序设计联赛夏季赛】排列计数
2022-07-06 11:18:00 【吴雨4】
乘法逆元的定义
若整数 b,m 互质,并且对于任意的整数 a,如果满足 b|a(整除),则存在一个整数 x,使得 a/b≡a*x(mod m),则称 x 为 b 的模 m 乘法逆元,记为
(mod m)。 b存在乘法逆元的充要条件是 b 与模数 m 互质。当模数 m为质数时, 即
为 b 的乘法逆元。

所以可以用快速幂的模板求解逆元。
题面:

代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e6+10;
const int mod=1e9+7;
//快速幂模板
int qmi(int a,int k,int p)
{
int res=1;
while(k)
{
if(k&1) res=(ll)res*a%p;
k>>=1;
a=(ll) a*a%p;
}
return res;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin>>n;
while(n--)
{
ll a,p;
cin>>a>>p;
if(a%p) cout<<qmi(a,p-2,p)<<endl;//只有当a不是p的倍数时,才有逆元。
//若a是p的倍数,则有a/b=0,不会同余于a*x了,即无逆元。
else cout<<"impossible"<<endl;
}
return 0;
}
那我们来说说逆元的作用吧
先来说说加减乘除的求余概念
(a + b) % p = (a%p + b%p) %p (√)
(a - b) % p = (a%p - b%p) %p (√)
(a * b) % p = (a%p * b%p) %p (√)
(a / b) % p = (a%p / b%p) %p (×)
这里不难看出来做除法取余是错的吧,看个例子:
(100/50)%20 = 2 ≠ (100%20) / (50%20) %20 = 0
对于一些题目,由于数字太大,电脑存不下,我们必须在中间过程中进行求余。若这个算式中出现除法,我们就不能先做除法再取模,由上面可知“除法取余是错的”,由此我们就需要用逆元来代替。
例题:E-排列计数_第20届上海大学程序设计联赛夏季赛(校外同步赛) (nowcoder.com)
题目描述
输入描述:
输出描述:
输出一行一个整数,表示满足条件的排列数,由于答案可能很大,请对1e9+7取模
示例1
输入
1
输出
1
示例2
输入
3
输出
360
由题目可以看出来,我们需要求的是2*n!/2的值输出。
如果直接写阶乘取模输出,当然会wa咯。

这里稍微把那个除2的运算改成乘上2(模mod)的逆元再取模就会ac了。
代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e6+10;
const int mod=1e9+7;
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
//快速幂模板
int qmi(int a,int k,int p)
{
int res=1;
while(k)
{
if(k&1) res=(ll)res*a%p;
k>>=1;
a=(ll) a*a%p;
}
return res;
}
int main()
{
IOS;
ll n;
cin>>n;
ll ans=1;
for(int i=1;i<=2*n;i++)
ans=(ans*i)%mod;
cout<<ans*qmi(2,mod-2,mod)%mod<<endl;
return 0;
}
边栏推荐
- 上海部分招工市场对新冠阳性康复者拒绝招录
- First day of rhcsa study
- R language uses rchisq function to generate random numbers that conform to Chi square distribution, and uses plot function to visualize random numbers that conform to Chi square distribution
- Interface test tool - postman
- [matlab] Simulink the input and output variables of the same module cannot have the same name
- Three years of Android development, Android interview experience and real questions sorting of eight major manufacturers during the 2022 epidemic
- 涂鸦智能在香港双重主板上市:市值112亿港元 年营收3亿美元
- R语言ggplot2可视化:使用ggpubr包的ggstripchart函数可视化分组点状条带图(dot strip plot)、设置add参数为不同水平点状条带图添加箱图
- test about BinaryTree
- QLabel 跑马灯文字显示
猜你喜欢

Visual Studio Code启动时提示“Code安装似乎损坏。请重新安装。”、标题栏显示“不受支持”信息的解决办法

星诺奇科技IPO被终止:曾拟募资3.5亿元 年营收3.67亿

抽象类与抽象方法

Solution of intelligent management platform for suppliers in hardware and electromechanical industry: optimize supply chain management and drive enterprise performance growth

线代笔记....
![[paper notes] transunet: transformers make strongencoders for medical image segmentation](/img/21/3d4710024248b62495e2681ebd1bc4.png)
[paper notes] transunet: transformers make strongencoders for medical image segmentation

二叉搜索树

Characteristic colleges and universities, jointly build Netease Industrial College
![[depth first search] Ji suanke: find numbers](/img/e4/708a1e8252bcd2d0d621c66bf6bfed.jpg)
[depth first search] Ji suanke: find numbers
![[matlab] Simulink the input and output variables of the same module cannot have the same name](/img/99/adfe50075010916439cd053b8f04c7.png)
[matlab] Simulink the input and output variables of the same module cannot have the same name
随机推荐
人体骨骼点检测:自顶向下(部分理论)
Openmv4 learning notes 1 --- one click download, background knowledge of image processing, lab brightness contrast
业务与应用同步发展:应用现代化的策略建议
R语言使用rchisq函数生成符合卡方分布的随机数、使用plot函数可视化符合卡方分布的随机数(Chi Square Distribution)
MRO工业品企业采购系统:如何精细化采购协同管理?想要升级的工业品企业必看!
Help improve the professional quality of safety talents | the first stage of personal ability certification and assessment has been successfully completed!
Php+redis realizes the function of canceling orders over time
Penetration test information collection - App information
五金机电行业智能供应链管理系统解决方案:数智化供应链为传统产业“造新血”
Describe the process of key exchange
Camel case with Hungarian notation
openmv4 学习笔记1----一键下载、图像处理背景知识、LAB亮度-对比度
Yutai micro rushes to the scientific innovation board: Huawei and Xiaomi fund are shareholders to raise 1.3 billion
线代笔记....
The nearest library of Qinglong panel
Xingnuochi technology's IPO was terminated: it was planned to raise 350million yuan, with an annual revenue of 367million yuan
Three years of Android development, Android interview experience and real questions sorting of eight major manufacturers during the 2022 epidemic
Video based full link Intelligent Cloud? This article explains in detail what Alibaba cloud video cloud "intelligent media production" is
R语言使用order函数对dataframe数据进行排序、基于单个字段(变量)进行降序排序(DESCENDING)
Some recruitment markets in Shanghai refuse to recruit patients with covid-19 positive
