组合数3 - lucas a、b较大的组合数

C_{a}^{b} = C_{a/p}^{b/p}* C_{a\%p}^{b\%p}

复杂度\log _{p}N *p *logp,约等于plogp

#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl '\n'
 
using namespace std;
 
typedef pair<int, int> PII;
typedef long long ll;
typedef long double ld;
 
ll a, b, mod;
 
ll qmi(ll a, ll k)
{
	ll res = 1;
	while(k)
	{
		if(k & 1)res = res * a % mod;
		a = a * a % mod;
		k >>= 1;
	}
	return res;
}
 
int C(int a, int b)
{
	ll res = 1, y = 1;
	for(int i = a, j = 1; j <= b; i --, j ++)
	{
		res = res * i % mod;
		y = y * j % mod;
	}
	res = res * qmi(y, mod - 2) % mod;
	return res;
}
 
int lucas(ll a, ll b)
{
	if(a < mod && b < mod)return C(a, b);
	return (ll)lucas(a / mod, b / mod) * lucas(a % mod, b % mod) % mod;
}
 
inline void solve()
{
	cin >> a >> b >> mod;
	cout << lucas(a, b) << endl;
}
 
int main()
{
	IOS
	int _;
	cin >> _;
	while(_ --)
	{
		solve();
	}
	
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值