HzuWHF


私信TA

用户名:I7I08I9047

访问量:76446

签 名:

我RUN了

等  级
排  名 18
经  验 20463
参赛次数 13
文章发表 127
年  龄 3
在职情况 学生
学  校 贺州学院
专  业

  自我简介:

        卢卡斯定理求组合数  C ( N, M ) % P,当 P 是素数时适用,这里可以当一个模板用  HUD 3037


#include<bits/stdc++.h>
using namespace std;

typedef long long LL;
const int SIZE = 100231;
LL F[SIZE];

void init(LL Mod) {
	F[0] = 1;
	for (int pos = 1; pos <= Mod; pos++)
		F[pos] = F[pos - 1] * pos % Mod;
}

LL inv(LL A, LL M) {
	return A == 1 ? 1 : inv(M % A, M) * (M - M / A) % M;
}

LL Lucas(LL N, LL M, LL Mod) {
	init(Mod);
	LL ans = 1;
	while (N && M) {
		LL A = N % Mod, B = M % Mod;
		if (A < B)
			return 0;
		ans = ans * F[A] % Mod * inv(F[B] * F[A - B] % Mod, Mod) % Mod;
		N /= Mod; M /= Mod;
	}
	return ans;
}

int main() {
    ios::sync_with_stdio(false);
	int quan; cin >> quan;
	LL N, M, Mod;
	while (quan--) {
		cin >> N >> M >> Mod;
		cout << Lucas(N + M, N, Mod) << endl;
	}
}


 

0.0分

2 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区