当前位置:网站首页>快速幂模板求逆元,逆元的作用以及例题【第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;
}
边栏推荐
- 抽象类与抽象方法
- 上海部分招工市场对新冠阳性康复者拒绝招录
- Openmv4 learning notes 1 --- one click download, background knowledge of image processing, lab brightness contrast
- 使用map函数、split函数一行键入多个元素
- AcWing 3537.树查找 完全二叉树
- Penetration test information collection - App information
- pytorch常见损失函数
- php+redis实现超时取消订单功能
- ROS custom message publishing subscription example
- 2022.2.12
猜你喜欢

如何提高网站权重

测试行业的小伙伴,有问题可以找我哈。菜鸟一枚~

人体骨骼点检测:自顶向下(部分理论)

提前解锁 2 大直播主题!今天手把手教你如何完成软件包集成?|第 29-30 期

Help improve the professional quality of safety talents | the first stage of personal ability certification and assessment has been successfully completed!

How word displays modification traces

AvL树的实现

线代笔记....

ROS custom message publishing subscription example

Php+redis realizes the function of canceling orders over time
随机推荐
pytorch常见损失函数
R language ggplot2 visualization: use the ggdotplot function of ggpubr package to visualize dot plot, set the palette parameter, and set the colors of data points and box graphs of dot plots at differ
第五期个人能力认证考核通过名单公布
同宇新材冲刺深交所:年营收9.47亿 张驰与苏世国为实控人
驼峰式与下划线命名规则(Camel case With hungarian notation)
Reptiles have a good time. Are you full? These three bottom lines must not be touched!
Estimate blood pressure according to PPG using spectral spectrum time depth neural network [turn]
Optical blood pressure estimation based on PPG and FFT neural network [translation]
Visual Studio Code启动时提示“Code安装似乎损坏。请重新安装。”、标题栏显示“不受支持”信息的解决办法
Penetration test information collection - App information
Interface test tool - postman
About NPM install error 1
[depth first search] Ji suanke: find numbers
Three years of Android development, Android interview experience and real questions sorting of eight major manufacturers during the 2022 epidemic
安装Mysql报错:Could not create or access the registry key needed for the...
Summary of performance knowledge points
The second day of rhcsa study
[paper notes] transunet: transformers make strongencoders for medical image segmentation
Helm deploy etcd cluster
Solve DoS attack production cases
