zoj 3556 How Many Sets I - 容斥原理

/***
容斥原理
考虑总的种数减去集合不为空的种数。
首先是总的种数:每个集合有2^n个子集,总共k的集合
所以总数就是2^(n*k)

再考虑集合不为空的情况,
首先考虑最后交集只有一个元素,这种情况总共有(2^(n-1))^k种
交集有两个元素(2^(n-2))^k种
交集有t个元素(2^(n-t))^k种
根据容斥原理,总共集合不为空的情况有
C(n,1) * (2^(n-1))^k - C(n,2) * (2^(n-2))^k + ... + C(n,t) * (-1)^(t+1) * (2^(n-t))^k
 = p
所以最后答案就是 2^(n*k) - p = (2^k - 1 )^n
***/
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
#include <map>
#include <set>
#include <cmath>
#include <algorithm>
#include <functional>
using namespace std;

typedef long long LL;
const int M = 1000000007;
LL n,k;

LL pow_mod(LL a,LL b,LL c){
    LL res =1;
    while(b){
        if(b&1) res = res*a%c;
        b >>= 1;
        a = a*a%c;
    }
    return res;
}

int main(){
    while(cin >> n >> k){
        LL ans = pow_mod(pow_mod(2,k,M)-1,n,M);
        cout << ans << endl;
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值