求助,样例过不了啊!给关注
查看原帖
求助,样例过不了啊!给关注
494699
卷王慢即快楼主2023/6/29 13:22
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
//A^cnt[i]_{len/2-(cnt[1]+cnt[2]+...+cnt[i-1])/2}
#define mod 1000000007
int n, c = 0, x = 0, len = 0; ll total = 1, sum = 0;
char a[2007];
int cnt[30], tmp[30];
inline ll power(ll a, ll b) {
	ll ans = 1;
	while(b) {
		if(b & 1) ans = (ans * a) % mod;
		a = (a * a) % mod;
		b >>= 1;
	}
	return ans;
}
inline ll work(int l, int r) {
	
	ll res = 1;
	for(int i = l; i <= r; i++)
		res = res * i % mod;
	return res;
}
inline ll A(int n, int m) { //n 小,m 大 
	//n! / (n - m)!
	if(n == 0) return 1;
	ll res = work(m - n + 1, m);
	return res * power(total, mod - 2) % mod;
}
int main() {
	cin >> n >> (a + 1);
	len = strlen(a + 1);
	for(int i = 1; i <= n; i++) total = (total * i) % mod;
	for(int i = 1; i <= n; i++) cnt[a[i] - 'a' + 1]++;
	for(int i = 1; i <= 26; i++)
		if(cnt[i] % 2 == 1) c++, x = i;
	if(c >= 2) { cout << total; return 0; }
//	if(c == 1) cnt[x]--;
	for(int i = 1; i <= 26; i++)
		tmp[i] = cnt[i] / 2;
	ll ans = 1, left = len / 2;
	for(int i = 1; i <= 26; i++) {
		if(left == 0) break; //如果没有剩余空间 
		ans = ans * A(cnt[i] / 2, left) % mod * work(1, cnt[i]) % mod;
		left -= cnt[i] / 2;
	}
	cout << ((total - ans) % mod + mod) % mod;
	return 0;
}
2023/6/29 13:22
加载中...