loj6485. LJJ 学二项式定理
题意
给出\(n, s, a_0, a_1, a_2, a_3\),求
\[\sum_{i = 0} ^ n \binom{n}{i} s ^ i a_{i \mod 4}
\]
对\(998244353\)取模的值。
题解
本来像强行用xiaomange
的方法艹过去,but failed。
大概猜到和原根有关,但是思考也只是停留在特殊的情况下,没有进行更有效的思考。
正解是单位根反演。
大概就是在推式子的过程中发现突破口的
\[\begin{aligned}
origin
& = \sum_{k = 0} ^ 3 a_k \sum_{i = 0} ^ n [4 | i + 4 - k] \binom{n}{i} s ^ i \\
\end{aligned}
\]
注意到在对\(998244353\)取模下有4次单位根\(\omega_4\),又由于有恒等式
\[[n | m] = \frac{1}{n} \sum_{i = 0} ^ {n - 1} \omega_n ^ {im}
\]
则
\[\begin{aligned}
origin
& = \sum_{k = 0} ^ 3 a_k \sum_{i = 0} ^ n \binom{n}{i} s ^ i \sum_{j = 0} ^ 3 \omega_4 ^ {j(i + 4 - k)} \\
& = \sum_{k = 0} ^ 3 a_k \sum_{j = 0} ^ 3 \sum_{i = 0} ^ n \binom{n}{i} s ^ i \omega_4 ^ {j(i + 4 - k)} \\
& = \sum_{k = 0} ^ 3 a_k \sum_{j = 0} ^ 3 \omega_4 ^ {j(4 - k)} \sum_{i = 0} ^ n \binom{n}{i} s ^ i \omega_4 ^ {ji} \\
& = \sum_{k = 0} ^ 3 a_k \sum_{j = 0} ^ 3 \omega_4 ^ {j(4 - k)} (s \omega_4 ^ j) ^ n \\
\end{aligned}
\]
再直接做即可。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod = 998244353, g = 3;
ll power (ll a, ll b) {
ll ret = 1;
for ( ; b; b >>= 1, a = a * a % mod) {
if (b & 1) {
ret = ret * a % mod;
}
}
return ret;
}
int T; ll w, n, s, a, p, q, ans;
int main () {
w = power(g, (mod - 1) / 4);
for (scanf("%d", &T); T; --T) {
scanf("%lld%lld", &n, &s), ans = 0;
for (int i = 0; i < 4; ++i) {
scanf("%lld", &a), p = s, q = 1;
for (int j = 0; j < 4; ++j) {
ans += a * power(q, 4 - i) % mod * power(p + 1, n);
ans %= mod;
p = p * w % mod;
q = q * w % mod;
}
}
ans = ans * power(4, mod - 2) % mod;
printf("%lld\n", ans);
}
return 0;
}