xiaoxin juju needs help(逆元)

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\leq 20)$ which represents the number of test cases. 
For each test case, there is a single line containing a string $S(1 \leq length(S) \leq 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


题目大意就是求给出的字符串可以组成几个不同的回文串


 知识点:

长度为n的字符串的全排列为n!,设字符串中第i钟字母重复的次数为ai,则没有重复的字母排列次数为

n! / (a1! * a2! * a3! * .........)

因为题目求的是回文串,所以将字符串长度除2,重复次数除2,再用逆元啊、求解即可


注意:

奇数长度的字符串中仅有一种字母的重复次数为奇数

偶数长度的字符串中所有字母的重复次数都为偶数


#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <map>
#include <vector>
#include <set>
#include <queue>
#include<iostream>
#include<string>


#define rep(i, l, r) for(int i=l; i<=r; i++)
#define dow(i, l, r) for(int i=l; i>=r; i--)
#define clr(x, c) memset(x, c, sizeof(x))
#define travel(x) for(edge *p=fir[x]; p; p=p->n)
#define all(x) (x).begin,(x).end
#define pb push_back
#define fi first
#define se second
#define l(x) Left[x]
#define r(x) Right[x]


using namespace std;


typedef long long ll;
typedef pair<int, int> Pii;
typedef long double ld;
typedef unsigned long long ull;


inline int read()
{
    int x = 0;
    bool f = 0;
    char ch = getchar();
    while (ch<'0' || '9'<ch) f |= ch == '-', ch = getchar();
    while ('0' <= ch && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
    return f ? -x : x;
}


#define mod 1000000007


ll quick_mod(ll a,ll n)
{
    ll t = 1;
    while(n)
    {
        if(n & 1) t = t*a%mod;
        a = a*a%mod;
        n >>= 1;
    }
    return t;
}


int main()
{
    string s;
    int t;
    cin>>t;
    ll num[35];
    while(t--)
    {
        memset(num,0,sizeof(num));
        cin>>s;
        int len=s.size();
        for(int i=0; i<len; i++)
        {
            num[s[i]-'a']++;
        }
        int flag=0;
        if(len%2==1)
        {
            for(int i=0; i<26; i++)
            {
                if(num[i]!=0)
                {
                    if(num[i]%2==1)
                        flag++;
                    num[i]=num[i]/2;
                }
            }
            if(flag>1)
            {
                printf("0\n");
                continue;
            }
        }
        else if(len%2==0)
        {
            flag=0;
            for(int i=0; i<26; i++)
            {
                if(num[i]!=0)
                {
                    if(num[i]%2==1)
                        flag++;
                    num[i]=num[i]/2;
                }
            }
            if(flag!=0)
            {
                printf("0\n");
                continue;
            }
        }
        len/=2;
        ll sum1=1;
        for(int i=1;i<=len;i++)
            sum1=sum1*i%mod;
        ll sum2;
        for(int i=0;i<26;i++)
        {
            if(num[i]!=0)
            {
                sum2=1;
                for(int j=1;j<=num[i];j++)
                {
                    sum2=sum2*j%mod;
                }
                sum1=sum1*quick_mod(sum2%mod,mod-2)%mod;
            }
        }
        printf("%I64d\n",sum1);
    }
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值