How Many Sets

来源:互联网 发布:atom php常用插件 编辑:程序博客网 时间:2024/05/20 14:15
Give a set S, |S| = n, then how many ordered set group (S1, S2, ..., Sk) satisfies S1 ∩ S2 ∩ ... ∩ Sk = ∅. (Si is a subset of S, (1 <= i <= k))

Input
The input contains multiple cases, each case have 2 integers in one line represent n and k(1 <= k <= n <= 231-1), proceed to the end of the file.

Output
Output the total number mod 1000000007.

Sample Input
1 1
2 2
Sample Output
1

9

题意:

容斥原理+快速幂

这个题目输出一开始用%I64d输出PE了两发,然后改成%lld就过了,跟一血就差30秒

个数为n的集合的子集有2^n个,从中选出K个使得他们的交集为空的个数。
由于集合可以重复被选,所以总的数目是2^(kn)
然后选中的集合都包含x这个数的数目是c(n,1)*2^(n-1)k
选中的集合包含x1,x2的数目是c(n,2)*2^(n-2)k
……
所以满足的集合的个数res=2^kn-c(n,1)*2^(n-1)k+c(n,2)*2(n-2)k-……
推出的公式为(2^k-1)^n
注意 容斥原理原理:
两个集合的容斥关系公式:A∪B =|A∪B| = |A|+|B| - |A∩B |(∩:重合的部分)
三个集合的容斥关系公式:|A∪B∪C| = |A|+|B|+|C| - |A∩B| - |B∩C| - |C∩A| + |A∩B∩C|
代码:
#include<cstdio>#include<algorithm>#include<cstring>using namespace std;typedef long long ll;const ll N=1000000007;
ll pow(ll x,ll n){    ll b=1;    while(n)    {        if(n&1) b=b*x%N;        x=x*x%N;        n>>=1;    }    return b;}int main(){    ll k,n;    while(scanf("%lld%lld",&n,&k)!=EOF)    {        ll ans=pow(2,k)-1;        ans=pow(ans,n);        printf("%lld\n",ans);    }    return 0;}


原创粉丝点击