求找UB
查看原帖
求找UB
200044
JS_TZ_ZHR楼主2020/11/13 15:10

RT

#include<bits/stdc++.h>
#define ll long long
#define int long long
using namespace std;
ll n,m,p,a[1005],b[1005],cnt,sum[1005],M=1,t[1005],q[1005];
ll fpow(ll x,ll y,ll z) {
	ll res=1;
	while(y) {
		if(y&1)res=(res*x)%z;
		x=(x*x)%z;
		y>>=1;
	}
	return res;
}
ll Mul(ll x,ll y,ll z) {
	ll res=0;
	while(y) {
		if(y&1)res=(res+x)%z;
		x=(x+x)%z;
		y>>=1;
	}
	return res;
}
ll inv(ll x,ll y) {
	if(x==1)return 1;
	else return ((y-y/x)*inv(y%x,y))%y;
}
ll CRT() {
	for(int i=1;i<=cnt;i++)a[i]%=b[i];
	ll res=0;
	for(int i=1; i<=cnt; i++)
		M*=b[i];
	for(int i=1; i<=cnt; i++)
		sum[i]=M/b[i],t[i]=inv(sum[i]%b[i],b[i]);
	for(int i=1; i<=cnt; i++)
		res=(res+Mul(sum[i],Mul(t[i],a[i],M),M))%M;
	return res;
}
ll fac(ll x,ll y,ll mod) {
	if(!x)return 1;
	ll res=1;
	for(int i=1; i<=mod; i++)
		if(i%y)res=(res*i)%mod;
	res=fpow(res,x/mod,mod);
	for(int i=x/mod*mod+1;i<=x;i++)
		if(i%y)res=(res*i)%mod;
	return (res*fac(x/y,y,mod))%mod;
}
ll exLucas(ll x,ll y,ll z,ll mod) {
	ll tot=0,res=1;
	for(int i=x; i; i/=z)tot+=i/z;
	for(int i=y; i; i/=z)tot-=i/z;
	for(int i=x-y; i; i/=z)tot-=i/z;
	res=(fpow(z,tot,mod)*fac(x,z,mod))%mod;
	res=(res*inv(fac(y,z,mod),mod))%mod;
	res=(res*inv(fac(x-y,z,mod),mod))%mod;
	return res;
}
ll exlucas(ll x,ll y) {
	for(int i=1; i<=cnt; i++)a[i]=exLucas(x,y,q[i],b[i]);
	return CRT();
}
signed main() {
	cin>>n>>m>>p;
	for(int i=2; i*i<=p; i++) {
		if(p%i==0) {
			b[++cnt]=1,q[cnt]=i;
			while(p%i==0)b[cnt]*=i,p/=i;
		}
	}
	if(p!=1)b[++cnt]=q[cnt]=p;
	cout<<exlucas(n,m)<<endl;
}
2020/11/13 15:10
加载中...