【玄2关】关于逆元&组合数取模
  • 板块学术版
  • 楼主miyachn
  • 当前回复4
  • 已保存回复4
  • 发布时间2025/7/30 10:10
  • 上次更新2025/7/30 10:15:30
查看原帖
【玄2关】关于逆元&组合数取模
1295276
miyachn楼主2025/7/30 10:10

原题是让求组合数取模的值,T<=100, 1<=m<=n<=100000,本人写的代码超时了,想知道哪里出了问题。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll; 
const ll N=100009;
ll power(ll x,ll p,ll mod){
	ll res=1;
	while(p){
		if(p&1) (res*=x)%=mod;
		(x*=x)%=mod;
		p>>=1;
	}
	return res;
}
ll getinv(ll x,ll mod){
	return power(x,mod-2,mod);
}
ll T,n,m,p,f[N],inv[N];
void build(){
	f[0]=1;
	for(int i=1;i<N;i++) f[i]=(f[i-1]*i)%p;
	for(int i=0;i<N;i++) inv[i]=getinv(f[i],p);
}
ll C(ll a,ll b){
	return f[a]*inv[b]%p*inv[a-b]%p;
}
int main(){
	cin>>T;
	while(T--){
		cin>>n>>m>>p;
		build();
		cout<<C(n,m)<<" ";
	}
	return 0;
}

2025/7/30 10:10
加载中...