zoj 3841 Cards(组合数学)

题目链接

Cards Time Limit: 2 Seconds      Memory Limit: 65536 KB

EdwardGy has a poker (52 cards excluding jokers). One day, he saw some cards was lined up on the table. He wanted to know how many ways he can place all of the rest cards in a row with a lower lexicographic order than the line of the cards which were already on the table.

The lexicographic order of the cards is A < 2 < 3 < 4 < 5 < 6 < 7 < 8 < 9 < 10 < J < Q < K, and the colors should be ignored. If the cards already on the table is

AA22334455667788991010JJKKK

EdwardGy have only five ways:

AA22334455667788991010JJQQQQK
AA22334455667788991010JJQQQKQ
AA22334455667788991010JJQQKQQ
AA22334455667788991010JJQKQQQ
AA22334455667788991010JJKQQQQ

Input

There are multiple test cases. Each test case has only one line, a valid string indicating the cards on the table.

Output

For each test cases, output the remainder of the number of ways modulo by 109+7.

Sample Input

AA22334455667788991010JJKKK

Sample output

5

题意:有52张扑克牌(除开了大王、小王)。现在桌子上摆了一些牌,问用剩下的所有牌摆放,有多少种摆法的字典序比桌子上的低?两种摆法不同的定义为:至少有一个位置的牌不同。

题解:设剩余的牌的个数为x,剩余的每种牌的个数为a1,a2,....a13。枚举求第i个位置比桌子上的小,摆的是j,前i-1个位置与桌子上的相同的摆法数,为(n-i)!/(a1!* a2!*.....(aj-1)!*..a13!)。注意特判剩余的牌的数目小于桌上的牌数的情况。

代码如下:

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<string>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<stdlib.h>
#include<vector>
#define inff 0x3fffffff
#define nn 110000
#define mod 1000000007
typedef long long LL;
const LL inf64=inff*(LL)inff;
using namespace std;
char s[110];
LL jx[110];
map<char,int>ma;
void init()
{
    jx[0]=1;
    int i;
    for(i=1;i<=55;i++)
    {
        jx[i]=(jx[i-1]*i)%mod;
    }
    ma['A']=1;
    for(i=2;i<=9;i++)
        ma[i+'0']=i;
    ma['1']=10;
    ma['J']=11;
    ma['Q']=12;
    ma['K']=13;
}
int num[15];
LL po(LL x,int y)
{
    LL re=1;
    LL tem=x;
    while(y)
    {
        if(y&1)
        {
            re=(re*tem)%mod;
        }
        tem=(tem*tem)%mod;
        y>>=1;
    }
    return re;
}
int main()
{
    int i,j;
    init();
    while(scanf("%s",s)!=EOF)
    {
        int ls=strlen(s);
        int gs=52;
        for(i=1;i<=13;i++)
            num[i]=4;
        for(i=0;i<ls;i++)
        {
            num[ma[s[i]]]--;
            gs--;
            if(s[i]=='1')
                i++;
        }
        LL ans=0;
        LL tem;
        LL fc;
        int fuck=gs;
        bool ok=true;
        for(i=0;i<ls;i++)
        {
            if(gs==0)
                break;
            for(j=1;j<=13;j++)
            {
                if(num[j]>0&&j<ma[s[i]])
                {
                    tem=jx[gs-1];
                    fc=jx[num[j]-1];
                    for(int k=1;k<=13;k++)
                    {
                        if(k==j)
                            continue;
                        fc=(fc*jx[num[k]])%mod;
                    }
                    tem=(tem*po(fc,mod-2))%mod;
                    ans=(ans+tem)%mod;
                }
            }
            if(num[ma[s[i]]]==0)
            {
                ok=false;
                break;
            }
            num[ma[s[i]]]--;
            gs--;
            if(s[i]=='1')
                i++;
        }
        if(ok&&fuck<52-fuck)
            ans=(ans+1)%mod;
        printf("%lld\n",ans);
    }
    return 0;
}


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