当前位置:网站首页>Fast power template for inverse element, the role of inverse element and example [the 20th summer competition of Shanghai University Programming League] permutation counting
Fast power template for inverse element, the role of inverse element and example [the 20th summer competition of Shanghai University Programming League] permutation counting
2022-07-06 19:09:00 【Wu Yu 4】
Definition of multiplicative inverse
If integer b,m Coprime , And for any integer a, If meet b|a( to be divisible by ), Then there is an integer x, bring a/b≡a*x(mod m), said x by b The mold m Multiplicative inverse element , Write it down as (mod m). b The necessary and sufficient condition for the existence of multiplicative inverse is b And modulus m Coprime . When modulus m Prime number , namely
by b Multiplicative inverse element of .
So we can use the template of fast power to solve the inverse element .
Example :876. Fast power inverse - AcWing Question bank
Topic :
Code :
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e6+10;
const int mod=1e9+7;
// Fast power template
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;// Only when a No p A multiple of the , Only then has the inverse element .
// if a yes p Multiple , Then there are a/b=0, Will not be the same as a*x 了 , That is, there is no inverse element .
else cout<<"impossible"<<endl;
}
return 0;
}
Let's talk about the function of inverse element
First, let's talk about the concept of addition, subtraction, multiplication and division
(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 (×)
It's not hard to see that it's wrong to do division and remainder , Look at an example :
(100/50)%20 = 2 ≠ (100%20) / (50%20) %20 = 0
For some topics , Because the number is too big , The computer can't save , We must find the remainder in the middle process . If division occurs in this expression , We can't divide first and then take the module , From the top “ Division and remainder are wrong ”, Therefore, we need to use the inverse element to replace .
Title Description
Input description :
Output description :
Output a line of integers , Indicates the number of permutations that meet the conditions , Because the answer can be big , Yes, please. 1e9+7 modulus
Example 1
Input
1
Output
1
Example 2
Input
3
Output
360
It can be seen from the title , What we need to ask for is 2*n!/2 Value output of .
If you write factorial modulo output directly , Of course. wa Slightly .
Here, put that slightly except 2 The operation of is changed to Multiply 2( model mod) And then take the module will ac 了 .
Code :
#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);
// Fast power template
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;
}
边栏推荐
- 三面蚂蚁金服成功拿到offer,Android开发社招面试经验
- Oracle advanced (IV) table connection explanation
- Lucun smart sprint technology innovation board: annual revenue of 400million, proposed to raise 700million
- 基于ppg和fft神经网络的光学血压估计【翻译】
- Solution of commercial supply chain management platform for packaging industry: layout smart supply system and digitally integrate the supply chain of packaging industry
- Tongyu Xincai rushes to Shenzhen Stock Exchange: the annual revenue is 947million Zhang Chi and Su Shiguo are the actual controllers
- 能源行业的数字化“新”运维
- LeetCode-1279. 红绿灯路口
- Analysis of frequent chain breaks in applications using Druid connection pools
- R语言ggplot2可视化:使用ggpubr包的ggdotplot函数可视化点阵图(dot plot)、设置palette参数设置不同水平点阵图数据点和箱图的颜色
猜你喜欢
美庐生物IPO被终止:年营收3.85亿 陈林为实控人
三年Android开发,2022疫情期间八家大厂的Android面试经历和真题整理
About static type, dynamic type, ID, instancetype
LeetCode-1279. 红绿灯路口
ACTF 2022圆满落幕,0ops战队二连冠!!
五金机电行业智能供应链管理系统解决方案:数智化供应链为传统产业“造新血”
How are you in the first half of the year occupied by the epidemic| Mid 2022 summary
Master Xuan joined hands with sunflower to remotely control enabling cloud rendering and GPU computing services
Summary of performance knowledge points
基于蝴蝶种类识别
随机推荐
QPushButton绑定快捷键的注意事项
人体骨骼点检测:自顶向下(部分理论)
第五期个人能力认证考核通过名单公布
English topic assignment (25)
Understanding disentangling in β- VAE paper reading notes
Interface test tool - postman
helm部署etcd集群
Detailed idea and code implementation of infix expression to suffix expression
Pytorch common loss function
[paper notes] transunet: transformers make strongencoders for medical image segmentation
Mathematical knowledge -- code implementation of Gaussian elimination (elementary line transformation to solve equations)
Crawling data encounters single point login problem
Intelligent supply chain management system solution for hardware and electromechanical industry: digital intelligent supply chain "creates new blood" for traditional industries
About NPM install error 1
五金机电行业供应商智慧管理平台解决方案:优化供应链管理,带动企业业绩增长
AutoCAD - what is the default lineweight for centerline drawing and CAD? Can I modify it?
关于静态类型、动态类型、id、instancetype
How are you in the first half of the year occupied by the epidemic| Mid 2022 summary
Estimate blood pressure according to PPG using spectral spectrum time depth neural network [turn]
多线程基础:线程基本概念与线程的创建