当前位置:网站首页>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;
}
边栏推荐
- English topic assignment (25)
- 基于ppg和fft神经网络的光学血压估计【翻译】
- Installation and management procedures
- AutoCAD - what is the default lineweight for centerline drawing and CAD? Can I modify it?
- Understanding disentangling in β- VAE paper reading notes
- 同宇新材冲刺深交所:年营收9.47亿 张驰与苏世国为实控人
- 基于蝴蝶种类识别
- 2022.2.12
- Openmv4 learning notes 1 --- one click download, background knowledge of image processing, lab brightness contrast
- Actf 2022 came to a successful conclusion, and 0ops team won the second consecutive championship!!
猜你喜欢
Three years of Android development, Android interview experience and real questions sorting of eight major manufacturers during the 2022 epidemic

Wx applet learning notes day01

多线程基础:线程基本概念与线程的创建

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

The second day of rhcsa study
受益匪浅,安卓面试问题

Actf 2022 came to a successful conclusion, and 0ops team won the second consecutive championship!!

Synchronous development of business and application: strategic suggestions for application modernization
![Deep circulation network long-term blood pressure prediction [translation]](/img/9c/c1ed28242a4536c1e8fde3414f82a8.png)
Deep circulation network long-term blood pressure prediction [translation]
![[translation] a GPU approach to particle physics](/img/07/57036c925155cab36678c696e89440.jpg)
[translation] a GPU approach to particle physics
随机推荐
AUTOCAD——中心线绘制、CAD默认线宽是多少?可以修改吗?
Tongyu Xincai rushes to Shenzhen Stock Exchange: the annual revenue is 947million Zhang Chi and Su Shiguo are the actual controllers
用于远程医疗的无创、无袖带血压测量【翻译】
Crawling data encounters single point login problem
涂鸦智能在香港双重主板上市:市值112亿港元 年营收3亿美元
上海部分招工市場對新冠陽性康複者拒絕招錄
Online notes
MRO industrial products enterprise procurement system: how to refine procurement collaborative management? Industrial products enterprises that want to upgrade must see!
AIRIOT物联网平台赋能集装箱行业构建【焊接工位信息监控系统】
Video based full link Intelligent Cloud? This article explains in detail what Alibaba cloud video cloud "intelligent media production" is
[translation] a GPU approach to particle physics
pychrm社区版调用matplotlib.pyplot.imshow()函数图像不弹出的解决方法
Noninvasive and cuff free blood pressure measurement for telemedicine [translation]
Interview assault 63: how to remove duplication in MySQL?
Meilu biological IPO was terminated: the annual revenue was 385million, and Chen Lin was the actual controller
test about BinaryTree
全套教学资料,阿里快手拼多多等7家大厂Android面试真题
渲大师携手向日葵,远控赋能云渲染及GPU算力服务
The list of people who passed the fifth phase of personal ability certification assessment was published
From 2022 to 2024, the list of cifar azrieli global scholars was announced, and 18 young scholars joined 6 research projects
