[生成函数][DP] Codeforces 891 E. Lust

Solution S o l u t i o n

设最后是这个样子 a=(aibi) a = ( a i − b i ) ,可以通过数学归纳得到

res=iaii(aibi) r e s = ∏ i a i − ∏ i ( a i − b i )
那么考虑组合意义:
Eibi=ki(aibi)==k!ibi!1nkibi=ki(aibi)k!nkibi=ki(aibi)bi! E ∑ i b i = k ∏ i ( a i − b i ) = k ! ∏ i b i ! 1 n k ∑ ∑ i b i = k ∏ i ( a i − b i ) = k ! n k ∑ ∑ i b i = k ∏ i ( a i − b i ) b i !
考虑生成函数。后面那个式子就等于
[xk]ij0xj(aij)j!=[xk]enxi(aix) [ x k ] ∏ i ∑ j ≥ 0 x j ( a i − j ) j ! = [ x k ] e n x ∏ i ( a i − x )
后面的东西可以暴力乘法 O(n2) O ( n 2 ) 算出来。
再考虑 enx e n x 的泰勒展开,算出 xk x k 的系数即可。
这个题从头到尾应该都是可以FFT优化的??

#include 
#define show(x) cerr << #x << " = " << x << endl
using namespace std;

typedef long long ll;
typedef pair<int, int> pairs;
typedef vector<int> poly;
const int MOD = 1000000007;
const int N = 5050;

inline char get(void) {
    static char buf[100000], *S = buf, *T = buf;
    if (S == T) {
        T = (S = buf) + fread(buf, 1, 100000, stdin);
        if (S == T) return EOF;
    }
    return *S++;
}
template<typename T>
inline void read(T &x) {
    static char c; x = 0; int sgn = 0;
    for (c = get(); c < '0' || c > '9'; c = get()) if (c == '-') sgn = 1;
    for (; c >= '0' && c <= '9'; c = get()) x = x * 10 + c - '0';
    if (sgn) x = -x;
}

int a[N];
int n, k, ans;
poly c;

inline void add(int &x, int a) {
    x = (x + a >= MOD) ? x + a - MOD : x + a;
}
inline void sub(int &x, int a) {
    x = (x < a) ? x - a + MOD : x - a;
}
inline poly operator *(poly a, poly b) {
    static poly c; c.clear();
    c.resize(a.size() + b.size() - 1);
    for (int i = 0; i < a.size(); i++)
        for (int j = 0; j < b.size(); j++)
            add(c[i + j], (ll)a[i] * b[j] % MOD);
    return c;
}
inline int pwr(int a, int b) {
    if (b < 0) b += MOD - 1;
    int c = 1;
    while (b != 0) {
        if (b & 1) c = (ll)c * a % MOD;
        b >>= 1; a = (ll)a * a % MOD;
    }
    return c;
}
inline int A(int n, int m) {
    int res = 1;
    for (int i = 0; i < m; i++)
        res = (ll)res * (n - i) % MOD;
    return res;
}
inline poly base(int x) {
    static poly c; c.clear();
    c.emplace_back(x);
    c.emplace_back(MOD - 1);
    return c;
}
inline void watch(poly x) {
    cerr << "{ ";
    for (int u: x) cerr << u << ", ";
    cerr << " }" << endl;;
}

int main(void) {
    freopen("1.in", "r", stdin);
    freopen("1.out", "w", stdout);
    read(n); read(k);
    c.emplace_back(1);
    for (int i = 1; i <= n; i++) {
        read(a[i]);
        c = c * base(a[i]);
        // watch(c);
    }
    for (int j = 1; j < c.size(); j++)
        sub(ans, (ll)c[j] * A(k, j) % MOD * pwr(n, -j) % MOD);
    cout << ans << endl;
    return 0;
}

你可能感兴趣的:(动态规划,生成函数)