题意
给定一个长度为 的整数数组 ,求有多少种排列 满足其前缀和数组是回文串。答案对 取模。
思路
设前缀和数组为 ,回文条件为 。
设 。
令 ,有 ,所以 只能是所有数加起来的和。也就是 。
不难发现,所有满足 的数对 ,有 。
若 为偶数,中间位置 必须满足 。结合回文性质可推出该位置值为 。
因此问题等价于:将数组元素分配到 个位置上,使得:
- 位置 的值恰好为 。
- 关于 对称位置的元素之和为 。
- 为偶数时,正中间位置为 。
从数组中取出一个值为 的元素放在第一位,有 种选法, 为偶数时再取一个 放中间(有 种选法)。剩余元素中,每个正值 的个数必须等于 的个数, 的个数必须为偶数,否则答案为零。
设剩余元素中值 有 对,值 有 个。总配度数
考虑带标号排列的计数:
- 将 组元素分配到 个对称位置对上,有 种可能。
- 对于每个组内,把正放前面还是负放前面,有 种。
- 对于每个相同的 放到不同组有 种。
- 对于零的对,把 个 放入 对个位置,由于 无方向性,所以贡献为 (即从 个零中选配对的方案数)。
最终式子
code
#include<bits/stdc++.h>
#define LL long long
using namespace std;
const int mod = 998244353;
const int N = 200005;
int fac[N],ifac[N],poww[N];
int qpow(int a,int b){
int res = 1;
while(b){
if(b & 1) res = 1LL * res * a % mod;
a = 1LL * a * a % mod;
b >>= 1;
}
return res;
}
void pre(){
fac[0] = ifac[0] = poww[0] = 1;
for(int i = 1;i < N;i++){
fac[i] = 1LL * fac[i - 1] * i % mod;
poww[i] = 1LL * poww[i - 1] * 2 % mod;
}
ifac[N - 1] = qpow(fac[N - 1],mod - 2);
for(int i = N - 2;i >= 1;i--){
ifac[i] = 1LL * ifac[i + 1] *(i + 1) % mod;
}
}
int t;
int a[N];
signed main(){
ios::sync_with_stdio(0);
cin.tie(nullptr);
pre();
cin >> t;
while(t--){
int n;
cin >> n;
map<int,int> cnt;
long long sum = 0;
for(int i = 1;i <= n;i++){
cin >> a[i];
cnt[a[i]]++;
sum += a[i];
}
if(cnt.find((int)sum) == cnt.end() || cnt[(int)sum] == 0){
cout << 0 << "\n";
continue;
}
int cntt = cnt[(int)sum];
cnt[(int)sum]--;
if(cnt[(int)sum] == 0) cnt.erase((int)sum);
int mid = 1;
if(n % 2 == 0){
if(cnt.find(0) == cnt.end() || cnt[0] == 0){
cout << 0 << "\n";
continue;
}
mid = cnt[0];
cnt[0]--;
if(cnt[0] == 0) cnt.erase(0);
}
bool ok = true;
int k = 0;
set<int>tot;
for(auto it = cnt.begin();it != cnt.end();++it){
tot.insert(abs(it->first));
}
long long ans = 1LL * cntt % mod * mid % mod;
int c0 = 0;
for(auto it = tot.begin();it != tot.end();++it){
int v = *it;
if(v == 0){
int z = cnt[0];
if(z % 2 != 0){
ok = false;break;
}
c0 = z / 2;
k += c0;
} else{
int cp = cnt.count(v) ? cnt[v] : 0;
int cn = cnt.count(-v) ? cnt[-v] : 0;
if(cp != cn){
ok = false;
break;
}
k += cp;
ans = 1LL * ans * poww[cp] % mod * fac[cp] % mod;
}
}
if(!ok){
cout << 0 << "\n";
continue;
}
ans = 1LL * ans * fac[k] % mod;
if(c0 > 0){
ans = 1LL * ans * fac[2 * c0] % mod * ifac[c0] % mod;
}
cout << ans << "\n";
}
return 0;
}