当前位置:网站首页>组合数模板
组合数模板
2022-08-10 07:56:00 【ThXe】
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10,mod=1e9+7;
typedef long long ll;
struct CalC {
ll f[N], inff[N];
ll imp(ll a, ll k, ll q) {
ll res = 1;
while (k) {
if (k & 1) res = res * a % q;
a = a * a % q;
k >>= 1;
}
return res;
}
void init() {
f[0] = inff[0] = 1;
for (int i = 1; i < N; i++) {
f[i] = f[i - 1] * i % mod;
inff[i] = inff[i - 1] * imp(i, mod - 2, mod) % mod;
}
}
ll C(ll a, ll b) {
if (a == b)return 1;
if (a < b)return 0;
return f[a] * inff[b] % mod * inff[a - b] % mod;
}
int lucas_C(ll a, ll b, ll p)/
{
if (a < b) return 0;
int down = 1, up = 1;
for (int i = a, j = 1; j <= b; i--, j++)
{
up = up * i % p;
down = down * j % p;
}
return up * imp(down, p - 2, p) % p;//其实优化之处就是把原本每次for循环都要qmi求的逆元优化到最后一步返回值的时候来求解
}
int lucas(ll a, ll b, ll p)///O(plogn)p为取模质数
{
if (a < p && b < p) return lucas_C(a, b, p);//这一步从定义出发计算即可
return lucas_C(a % p, b % p, p) * lucas(a / p, b / p, p) % p;
//a%p后肯定是<p的,所以可以用C(),但a/p后不一定<p 所以用lucas继续递归
}
}Cal;
int main() {
Cal.init();
int n; cin >> n;
while (n--)
{
ll a, b, p; cin >> a >> b >> p;
cout << Cal.lucas(a, b, p) << endl;
}
}
边栏推荐
猜你喜欢
明明加了唯一索引,为什么还是产生重复数据?
初使jest 单元测试
CV+Deep Learning——网络架构Pytorch复现系列——classification(三:MobileNet,ShuffleNet)
IDLE development wordCount program (5)
DGIOT三千万电表集抄压测
自动化测试框架Pytest(二)——前后置处理
Uni-app develops WeChat applet using local images as background images
VS2013-调试汇编代码-生成asm文件-结构体内存布局-函数参数压栈-调用约定
二叉树 --- 堆
Uni-app开发微信小程序使用本地图片做背景图
随机推荐
90.(cesium之家)cesium高度监听事件
2022-08-01 网工进阶(二十四) STP进阶知识
initramfs与initrd的区别
VS2013-debug assembly code-generate asm file-structure memory layout-function parameter stack-calling convention
人工神经网络模型的特点,人工神经网络模型定义
S0:12345:respawn:/bin/start_getty 115200 ttyS0 vt102
Rust学习:6.4_复合类型之枚举
PLSQL学习第一天
时序动作定位 | ASM-Loc:弱监督时序动作定位的动作感知片段建模(CVPR 2022)
详解构建mock服务最方便的神器——Moco
每日一题,数组字符串的匹配问题
Introduction to the C language to realize bubble sort
nrm 使用详解
What is an MQTT gateway?What is the difference with traditional DTU?
调试ZYNQ的u-boot 2017.3 不能正常启动,记录调试过程
If the data of the oracle business table is added, deleted, or modified, will the index of the table write redo and undo?
概率分布及其应用
winget package manager
Is the write performance of raid5 faster than raid10?
软件测试面试题避雷(HR面试题)最常见的面试问题和技巧性答复