样例输出3 0求条
查看原帖
样例输出3 0求条
1753388
xuan_1楼主2025/8/4 20:14
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
namespace xuan_1{
	const int N = 1e6;
	typedef long long ll;
	int cnt;
	ll n, m, p;
	ll a[N], c[N];
	ll power(ll a, ll b, const ll p = LLONG_MAX){
		ll ans = 1;
		while (b){
			if (b & 1){
				ans = ans * a % p;
			}a = a * a % p;
			b >>= 1;
		}return ans;
	}
	ll fac(ll n, ll m, const ll p){
		if (!n){ 
			return 1;
		}ll ans = 1;
		for (int i = 1; i < p; i++){
			if (i % m){
				ans = ans * i % p;
			}
		}ans = power(ans, n / p, p);
		for (int i = 1; i <= n % p; i++){
			if (i % m){
				ans = ans * i % p;
			}
		}return ans * fac(n / m, m, p) % p;
	}
	ll exgcd(ll a, ll b, ll &x, ll &y){
		if (!b){
			x = 1, y = 0;
			return a;
		}ll xx, yy, g = exgcd(b, a % b, xx, yy);
		x = yy;
		y = xx - a / b * yy;
		return g;
	}
	ll inv(const ll a, const ll p){
		ll x, y;
		exgcd(a, p, x, y);
		return (x % p + p) % p;
	}
	ll C(ll n, ll m, ll p, ll pk){
		if (n < m){
			return 0;
		}ll f1 = fac(n, p, pk), f2 = fac(m, p, pk), f3 = fac(n - m, p, pk), cnt = 0;
		for (ll i = n; i; i /= p){
			cnt += i / p;
		}for (ll i = m; i; i /= p){
			cnt -= i / p;
		}for (ll i = n - m; i; i /= p){
			cnt -= i / p;
		}return f1 * inv(f2, pk) % pk * inv(f3, pk) % pk * power(p, cnt, pk) % pk;
	}
	ll CRT(){
		ll M = 1, ans = 0;
		for (int i = 0; i < cnt; i++){
			M *= c[i];
		}for (int i = 0; i < cnt; i++){
			ans = (ans + a[i] * (M / c[i]) % M * inv(M / c[i], c[i]) % M) % M;
		}return ans;
	}
	ll exlucas(const ll n, const ll m, ll p){
		ll tmp = sqrt(p);
		for (int i = 2; p > 1 && i <= tmp; i++){
			ll tmp = 1;
			while (p % i == 0){
				p /= i, tmp *= i;
			}if (tmp > 1){
				a[cnt] = C(n, m, i, tmp);
				c[cnt++] = tmp;
			}
		}if (p > 1){
			a[cnt] = C(n, m, p, p);
			c[cnt++] = p;
		}return CRT();
	}
	int main_252(){
		ios::sync_with_stdio(false);
		scanf("%lld%lld%lld", &n, &m, &p);
		printf("%lld\n", exlucas(n + m, n, p));
		return 0;
	}
}
int t;
int main(){
	scanf("%d", &t);
	while (t--){
		xuan_1 :: main_252();
	}return 0;
}
2025/8/4 20:14
加载中...