HDU6442(二项式)

要点

  • 懒得打公式了,题解
  • 把题目要求的复杂公式化简成熟悉的东西,一是看穿前面加个\(n!\)化为\(C_n^i,i为奇数\);二是将奇数的条件去掉的数学技巧。
  • 形为\({(a + b\sqrt{B})}^n\)的快速幂
#include 
#include 
#include 
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 << '\n';
    }
}

转载于:https://www.cnblogs.com/AlphaWA/p/10884694.html

你可能感兴趣的:(HDU6442(二项式))