arc106 D - Powers
作者:互联网
题意:
给定整数 \(K\) 和长为 \(n\) 的数组 \(a[]\),对每个 \(1\le X\le K\) 计算
\[\left( \sum_{i=1}^{n-1}\sum_{j=i+1}^n (a_i+a_j)^X \right) \mod 998244353 \]\(2\le n\le 1e5, 1\le K\le 300, 1\le a_i\le 1e8\)
思路:
设
\[res=\sum_{i=1}^{n}\sum_{j=1}^n (a_i+a_j)^X \]那么答案就是 \(\left(res-\sum\limits_{i=1}^n (2a_i)^X \right)/2\)
展开 \(res\),
\[\begin{aligned} res &= \sum_{i=1}^n\sum_{j=1}^n\sum_{k=0}^X C_X^k a_i^k a_j^{X-k} \\ &= \sum_{i=1}^n\sum_{j=1}^n\sum_{k=0}^X X!\frac{a_i^k}{k!} \frac{a_j^{X-k}}{(X-k)!} \\ &= X! \sum_{k=0}^X \left(\sum_{i=1}^n \frac{a_i^k}{k!}\right) \left(\sum_{j=1}^n \frac{a_j^{X-k}}{(X-k)!}\right) \end{aligned} \]\(O(nX)\) 预处理 \(f(K)=\sum\limits_{i=1}^n \frac{a_i^K}{K!}\),则可以 \(O(X)\) 得到每个 \(X\),总复杂度 \(O(NK+K^2)\)
为了写代码方便,写成
\[sum(K)=\sum_{i=1}^n a_i^K \\ res = \sum_{k=0}^X C_X^k \sum_{i=1}^n a_i^k \sum_{i=1}^n a_i^{X-k} = \sum_{k=0}^X C_X^k sum(k) sum(X-k) \\ ans = \left(res-2^X\sum_{i=1}^n a_i^X \right)/2 = \left(res-2^Xsum(X) \right)/2 \]ll n, K, a[N], sum[303];
void sol() {
cin >> n >> K;
for(int i = 1; i <= n; i++) cin >> a[i];
for(int i = 1; i <= n; i++) { //预处理sum[]
ll apow = 1; //a[i]^k
for(int k = 0; k <= K; k++)
add(sum[k], apow), mul(apow, a[i]);
}
for(int X = 1; X <= K; X++) {
ll ans = -qmi(2, X) * sum[X] % mod;
for(int k = 0; k <= X; k++)
add(ans, sum[k] * sum[X-k] % mod * C(X, k) % mod);
mul(ans, qmi(2, mod-2)); //除以2
cout << ans << endl;
}
}
标签:le,frac,arc106,sum,right,res,Powers,left 来源: https://www.cnblogs.com/wushansinger/p/16347262.html