考虑枚举 i, j, 那么 x \ge j 的 x 的 w 可以是 1 或 2,除了 i 需要是 2 以外其余都可以随便填,再令 k 为最小的满足条件的 k,则 [i + 1, k - 1] 都得是 2 不然不会考虑到 k,最后一段的贡献是 2^{n - k + 1},没有 k 满足时是 1,所以不用特判。
再看前面,任选 1, 2 使得总花费是 m - 2,范德蒙德卷积得方案为 \dbinom{j - 2}{m - i - 1}。前后乘一起即可。
时间复杂度 \mathcal{O}(\sum n^2)。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
// typedef __int128 i128;
typedef pair<int, int> pii;
const int N = 5e3 + 10, mod = 998244353;
template<typename T>
void dbg(const T &t) { cout << t << endl; }
template<typename Type, typename... Types>
void dbg(const Type& arg, const Types&... args) {
cout << arg << ' ';
dbg(args...);
}
int C[N][N], pw[N];
namespace Loop1st {
int n, m, a[N];
void add(ll &x, ll y) { x += y; if (x >= mod) x -= mod; }
void main() {
cin >> n >> m;
ll ans = 0;
for (int i = 1; i <= n; i++) cin >> a[i];
sort(a + 1, a + n + 1, greater<int>());
for (int i = 1; i < n; i++) {
int k = 1;
for (int j = n; j > i; j--) if (a[j] < a[i] && 2 * a[j] > a[i]) {
while (k <= n && a[j] + a[k] >= a[i]) k++;
if (m - i - 1 > j - 2 || m - 1 < i || j < 2) continue;
add(ans, (ll)C[j - 2][m - i - 1] * pw[n - k + 1] % mod);
}
}
cout << (pw[n] - ans + mod) % mod << '\n';
}
}
int main() {
// freopen("data.in", "r", stdin);
// freopen("data.out", "w", stdout);
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
pw[0] = 1;
C[0][0] = 1;
for (int i = 1; i < N; i++) {
pw[i] = pw[i - 1] * 2 % mod;
C[i][0] = C[i][i] = 1;
for (int j = 1; j < i; j++) {
C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % mod;
}
}
int c, T; cin >> c >> T;
while (T--) Loop1st::main();
return 0;
}