大意:给定字符串$s$, 保证长度为偶数, 给定q个询问, 每次询问给定两个位置$x$,$y$, 可以任意交换字符, 要求所有字符$s[x],s[y]$在同一半边, 剩余所有同种字符在同一半边的方案数
注意到询问数虽然是1e5, 但有效的只有$52^2$, 考虑预处理出$52^2$后O(1)回答.
假设两半的字符种类已经定好, 那么种类数就为$2\frac{(n/2)!^2}{\prod\limits_{i} (c_i!)}$.
考虑如何分配每一半的字符, 若对$s[x],s[y]$所在的半边$DP$的话复杂度是$O(52^3n)$超时, 所以可以考虑另一半边的分配, 也就是说求出不包含$s[x],s[y]$并且长度$n/2$的方案数, 可以用可逆背包$O(52^2n)$求出
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <string.h>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
//head
const int N = 1e5+10;
int n, m, q;
char s[N];
int a[N], b[N], c[N];
int f[N], F[60][N], ans[60][60];
ll fac[N];
int main() {
scanf("%s", s+1);
n = strlen(s+1), m = n/2;
REP(i,1,n) a[i]=b[i]=s[i];
sort(b+1,b+1+n),*b=unique(b+1,b+1+n)-b-1;
REP(i,1,n) ++c[a[i]=lower_bound(b+1,b+1+*b,a[i])-b];
REP(i,1,*b) if (c[i]>m) {
scanf("%d", &q);
while (q--) puts("0");
return 0;
}
fac[0]=fac[1]=1;
REP(i,1,n) fac[i]=fac[i-1]*i%P;
ll C = 2*fac[m]%P*fac[m]%P;
REP(i,1,*b) C = C*inv(fac[c[i]])%P;
f[0] = 1;
REP(i,1,*b) PER(j,c[i],m) (f[j]+=f[j-c[i]])%=P;
REP(i,1,*b) {
REP(j,0,c[i]-1) F[i][j]=f[j];
REP(j,c[i],m) {
F[i][j]=f[j]-F[i][j-c[i]];
if (F[i][j]<0) F[i][j]+=P;
}
ans[i][i] = F[i][m];
}
REP(i,1,*b) REP(ii,i+1,*b) {
REP(j,0,c[ii]-1) f[j]=F[i][j];
REP(j,c[ii],m) {
f[j]=F[i][j]-f[j-c[ii]];
if (f[j]<0) f[j]+=P;
}
ans[i][ii] = f[m];
}
scanf("%d", &q);
REP(i,1,q) {
int x, y;
scanf("%d%d", &x, &y);
x = a[x], y = a[y];
if (x>y) swap(x,y);
printf("%d\n",int(C*ans[x][y]%P));
}
}