hdu 5651 (组合数学 + 阶乘求逆元)

xiaoxin juju needs help

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1149    Accepted Submission(s): 329


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
 

Source
BestCoder Round #77 (div.2)

这题给你一个字符串问你可以组成多少个不同的回文字符串
 回文字符串可以分成左右两部分所以只要求每个字符个数的一半可以形成多少种不同的字符串即可
int len=s.length();      res=fact(len/2)/(fact(a1)*fact(a1)...)%Mod
利用组合数学可以求解可以形成多少种不同方法
由于fact(len/2)/(fact(a1)*fact(a1)...)%Mod != fact(len/2)%Mod/f(act(a1)*fact(a1)...%Mod)%Mod  
但是乘法可以 既 :
(a/b)%Mod != (a%Mod/b%Mod)%Mod  (a*b%Mod = a%Mod*b%Mod )%Mod
所以这里就要想办法使除法变成乘法了 ---------逆元: a*b≡1(mod p)  a*b 和1关于模p同余  这里的b就是a的逆元
逆元有一个性质: 假如a的逆元为b    a/c = a*b 所以这里就可以将除法转化为乘法了
接下来问题便是如何求阶乘的逆元了  这里可以利用费马小定理:假如P是质数 a是整数并且a,p 互质  
a^p-1 ≡1(mod p)  
所以a的逆元便是a^p-2 次方  这里的p为10^9+7 利用快速幂求出500的阶乘的逆元,为什么要求500的呢?因为可以利用到一个递推式 (inv[i]=inv[i+1]*(i+1))%Mod  证明: 令f(n)为n的逆元  f(n!)=f((n-1)!*n)   => f(n!)/f(n) = f((n-1)!) => f(n!)*f(f(n)) = f((n-1)!)  阶乘的阶乘为本身 所以 f(n!)*n=f((n-1)!)


#include 
using namespace std;
#define Mod 1000000007
#define ll long long
char s[1007];
mapm;
map::iterator it;
ll fact[505];
ll inv[505];
void Fact()
{
    fact[0]=1;
    fact[1]=1;
    for(ll i=2;i<501;i++)
        fact[i]=(fact[i-1]*i)%Mod;
}

ll quickpow(ll n,ll p)
{
    ll sum=1;
    ll t=n;
    while(p)
    {
        if(p%2==1)
            sum=(sum*t)%Mod;
        p/=2;
        t=(t*t)%Mod;
    }
    return sum;
}

void Inv()
{
    inv[0]=1;
    inv[500]=quickpow(fact[500],Mod-2); //求阶乘逆元
    for(ll i=499;i>=1;i--)
        inv[i]=(inv[i+1]*(i+1))%Mod;
}
int main()
{
    Fact();
    Inv();
    int t;
    scanf("%d",&t);
    while(t--)
    {
        m.clear();
        scanf("%s",s);
        int len=strlen(s);
        for(int i=0;isecond;
            if(t%2==0)
            {
                res=(res*inv[t/2])%Mod;
            }
            else
            {
                val=t;
                cnt++;
            }
        }
        if(cnt>1) //判断奇数个数
        {
            puts("0");
            continue;
        }
        res=(res*inv[val/2])%Mod;
        printf("%lld\n",(fact[len/2]*res)%Mod);
    }
    return 0;
}




你可能感兴趣的:(acm,数学题)