hdu5651

链接:hdu5651

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

    题解

  • 如果奇数大于1肯定无解,然后所有字母出现次数除2,按照不相异的元素的全排列公式进行计算,然后用乘法逆元做一下,
  • 乘法逆元用拓展欧几里得做
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#define rep(i,x,y) for(register int i=x;i<=y;++i)
#define repd(i,x,y) for(register int i=x;i>=y;--i)
#define ll long long
using namespace std;
const int N=1e5;
const ll p=1000000007;
char str[N];
int a[30],T,len,cnt,flag;

int exgcd(ll a,ll b,ll &d,ll &x,ll &y){
if(b==0){x=1; y=0; d=a;}
else {exgcd(b,a%b,d,x,y); ll t=x;x=y;y=t-a/b*x;}
}

ll inv(ll a,ll p){
ll d,x,y;
exgcd(a,p,d,x,y);
return d==1?(x%p+p)%p:-1;
}
void readin(){
cin>>str;
len=strlen(str);cnt=flag=0;
memset(a,0,sizeof(a));
rep(i,0,len-1)a[str[i]-'a']++;
rep(i,0,29){
if(a[i]&1)flag++;
if(a[i]){
a[i]>>=1;
cnt+=a[i];
}
}
}

ll sum(ll X){
ll tp=1;
rep(i,2,X)tp=tp*i%p;
return tp;
}

int main(){
ios::sync_with_stdio(false);
cin>>T;
while(T--){
readin();
if(flag>1){
cout<<0<<endl;
continue;
}
rep(i,0,26)if(a[i]!=0){
a[i]=sum(a[i]);
a[i]=inv(a[i],p);
}
ll ans=sum(cnt);
rep(i,0,26)if(a[i])ans=ans*a[i]%p;
cout<<ans<<endl;
}
return 0;
}