当前位置:网站首页>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;
}
边栏推荐
- Analysis of frequent chain breaks in applications using Druid connection pools
- AUTOCAD——中心线绘制、CAD默认线宽是多少?可以修改吗?
- Mathematics in machine learning -- common probability distribution (XIII): Logistic Distribution
- Synchronous development of business and application: strategic suggestions for application modernization
- Jushan database was among the first batch of financial information innovation solutions!
- Precautions for binding shortcut keys of QPushButton
- Qlabel marquee text display
- 上海部分招工市场对新冠阳性康复者拒绝招录
- The nearest library of Qinglong panel
- In 50W, what have I done right?
猜你喜欢

Characteristic colleges and universities, jointly build Netease Industrial College

Oracle advanced (IV) table connection explanation

Describe the process of key exchange

根据PPG估算血压利用频谱谱-时间深度神经网络【翻】

Abstract classes and abstract methods

From 2022 to 2024, the list of cifar azrieli global scholars was announced, and 18 young scholars joined 6 research projects

Handwritten online chat system (principle part 1)

Synchronous development of business and application: strategic suggestions for application modernization
![Optical blood pressure estimation based on PPG and FFT neural network [translation]](/img/88/2345dac73248a5f0f9fa3142ca0397.png)
Optical blood pressure estimation based on PPG and FFT neural network [translation]

快速幂模板求逆元,逆元的作用以及例题【第20届上海大学程序设计联赛夏季赛】排列计数
随机推荐
On AAE
C language daily practice - day 22: Zero foundation learning dynamic planning
Tensorflow and torch code verify whether CUDA is successfully installed
驼峰式与下划线命名规则(Camel case With hungarian notation)
Visual Studio Code启动时提示“Code安装似乎损坏。请重新安装。”、标题栏显示“不受支持”信息的解决办法
How are you in the first half of the year occupied by the epidemic| Mid 2022 summary
RedisSystemException:WRONGTYPE Operation against a key holding the wrong kind of value
2022.2.12
Actf 2022 came to a successful conclusion, and 0ops team won the second consecutive championship!!
AIRIOT物联网平台赋能集装箱行业构建【焊接工位信息监控系统】
人体骨骼点检测:自顶向下(部分理论)
基于ppg和fft神经网络的光学血压估计【翻译】
How word displays modification traces
渲大师携手向日葵,远控赋能云渲染及GPU算力服务
同宇新材冲刺深交所:年营收9.47亿 张驰与苏世国为实控人
Deep circulation network long-term blood pressure prediction [translation]
LeetCode-1279. 红绿灯路口
Precautions for binding shortcut keys of QPushButton
朗坤智慧冲刺科创板:年营收4亿 拟募资7亿
Noninvasive and cuff free blood pressure measurement for telemedicine [translation]
