[BZOJ2839]: set the count

Topic Portal


Subject description:

A N set elements have 2 N different subsets (containing empty set), this now in 2 N number of elements in the set of a plurality of sets taken in (at least one), so that their intersection is K , obtaining the number of law programs, the answer mode 1,000,000,007 . (Oh ~ is a prime number)


Input formats:

Line two integers N, K .


Output formats:

A behavioral answer.


Example:

Sample input:

3 2

Sample output:

6


Sample Description:

Suppose the original set is {A, B, C}

Solution for the condition is satisfied: {AB, the ABC}, {the AC, the ABC}, {the BC, the ABC}, {AB}, the AC {}, {} the BC


 

Data range and tips:

For 100% of the data, 1≤N≤1 . 6 ; 0≤k≤n ;


answer:

I do not know why this question is like to see a combination of mathematics and the inclusion-exclusion, do not ask me why.

A pair of good - since you're so cute ...... then ......

In fact, I feel what this pair of questions ...... what a collection, and if you can not think of one second what algorithm, go out into the inclusion and exclusion think about it, is basically a personal feeling of inclusion and exclusion.

Anyway, you will earn inclusion and exclusion, not that you do not lose ( since you do not, no, no, you're the most divine ).

Words Reformed turn (in fact, is not just nonsense pair of ~):

The first is a combination of mathematics, since he is very dedicated to go out into combinatorics go think about it.

Obviously, the problem can be transformed into the first n selected number in the k th, and then select any number of collections in the rest of the numbers, so that their intersection is empty can.

This time the answer is: ANS = (a bunch of numbers, I do not know how much) × C (the n-, k) .

Then, "I do not know how much," the number seems hard to find, they will make up 2 (NK) sets, then you have to choose from among these collections, so that they do not intersect, then I guess you have money, then you can make it first ran, himself frozen a few hundred years maybe it can be considered complete? Hard to say ~

Then obviously you can not do this, how to do?

I say there is inclusion and exclusion.

Then we consider their intersection is i (i = [k, the n-], i∈N *) .

From n elements selected from i-th element, the remaining ni elements may be comprised of 2 (ni) different collections, then these sets there are 2 (2 ^ (ni)) -1 combinations, -1 is because we can not do nothing option.

Program Number is the C (n-, I) × C (I, K) × ( 2 (2 ^ (Ni)) -1 ) .

This time we should consider our great inclusion and exclusion, and Qi Jia can even cut.


 

Code time:

#include<bits/stdc++.h>
using namespace std;
long long n,k;
long long ans;
long long jc[1000005],inv[1000005];
long long qpow(long long x,long long y,long long mod)//快速幂
{
	long long ans=1;
	while(y)
	{
		if(y%2)ans=(ans*x)%mod;
		y>>=1;
		x=(x*x)%mod;
	}
	return ans;
}
void pre_work()//预处理
{
	jc[0]=1;
	for(long long i=1;i<=1000000;i++)
		jc[i]=(jc[i-1]*i)%1000000007;
	inv[1000000]=qpow(jc[1000000],1000000005,1000000007);
	for(long long i=999999;i>=0;i--)
		inv[i]=(inv[i+1]*(i+1))%1000000007;
 }
long long cm (long long n, long long m) {return jc [n] * inv [m]% 1000000007 * inv [nm]% 1000000007;} // find C 
int main () 
{ 
	pre_work (); 
	Scanf ( " % LLD% LLD ", & n-, & K); 
	int In Flag =. 1; // for Qijia even Save 
	for (Long Long I = K; I <= n-; I ++) 
	{ 
		ANS = (ANS + (((cm & lt (n- , i) * cm (i, k))% 1000000007 * (qpow (2, qpow (2, ni, 1000000006), 1000000007) -1))% 1000000007) * flag% 1000000007)% 1000000007; // formula, Note that inclusion and exclusion 
		In Flag-FLAG =; 
	} 
	COUT << (ANS + 1000000007)% 1000000007; // as the final step may be a subtraction, so attention again to + mod MOD% 
	return 0; 
}

 

rp ++

Guess you like

Origin www.cnblogs.com/wzc521/p/11123353.html