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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值