当前位置:网站首页>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;
}
边栏推荐
- LeetCode_58(最后一个单词的长度)
- Neural network convolution layer
- Pico Neo3手柄抓取物体
- [daily question in summer] Luogu p5740 [deep foundation 7. Example 9] the best student
- 神经网络-最大池化的使用
- The longest increasing subsequence and its optimal solution, total animal weight problem
- Serialization and deserialization of objects
- [hardware ten treasures catalogue] - reprinted from "hardware 100000 whys" (under continuous update ~ ~)
- 洗个冷水澡吧
- Leecode question brushing record 1332 delete palindrome subsequence
猜你喜欢

分布式架构系统拆分原则、需求、微服务拆分步骤

STM32扩展板 温度传感器和温湿度传感器的使用

技术分享| 融合调度中的广播功能设计
![AssertionError assert I.ndim == 4 and I.shape[1] == 3](/img/b1/0109bb0f893eb4c8915df36c100907.png)
AssertionError assert I.ndim == 4 and I.shape[1] == 3

The longest increasing subsequence and its optimal solution, total animal weight problem

先有网络模型的使用及修改

Neural networks - use of maximum pooling

CF1638E. Colorful operations Kodori tree + differential tree array
![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*

Principle, technology and implementation scheme of data consistency in distributed database
随机推荐
[2020 overview] overview of link prediction based on knowledge map embedding
分布式架构系统拆分原则、需求、微服务拆分步骤
[hard ten treasures] - 2 [basic knowledge] characteristics of various topological structures of switching power supply
Shell之Unix运维常用命令
AssertionError assert I.ndim == 4 and I.shape[1] == 3
The longest increasing subsequence and its optimal solution, total animal weight problem
Character input stream and character output stream
Common UNIX Operation and maintenance commands of shell
点赞的云函数
STM32 extended key scan
【硬十宝典】——2.【基础知识】开关电源各种拓扑结构的特点
C#读写应用程序配置文件App.exe.config,并在界面上显示
[summer daily question] Luogu p5886 Hello, 2020!
C - detailed explanation of operators and summary of use cases
科研狗可能需要的一些工具
Basic usage, principle and details of session
RDF query language SPARQL
Construction of Meizhou nursing laboratory: equipment configuration
先有网络模型的使用及修改
pytorch 卷积操作