社区讨论

lucas求调,爆re了

P3807【模板】卢卡斯定理 / Lucas 定理参与者 1已保存回复 0

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
0 条
当前快照
1 份
快照标识符
@lywr3qcj
此快照首次捕获于
2024/07/22 16:54
2 年前
此快照最后确认于
2024/07/22 17:43
2 年前
查看原帖
董晓老师那抄的板子,爆re了求调
CPP
// Problem: P3807 【模板】卢卡斯定理/Lucas 定理
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P3807
// Memory Limit: 125 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e5+10;
ll f[N],g[N];
int t;
ll ksm(ll a,ll b,ll p)
{
	ll ans=1,base=a;
	while(b)
	{
		if(b&1)ans*=base,ans%=p;
		base*=base,base%=p;b>>=1;
	}
	return ans;
}
void init(ll p)
{
	f[0]=g[0]=1;
	for(int i=1;i<=p;i++)
	{
		f[i]=f[i-1]*i%p;
		g[i]=g[i-1]*ksm(i,p-2,p)%p;
	}
}
ll getC(ll n,ll m,ll p)
{
	return f[n]*g[m]*g[n-m]%p;
}
ll lucas(ll n,ll m,ll p)
{
	if(m==0)return 1;
	return lucas(n/p,m/p,p)*getC(n%p,m%p,p)%p;
}
void solve()
{
	ll n,m,p;
	cin>>n>>m>>p;
	init(p);
	cout<<lucas(n+m,m,p);
}
int main()
{
	ios::sync_with_stdio(false);cin.tie(0),cout.tie(0);
	cin>>t;
	while(t--){solve();cout<<'\n';}return 0;
}

回复

0 条回复,欢迎继续交流。

正在加载回复...