容斥计算多重组合

http://oj.ecustacm.cn/problem.php?id=1253

题意:一副扑克牌52张(除去大小王),求从中拿13张牌的组合数(只考虑点数)。

1、解法:应用容斥原理:

该多重集合S = {4*1 , 4*2 , 4*3 , 4*4....... 4*10 , 4*11(J), 4*12(Q) , 4*13(K)}

定义性质集合P = {P1 , P2 , ..... P13}.Pi表示13组合中i的个数大于等于5的集合2

可以知道全集U为13种类型对象的多重集,每种元素均有无限重复数,的13组合为C (13 + 13 - 1 , 13).

à = U - (P1 + P2 + ... + P13) + (P1∩P2 + P1∩P3.....+P12∩P13).......

先选4张i其他牌张数不限 Pi = C (8 + 13 - 1 , 8)。

Pi ∩ Pj = C(3 + 13 - 1 , 3).

可以知道三个集合的交集为0.

à = C(13 + 13 - 1 , 13) - 13 * C(8+13-1 , 8) + C(13,2)C(3+13-1 , 3) = 3598180.

参考博客:https://www.cnblogs.com/vectors07/p/7976446.html

2、解法:dfs

枚举每一种牌的数量。

#include<bits/stdc++.h>
using namespace std;
int ans ;

void dfs(int num , int t)
{
    if(num == 13){//凑齐13张牌了
        ans++;
        return ;
    }
    if(t >= 13) return ;//dfs深度为13,一共13种牌
    for(int i = 0 ; i <= 4 ; i++)//枚举每一种牌的数量
        dfs(num+i , t+1);
}

int main()
{

    dfs(0 , 0);
    cout << ans << endl ;
    return 0;
}

 

原文链接: https://www.cnblogs.com/nonames/p/12248953.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;

也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬

    容斥计算多重组合

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/326524

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年3月1日 下午3:51
下一篇 2023年3月1日 下午3:52

相关推荐