HDU - 5651 xiaoxin juju needs help 组合数学 多重集的全排列
给定一个字符串,问其能通过重新排列生成多少不同的回文串。
显然只需要考虑一边就好了,考虑一边实际上就是多重集的全排列问题。
ll f[505]; void init() { f[0] = 1ll; for (int i = 1; i <= 500; i++) f[i] = f[i - 1] * i % MOD; } char s[1005]; map<char, int> mp; int main() { int T = readint(); init(); while (T--) { mp.clear(); scanf("%s", s); int len = strlen(s); for (int i = 0; i < len; i++) mp[s[i]]++; int cnt = 0; vector<int> v; for (auto it = mp.begin(); it != mp.end(); it++) if (it->second & 1) cnt++,v.push_back((it ->second - 1) >> 1); else v.push_back(it->second / 2); if (cnt > 1) { puts("0"); continue; } ll sum = 0; for (int i = 0; i < v.size(); i++) sum += v[i]; ll res = f[sum]; for (int i = 0; i < v.size(); i++) res *= ksm(f[v[i]], MOD - 2, MOD), res %= MOD; Put(res); puts(""); } }