xiaoxin juju needs help
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1844 Accepted Submission(s): 555
Problem Description
As we all known, xiaoxin is a brilliant coder. He knew palindromic strings when he was only a six grade student at elementry school.
This summer he was working at Tencent as an intern. One day his leader came to ask xiaoxin for help. His leader gave him a string and he wanted xiaoxin to generate palindromic strings for him. Once xiaoxin generates a different palindromic string, his leader will give him a watermelon candy. The problem is how many candies xiaoxin’s leader needs to buy?
Input
This problem has multi test cases. First line contains a single integer T(T≤20) which represents the number of test cases.
For each test case, there is a single line containing a string S(1≤length(S)≤1,000).
Output
For each test case, print an integer which is the number of watermelon candies xiaoxin’s leader needs to buy after mod 1,000,000,007.
Sample Input
3
aa
aabb
a
Sample Output
1
2
1
题意:给你一个字符串,求打乱字符后,有多少种回文串。
第一种解法 扩展欧几里得求乘法逆元
假如要求 (a / b) % p 条件 1.(b|a 即b整除a) 2.(gcd(b,p) == 1)
也就是求 = (a%p) * (1/b)%p
现在关键就在怎么求 (1/b)%p
设 x = (1/b)%p 我们称 x为 b在模p下的乘法逆元
(定理:b存在模p的乘法逆元的充要条件是gcd(b,p) = 1)
也就是 (b * x) % p = 1
上面的式子等价于 b*x + p*y = 1;
所以求得的 x就是 b模p下的乘法逆元了
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#include <cmath>
#include <stack>
#include <string>
#include <map>
#include <set>
#define pi acos(-1)
#define LL long long
#define ULL unsigned long long
#define inf 0x3f3f3f3f
#define INF 1e18
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
using namespace std;
typedef pair<int, int> P;
const int maxn = 1e5 + 5;
const int mod = 1e9 + 7;
LL exgcd(LL a, LL b, LL &x, LL &y)
{ // 求乘法逆元
if (b == 0){
x = 1; y = 0;
return a;
}
LL ans = exgcd(b, a%b, x, y);
LL t = x; x = y;
y = t - a/b*y;
return ans;
}
LL Fac(int sum)
{ // 计算阶乘
LL ans = 1;
for (int i = 2; i <= sum; i++)
ans = ans * i % mod;
return ans;
}
int alpha[30];
int main(void)
{
// freopen("C:\\Users\\wave\\Desktop\\NULL.exe\\NULL\\in.txt","r", stdin);
string s;
int T, i, cnt, n;
cin >> T;
while (T--)
{
cin >> s;
memset(alpha, 0, sizeof(alpha));
for (i = 0; i < s.length(); i++)
alpha[s[i] - 'a']++;
cnt = 0;
for (i = 0; i < 26; i++)
if (alpha[i] & 1) cnt++;
if (cnt > 1){
printf("0\n");
continue;
}
LL x, y, t, ans;
LL sum = s.length() / 2;
for (i = 0; i < 26; i++){
alpha[i] /= 2;
if (alpha[i]){
t = Fac(alpha[i]);
exgcd(t, mod, x, y);
if (x < 0) x += mod;
alpha[i] = x;
}
}
ans = Fac(sum);
for (i = 0; i < 26; i++){
if (alpha[i])
ans = ans * alpha[i] % mod;
}
cout << ans << endl;
}
return 0;
}
第二种解法 全排列
C(N,M)=C(N-1,M-1)+C(N-1,M)
参考1
参考2
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#include <cmath>
#include <stack>
#include <string>
#include <map>
#include <set>
#define pi acos(-1)
#define LL long long
#define ULL unsigned long long
#define inf 0x3f3f3f3f
#define INF 1e18
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
using namespace std;
typedef pair<int, int> P;
const int maxn = 1e5 + 5;
const int mod = 1e9 + 7;
LL C[505][505];
int alpha[30], a[30];
void init()
{
memset(C, 0, sizeof(C));
for (int i = 0; i < 505; i++){
C[i][1] = i;
C[i][0] = 1;
}
for (int i = 2; i < 505; i++){
for (int j = 2; j <= i; j++){
C[i][j] = (C[i-1][j] + C[i-1][j-1]) % mod;
}// C(n,m) = C(n-1,m) + C(n-1,m-1) 公式
}
}
int main(void)
{
// freopen("C:\\Users\\wave\\Desktop\\NULL.exe\\NULL\\in.txt","r", stdin);
init();
int T, i, j, cnt, n;
string s;
cin >> T;
while (T--)
{
cin >> s;
memset(alpha, 0, sizeof(alpha));
for (i = 0; i < s.length(); i++)
alpha[s[i] - 'a']++;
cnt = 0;
for (i = 0; i < 26; i++)
if (alpha[i] & 1) cnt++;
if (cnt > 1){
printf("0\n");
continue;
}
cnt = 0;
n = s.length() / 2; // 因为至多只有一个字母为奇数个 所以直接除2
for (i = 0; i < 26; i++){
if (alpha[i])
a[++cnt] = alpha[i] / 2;
}
LL ans = 1, m;
for (i = 1; i <= cnt; i++){
m = a[i];
ans = (ans * C[n][m]) % mod;
n -= m;
}
cout << ans << endl;
}
return 0;
}