[Luogu] 计算系数

https://www.luogu.org/problemnew/show/P1313#sub

Answer = a ^ n * b ^ m * C(k, min(n,  m))

这里用费马小定理求逆元来计算组合数

#include <bits/stdc++.h>

using namespace std;

#define Mod 10007

#define LL long long
#define gc getchar()

LL a, b, k, n, m, p;

inline LL read() {
    LL x = 0; char c = gc;
    while(c < '0' || c > '9') c = gc;
    while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = gc;
    return x;
}

LL Answer;

LL ksm(LL num, LL z) {
    LL ret = 1;
    while(z) {
        if(z & 1) ret = ret * num % Mod;
        num = num * num % Mod;
        z >>= 1;
    }
    return ret;
}

LL Calc() {
    LL down = 1, num = 1, k_p, k_, p_;
    for(int i = 1; i <= k; i ++) {
        num = num * i % Mod;
        if(i == p) p_ = num;
        if(i == k - p) k_p = num;
        if(i == k) k_ = num;
    }
    LL imp = p_ * k_p % Mod;
    down = ksm(imp, Mod - 2);
    return k_ * down % Mod;
}

int main() {
    a = read(); b = read(); k = read(); n = read(); m = read();
    a %= Mod; b %= Mod;
    p = min(n, m);
    a = ksm(a, n);
    b = ksm(b, m);
    LL C_k_p = Calc();
    LL Ans = (C_k_p * (a * b % Mod) % Mod);
    printf("%lld", Ans);
    return 0;
}
/*
1 1 5 2 3
 a ,b ,k ,n ,m
*/

 

posted @ 2018-03-27 14:08  xayata  阅读(172)  评论(0编辑  收藏  举报