当前位置:网站首页>组合数模板
组合数模板
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;
}
}
边栏推荐
- 自动化测试框架Pytest(二)——前后置处理
- 2022-08-01 网工进阶(二十三) VLAN高级技术-VLAN聚合、MUX VLAN
- 机器人控制器编程实践指导书旧版-实践二 传感器(模拟量)
- DGIOT 30 million meters set pressure reading
- SCS【2】单细胞转录组 之 cellranger
- Quickly enter the current date and time
- 英国国家卫生服务遭受攻击,系统出现大面积故障
- Discussion on Chinese Fuzzy Retrieval in Databases
- navicat for mysql 连接时报错:1251-Client does not support authentication protocol requested by server
- 阿里云数据库 RDS SQL Server 版的服务器绑定域名www.cxsdkt.cn.的呢?
猜你喜欢

Rust学习:6.3_复合类型之元组

机器人控制器编程实践指导书旧版-实践一 LED灯(数字量)

2022-08-01 网工进阶(二十四) STP进阶知识

WooCommerce 安装和 rest api 使用

VS2013-调试汇编代码-生成asm文件-结构体内存布局-函数参数压栈-调用约定

自动化测试框架Pytest(二)——前后置处理

协同工具满足70%-90%的工作需求,成为企业香饽饽

Based on STC8G2K64S4 single-chip microcomputer to display analog photosensitive analog value through OLED screen

ATH10 sensor reads temperature and humidity

Nude speech - lying flat - brushing questions - big factory (several tips for Android interviews)
随机推荐
PLSQL学习第四天
英国国家卫生服务遭受攻击,系统出现大面积故障
941 · Sliding Puzzles
30条实用MySQL优化法则
Go-Excelize API源码阅读(十一)—— GetActiveSheetIndex()
WooCommerce installation and rest api usage
快速输入当前日期与时间
基于STC8G2K64S4单片机通过OLED屏幕显示模拟量光敏模拟值
Uni-app develops WeChat applet using local images as background images
Quickly enter the current date and time
添加spark的相关依赖和打包插件(第六弹)
day16--The use of the packet capture tool Charles
uni 小程序腾讯地图polygon背景透明度
占位占位1
调试ZYNQ的u-boot 2017.3 不能正常启动,记录调试过程
Introduction to the delta method
pytest之parametrize参数化
Class Notes (7) (1) - #647. Find the root and the child (root)
mysql数据库月增长量问题
winget包管理器