当前位置:网站首页>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;
}
边栏推荐
- R语言dplyr包进行数据分组聚合统计变换(Aggregating transforms)、计算dataframe数据的分组均值(mean)
- 数学知识——高斯消元(初等行变换解方程组)代码实现
- 史上超级详细,想找工作的你还不看这份资料就晚了
- AcWing 3537. Tree lookup complete binary tree
- Certains marchés de l'emploi de Shanghai refusent d'embaucher des personnes qui se rétablissent positives à Xinguan
- Describe the process of key exchange
- Analysis of frequent chain breaks in applications using Druid connection pools
- Solve DoS attack production cases
- Reptiles have a good time. Are you full? These three bottom lines must not be touched!
- QLabel 跑马灯文字显示
猜你喜欢

Xingnuochi technology's IPO was terminated: it was planned to raise 350million yuan, with an annual revenue of 367million yuan

Synchronous development of business and application: strategic suggestions for application modernization

Tongyu Xincai rushes to Shenzhen Stock Exchange: the annual revenue is 947million Zhang Chi and Su Shiguo are the actual controllers

朗坤智慧冲刺科创板:年营收4亿 拟募资7亿

Word如何显示修改痕迹

Solve DoS attack production cases

About static type, dynamic type, ID, instancetype

业务与应用同步发展:应用现代化的策略建议

美庐生物IPO被终止:年营收3.85亿 陈林为实控人

深度循环网络长期血压预测【翻译】
随机推荐
R语言ggplot2可视化:使用ggpubr包的ggstripchart函数可视化分组点状条带图(dot strip plot)、设置add参数为不同水平点状条带图添加箱图
关于静态类型、动态类型、id、instancetype
手写一个的在线聊天系统(原理篇1)
Yutai micro rushes to the scientific innovation board: Huawei and Xiaomi fund are shareholders to raise 1.3 billion
包装行业商业供应链管理平台解决方案:布局智慧供应体系,数字化整合包装行业供应链
In 50W, what have I done right?
[translation] a GPU approach to particle physics
上海部分招工市场对新冠阳性康复者拒绝招录
The nearest library of Qinglong panel
R语言使用rchisq函数生成符合卡方分布的随机数、使用plot函数可视化符合卡方分布的随机数(Chi Square Distribution)
Deep circulation network long-term blood pressure prediction [translation]
Don't miss this underestimated movie because of controversy!
Based on butterfly species recognition
Installation and management procedures
test about BinaryTree
Qlabel marquee text display
[matlab] Simulink the input and output variables of the same module cannot have the same name
Mathematical knowledge -- code implementation of Gaussian elimination (elementary line transformation to solve equations)
QPushButton绑定快捷键的注意事项
中缀表达式转后缀表达式详细思路及代码实现
