HDU6442(二项式)

要点

  • 懒得打公式了,题解
  • 把题目要求的复杂公式化简成熟悉的东西,一是看穿前面加个(n!)化为(C_n^i,i为奇数);二是将奇数的条件去掉的数学技巧。
  • 形为({(a + bsqrt{B})}^n)的快速幂
#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;

typedef long long ll;
int T;
ll A, B, n, p;
ll prime[1000005];

void Init() {
	for (int i = 1; i <= 1000; i++)
		for (int j = i * i; j <= (int)1e6; j += i * i)
			prime[j] = i;
}

ll ksm(ll a, ll b, ll n, ll mod) {
	ll res = 1, ret = 0;
	for (; n; n >>= 1) {
		if (n & 1) {
			ll t = res, p = ret;
			res = (t * a % mod + p * b % mod * B % mod) % mod;
			ret = (t * b % mod + p * a % mod) % mod;
		}
		ll tmp = a;
		a = (a * a % mod + b * b % mod * B % mod) % mod;
		b = 2LL * tmp % mod * b % mod;
	}
	return ret;
}

int main() {
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	Init();
	for (cin >> T; T--;) {
		cin >> A >> B >> n >> p;
		ll u = prime[B];
		B = B / (u * u);
		cout << 1 << " " << ksm(A, u, n, p) << " " << B << '
';
	}
}
原文地址:https://www.cnblogs.com/AlphaWA/p/10884694.html