1. 程式人生 > >hdu 5651 (組合數學 + 階乘求逆元)

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 這題給你一個字串問你可以組成多少個不同的迴文字串
 迴文字串可以分成左右兩部分所以只要求每個字元個數的一半可以形成多少種不同的字串即可
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 <bits/stdc++.h>
using namespace std;
#define Mod 1000000007
#define ll long long
char s[1007];
map<char,int>m;
map<char,int>::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;i<len;i++)
        {
            m[s[i]]++;
        }
        int cnt=0;
        int val=0;
        ll res=1;
        for(it=m.begin();it!=m.end();it++)
        {
            int t=it->second;
            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;
}