当前位置:网站首页>Find the number of combinations (the strongest optimization)
Find the number of combinations (the strongest optimization)
2022-07-27 05:21:00 【Ye Chen】
Simple optimization ( No modulo operation )
When m < n-m when , hold n - m Change it to m, Use the following algorithm , Simple optimization
C n m = C n n − m C_{n}^{m}=C_{n}^{n-m} Cnm=Cnn−m
The code is as follows :
#include<cstdio>
using namespace std;
long long C(int n, int m){
if (m<n-m) m=n-m;
long long ans=1;
for (int i=m+1;i<=n;i++) ans *= i;
for (int i=1;i<=n-m;i++) ans /= i;
return ans;
}
int main(){
printf("%lld\n",C(n,m));
return 0;
}
Advanced optimization ( Inverse element - Modular of combinatorial data )
problem : seek C n m C_{n}^{m} Cnm%p( classic p=1e9+7)
stay C n m C_{n}^{m} Cnm Operation , Modulus property does not apply to divisor , Need to put “ division ” Convert to “ Multiplication ”, Only with the help of the nature of the module can we not explode long long Calculate the number of combinations . And that's where it comes in “ Inverse element ”!
Inverse element : about a and p(a and p Relatively prime ), if a*b%p≡1, said b by a%p Inverse element .
What's the use of this inverse element ? Imagine begging ( a b \frac{a}{b} ba )%p, If you know b%p The inverse element of is c, It can be transformed into ( a b \frac{a}{b} ba )%p = a*c%p = (a%p)(c%p)%p
How to find the inverse element ? At this time, we need to introduce the powerful Fermat theorem !
Fermat's small Theorem : about a And prime numbers p, Satisfy $ a^{p-1} $ %p≡1
And then because $ a^{p-1} $ = a p − 2 a^{p-2} ap−2 ∗a, So there is a p − 2 a^{p-2} ap−2∗a%p≡1! By comparing the definition of inverse element , a p − 2 a^{p-2} ap−2 yes a Inverse element !
So the problem is transformed into solving a p − 2 a^{p-2} ap−2, That is, it becomes the problem of finding the fast power ( Of course, this needs to be met p As a prime number ).
Please refer to another article of blogger for quick power : Fast power algorithm
Now summarize the solution C n m C_{n}^{m} Cnm%p Steps for :
- Through the loop , Calculate in advance all less than max_number The factorial ( %p ) Result , Deposit in fac[max_number] in ( fac[i] = i ! %p )
- seek m!%p Inverse element ( The o fac[m] Inverse element ): According to Fermat's theorem ,x%p The inverse element of is x p − 2 x^{p-2} xp−2, So by fast exponentiation , solve f a c [ m ] p − 2 fac[m]^{p-2} fac[m]p−2 %p, Write it down as M
- seek (n-m)!%p Inverse element : Similarly, it is to solve f a c [ n − m ] p − 2 fac[n-m]^{p-2} fac[n−m]p−2%p, Write it down as NM
- C n m C_{n}^{m} Cnm%p = ( ( fac[n] * M)%p * NM)%p
use C++ Implemented code , Input is n,m,p, requirement 0<=m<=n<=1e6( otherwise fac There is no room for ), And gcd(p,n!)=1( Namely mutual prime ), Output is Cmn%p
long long fac[MAXN];
long long n,m,p;
// Fast exponentiation x^n%mod
long long pow_mod(long long x, long long n, long long mod) {
long long res=1;
while(n){
if(n&1) res=res*x%mod;
x=x*x%mod;
n>>=1;
}
return res;
}
int main() {
while(~scanf("%lld %lld %lld", &n, &m, &p)) {
// Pretreatment requires fac,fac[i]=i!%p
fac[0] = 1;
for (int i = 1; i <= n; i++) {fac[i] = fac[i - 1] * i % p;}
// Combinatorial number = n!*(m!%p Inverse element )*((n-m)!%p Inverse element )%p
printf("%lld\n", fac[n] * pow_mod(fac[m], p - 2, p) % p * pow_mod(fac[n - m], p - 2, p) % p);
}
}
Reference link —— Inverse element - Modular of combinatorial data
边栏推荐
- Translation of robot and precise vehicle localization based on multi sensor fusion in diverse city scenes
- 整合SSM
- Could not autowire.No beans of ‘userMapper‘ type found.
- B1025 反转链表*******
- 《Robust and Precise Vehicle Localization based on Multi-sensor Fusionin Diverse City Scenes》翻译
- JVM Part 1: memory and garbage collection part 7 -- runtime data area heap
- 辗转相除法
- Detailed description of polymorphism
- Shell course summary
- Li Kou achieved the second largest result
猜你喜欢

如何快速有效解决数据库连接失败问题
![[Niuke discussion area] Chapter 7: building safe and efficient enterprise services](/img/62/2b170e8dd5034708aebc6df0bc2b06.png)
[Niuke discussion area] Chapter 7: building safe and efficient enterprise services

Svn usage details

JVM上篇:内存与垃圾回收篇三--运行时数据区-概述及线程

微淼联合创始人孙延芳:以合规为第一要义,做财商教育“正规军”

JVM Part 1: memory and garbage collection part 9 - runtime data area - object instantiation, memory layout and access location

B1021 single digit statistics

How to test the payment process?

笔记系列之docker安装Postgresql 14
![[CSAPP] Application of bit vectors | encoding and byte ordering](/img/96/344936abad90ea156533ff49e74f59.gif)
[CSAPP] Application of bit vectors | encoding and byte ordering
随机推荐
Bean的生命周期&&依赖注入*依赖自动装配
Machine learning overview
Tcp server是如何一个端口处理多个客户端连接的(一对一还是一对多)
Raspberry pie output PWM wave to drive the steering gear
JVM上篇:内存与垃圾回收篇九--运行时数据区-对象的实例化,内存布局与访问定位
JVM Part 1: memory and garbage collection part 8 - runtime data area - Method area
34. Analyze flexible.js
微淼联合创始人孙延芳:以合规为第一要义,做财商教育“正规军”
2022年郑州轻工业新生赛题目-打死我也不说
Another skill is to earn 30000 yuan a month+
B1025 反转链表*******
稀疏数组→五子棋的存盘续盘等操作
Introduction to dynamic memory functions (malloc free calloc realloc)
使用Druid连接池创建DataSource(数据源)
B1027 打印沙漏
MQ message queue is used to design the high concurrency of the order placing process, the generation scenarios and solutions of message squeeze, message loss and message repetition
How to store the startprocessinstancebykey method in acticiti in the variable table
Two dimensional array summation exercise
B1030 完美数列
2021 OWASP top 4: unsafe design