当前位置:网站首页>AcWing 887. Finding combinatorial number III (Lucas theorem)
AcWing 887. Finding combinatorial number III (Lucas theorem)
2022-07-01 04:51:00 【MangataTS】
Problem plane connection
https://www.acwing.com/problem/content/description/889/
Ideas
We will find that our modulus is much smaller than the number of combinations we require , And modulus p It's a prime number , Then we can use lucas Theorem To help us solve this problem quickly ,Lucas Theorem : C a b m o d p = C a m o d p b m o d p × l u c a s ( a / p , b / p ) m o d p C_a ^b \ mod\ p = C_{a \ mod \ p} ^{ {b \ mod \ p}} \times lucas(a/p,b/p) \ mod \ p Cab mod p=Ca mod pb mod p×lucas(a/p,b/p) mod p
In the mold p Under the circumstances , Then we can easily write the code for this idea , Of course, we can preprocess factorials or calculate directly when dealing with the calculation of combinatorial numbers , In the case of less visits to this topic, we can do both
About Lucas For the proof of the theorem, see :
https://www.cnblogs.com/onlyblues/p/15339937.html
Code
Preprocessing factorials
when :4047 ms
#include<bits/stdc++.h>
using namespace std;
//---------------- Custom part ----------------
#define ll long long
#define endl "\n"
#define PII pair<int,int>
#define INF 0x3f3f3f3f
int dx[4] = {
-1, 0, 1, 0}, dy[4] = {
0, 1, 0, -1};
const int N = 2e6+10;
ll t,n,m,q,fact[N],invfact[N],mod;
ll ksm(ll a,ll b) {
ll ans = 1;
for(;b;b>>=1LL) {
if(b & 1) ans = ans * a % mod;
a = a * a % mod;
}
return ans;
}
ll lowbit(ll x){
return -x & x;}
//---------------- Custom part ----------------
void init(){
invfact[0]= fact[0] = 1;// initialization
for(int i = 1;i < N; ++i) {
fact[i] = fact[i-1] * i % mod;
invfact[i] = ksm(fact[i],mod-2);
}
}
ll C(ll a,ll b){
return (fact[a] * invfact[a-b]) % mod * invfact[b] % mod ;
}
ll lucas(ll a,ll b,ll p){
if(a < p && b < p) return C(a,b);
else return lucas(a/p,b/p,p) * C(a % p,b % p) % p;
}
void slove(){
ll a,b;
cin>>a>>b>>mod;
init();
cout<<lucas(a,b,mod)<<endl;
}
int main()
{
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>t;
while(t--){
slove();
}
return 0;
}
Direct calculation
when :97ms
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
int qmi(int a, int k, int p)
{
int res = 1;
while (k)
{
if (k & 1) res = (LL)res * a % p;
a = (LL)a * a % p;
k >>= 1;
}
return res;
}
int C(int a, int b, int p)
{
if (b > a) return 0;
LL resL = 1,resR = 1;
for (int i = 1, j = a; i <= b; i ++, j -- )
{
resL = (LL)resL * j % p;
resR = (LL)resR * i % p;
}
return resL * qmi(resR,p-2,p) % p;
}
int lucas(LL a, LL b, int p)
{
if (a < p && b < p) return C(a, b, p);
return (LL)C(a % p, b % p, p) * lucas(a / p, b / p, p) % p;
}
int main()
{
int n;
cin >> n;
while (n -- )
{
LL a, b;
int p;
cin >> a >> b >> p;
cout << lucas(a, b, p) << endl;
}
return 0;
}
边栏推荐
- LeetCode1497-检查数组对是否可以被 k 整除-数组-哈希表-计数
- Common UNIX Operation and maintenance commands of shell
- Some tools that research dogs may need
- 1076 Forwards on Weibo
- LeetCode_ 28 (implement strstr())
- Design experience of Meizhou clinical laboratory
- 分布式事务-解决方案
- LeetCode_28(实现 strStr())
- LeetCode522-最长特殊序列II-哈希表-字符串-双指针
- [une question par jour pendant l'été] course luogu p1568
猜你喜欢

CF1638E. Colorful operations Kodori tree + differential tree array

Manually implement a simple stack

神经网络-最大池化的使用

STM32扩展板 数码管显示

Pytorch(二) —— 激活函数、损失函数及其梯度
![Solution: thread 1:[< *> setvalue:forundefined key]: this class is not key value coding compliant for the key*](/img/88/0b99d1db2cdc70ab72d2b3c623dfaa.jpg)
Solution: thread 1:[< *> setvalue:forundefined key]: this class is not key value coding compliant for the key*

Common methods in transforms

技术分享| 融合调度中的广播功能设计

无器械健身

常用的Transforms中的方法
随机推荐
Neural networks - use of maximum pooling
Difficulties in the development of knowledge map & the importance of building industry knowledge map
Several methods of creating thread classes
【暑期每日一题】洛谷 P2026 求一次函数解析式
Neural network convolution layer
RuntimeError: “max_pool2d“ not implemented for ‘Long‘
先有网络模型的使用及修改
One click shell to automatically deploy any version of redis
分布式事务-解决方案
Serialization and deserialization of objects
Thread safety issues
STM32扩展版 按键扫描
Shell analysis server log command collection
Print stream and system setout();
Quelques outils dont les chiens scientifiques pourraient avoir besoin
LeetCode522-最长特殊序列II-哈希表-字符串-双指针
Research on medical knowledge atlas question answering system (I)
Use and modification of prior network model
[daily question in summer] Luogu p1568 race
js解决浮点数相乘精度丢失问题