hdu-5651 xiaoxin juju needs help(数学+gcd约分求阶乘)

题目链接:

xiaoxin juju needs help

Time Limit: 2000/1000 MS (Java/Others)    

Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 491    Accepted Submission(s): 142


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(T20) which represents the number of test cases.
For each test case, there is a single line containing a string S(1length(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
题意:问给的字符串能形成多少个不同的回文串;
思路:因为回文,所以都是取一半计算,n=len/2,a[i]=a[i]/2;(a[i]为第i个字母出现的次数)ans=n!/(a[i]!*a[j]!*a[k]!*..)(i,j,k...都是大于0的a[]);麻烦之处就是阶乘会爆掉,除法%mod我又不会,所以想了这个gcd暴力约分的方法;
AC代码:
#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
#include <algorithm>
using namespace std;
const int mod=1e9+7;
char str[1010];
int a[27],b[1010];
int gcd(int x,int y)
{
    if(y==0)return x;
    return gcd(y,x%y);
}
queue<int>qu;
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        while(!qu.empty())qu.pop();
        memset(a,0,sizeof(a));
        scanf("%s",str);
        int len=strlen(str);
        for(int i=0;i<len;i++)
        {
            a[str[i]-'a']++;
        }
        int flag=0;
        long long ans=1;
        for(int i=0;i<26;i++)
        {
            if(a[i]%2)
            {
                flag++;
            }
            a[i]=a[i]/2;
        }
        for(int i=1;i<=len/2;i++)
        {
            b[i]=(long long)i;
        }
        //memset(vis,0,sizeof(vis));
        for(int i=0;i<26;i++)
        {
            if(a[i]>1)
            {
                for(int j=2;j<=a[i];j++)
                {
                   qu.push(j);
                }
            }
        }
        while(!qu.empty())
        {
            int x=qu.front();
            qu.pop();
            for(int i=2;i<=len/2;i++)
            {
                if(b[i]%x==0)
                {
                    b[i]/=x;
                    break;
                }
                else
                {
                    int y=gcd(b[i],x);
                    if(y>1)
                    {
                        b[i]/=y;
                        x/=y;
                        if(x>1)qu.push(x);
                        break;
                    }
                }
            }

        }
        for(int j=1;j<=len/2;j++)
        {
            ans*=(long long)b[j];
            ans%=mod;
        }
        if(flag>1)cout<<"0"<<"\n";
        else cout<<ans<<"\n";

    }

    return 0;
}

 

posted @ 2016-03-27 00:24  LittlePointer  阅读(449)  评论(0编辑  收藏  举报