ZOJ 3556 How Many Sets I
How Many Sets I
64-bit integer IO format: %lld Java class name: Main
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
Source
Author
从子集中选k个的有序组合个数(子集可重复被选中)有 $(2^n)^k=2^{n\times k}$;
故总数为$S=2^{n\times k}$;
$设S(x)为k个集合的有序组合的个数,这些集合都包含至少一个x。$
$S(x_1 \& x_2)为k个集合的有序组合的个数,这些集合都包含至少一个x_1和x_2$
$S(x_1\& x_2\& x_3...x_k)为k个集合的有序组合的个数,这些集合都包含至少一个x_1,x_2...x_k。$
$而 S(x)=(2^{n-1})^k =2^{(n-1)\times k};$
$S(x_1 \& x_2)=(2^{n-2})^k=2^{(n-2)\times k};$
$S(x_1\& x_2 \&...\& x_i)=(2^{n-i})^k=2^{(n-i)\times k};$
由容斥原理知,我们要得到的就是
$S-(n,1)\times S(x) + (n,2)\times (S(x_1\& x_2)-(n,3)\times (S(x_1\& x_2\&x_3)+....(-1)^i\times (n,i)\times S(x_1\& x_2\&...x_i)...(-1)^n\times (n,n)\times S(x_1\& x_2...\& x_n);$
$化简得ans=(2^K-1)^N;$
1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long LL; 4 LL mod = 1000000007,n,k; 5 LL quickPow(LL base,LL index){ 6 LL ret = 1; 7 while(index){ 8 if(index&1) ret = ret*base%mod; 9 index >>= 1; 10 base = base*base%mod; 11 } 12 return ret; 13 } 14 int main(){ 15 while(~scanf("%lld%lld",&n,&k)) 16 printf("%lld\n",quickPow(((quickPow(2,k) - 1)%mod + mod)%mod,n)); 17 return 0; 18 }