-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathD_Even_String.cpp
More file actions
84 lines (66 loc) · 1.5 KB
/
D_Even_String.cpp
File metadata and controls
84 lines (66 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <bits/stdc++.h>
using namespace std;
// First you make it work, then you can always make it beautiful
#define int long long
const int mod = 998244353;
const int N = 5e5 + 5;
int fact[N];
int ifact[N];
int modpowr(int a, int b) {
int res = 1;
while(b) {
if(b & 1) res = (res * a) % mod;
a = (a * a) % mod;
b >>= 1;
}
return res;
}
void compute() {
fact[0] = 1;
for(int i=1;i<N;i++) fact[i] = (i * fact[i-1]) % mod;
ifact[N-1] = modpowr(fact[N-1], mod - 2);
for(int i=N-2;i>=0;i--) {
ifact[i] = ((i+1) * ifact[i+1]) % mod;
}
}
void solve() {
vector<int> freq(26);
int sum = 0;
for(int i=0;i<26;i++) {
cin >> freq[i];
sum += freq[i];
}
int even = sum/2;
int odd = sum - even;
vector<int> dp(even + 1), dp2(even + 1);
dp2[0] = 1;
for(int i=25;i>=0;i--) {
if(freq[i] == 0) continue;
for(int j=0;j<=even;j++) {
dp[j] = dp2[j];
if(j - freq[i] >= 0) {
dp[j] = (dp[j] + dp2[j-freq[i]]) % mod;
}
}
dp2 = dp;
}
int ways = dp[even];
int ans = (fact[even] * fact[odd]) % mod;
(ans *= ways) %= mod;
for(int i=0;i<26;i++) {
(ans *= ifact[freq[i]]) %= mod;
}
cout << ans << "\n";
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
compute();
int _;
cin >> _;
while (_-->0) {
solve();
}
return 0;
}