当前位置:网站首页>组合数模板
组合数模板
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;
}
}
边栏推荐
猜你喜欢

90.(cesium之家)cesium高度监听事件

day16--The use of the packet capture tool Charles

90. (cesium house) cesium height monitoring events

iwemeta元宇宙:阿里首任COO:如何打造销售铁军

张驰课堂:老板会武术,谁也挡不住!六西格玛培训的魅力

数据库公共字段自动填充

PLSQL学习第一天

Nude speech - lying flat - brushing questions - big factory (several tips for Android interviews)

明明加了唯一索引,为什么还是产生重复数据?

【MySQL】SQL语句
随机推荐
如何设计神经网络结构,神经网络设计与实现
【转】探秘钉钉的即时消息服务DTIM
SQL SERVER 数据库,表的数据发生增删改,该表的索引会在ldf日志中记录吗?
If the data of the oracle business table is added, deleted, or modified, will the index of the table write redo and undo?
uni 小程序腾讯地图polygon背景透明度
placeholder 1
phpstudy starts automatically
进程管理(动态的)
短视频同城流量宣传小魔推有何优势?如何给实体商家带来销量?
浅谈C语言实现冒泡排序
The sixteenth day & the basic operation of charles
iwemeta元宇宙:阿里首任COO:如何打造销售铁军
明明加了唯一索引,为什么还是产生重复数据?
941 · 滑动拼图
Fiddler(八) - 抓取手机APP的流量-插件Fiddler Orchestra Beta安装&配置
同步锁synchronized追本溯源
【NeRF】原始论文解读
每日一题,数组字符串的匹配问题
WooCommerce installation and rest api usage
phpstudy开机自启