xiaoxin juju needs help
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1262 Accepted Submission(s): 363
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?
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) .
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
这里很容易发现结果是(l/2)!/(a0!a1!.....a25!)其中ai为对应字母的个数的一半。但是这里要用逆元来求。由费马小定理, a(p-1)≡1(mod p)可知,两边同除以a既得a的逆元。加法中x的逆元为-x,乘法中a 的逆元为1/a。
#include <iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define maxn 1010
#define MOD 1000000007
using namespace std;
char s[maxn];
int r[50];
__int64 a[maxn];
void mul()
{
a[0]=1;
for(int i=1;i<=1000;++i)
a[i]=(i*a[i-1])%MOD;
}
__int64 pow_mul(__int64 a,__int64 b)
{
__int64 sum=1;
while(b)
{
if(b&1)
sum=sum*a%MOD;
a=a*a%MOD;
b=b/2;
}
return sum;
}
int main()
{
int t,l;
__int64 sum;
mul();
scanf("%d",&t);
while(t--)
{
scanf("%s",s);
l=strlen(s);
memset(r,0,sizeof(r));
sort(s,s+l);
if(l==1)
{
printf("1\n");
continue;
}
int k=1,c=1,num=0;
memset(r,0,sizeof(r));
for(int i=1;i<l;++i)
{
if(s[i]==s[i-1])
c++;
else
{
r[k++]=c/2;
if(c&1)
num++;
c=1;
}
}
r[k++]=c/2;
if(c&1)
num++;
if((num==1&&(l&1))||(num==0&&l%2==0))
{
sum=a[l/2];
for(int i=0;i<=26;++i)
sum=sum*pow_mul(a[r[i]],MOD-2)%MOD;
printf("%I64d\n",sum);
}
else
printf("0\n");
}
return 0;
}