bzoj2839 set count (volume + repellent composition)

Collection of Count

Memory limit: 128 MiB time limit: 1000 ms standard output
 
 

Title Description

A set of N elements there are 2 ^ N different subsets (containing empty set), is now set to be taken out in a number of these sets of 2 ^ N (at least one), such that the number of elements to their intersection K, Acquire the number of programs, the answer mode 1,000,000,007. (Oh ~ is a prime number)

Input Format

Line two integers N, K

Output Format

A behavioral answer.

Sample

Sample input

3 2

Sample Output

6

Data range and tips

Sample Description

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

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

the data shows

To 100% of the data, 1≤N≤1000000; 0≤K≤N;

A good number of topics (see the solution to a problem anyway, I just do it %%%% * ZJ https://www.cnblogs.com/zj75211/p/8029343.html) I have a la carte, all kinds of errors. Such an operation, all of a Cheddar dog to do it?

The idea is wonderful, the first step: First elected number C (n, k), Step Two: Then we have to do is to let the remaining set intersection is empty. Note that we have chosen is in addition to the remainder of the set C also choose what (plus this set on C)

Explain (I'm not very good language skills, by way of example to explain)

31 e.g.

Suppose we elected the first step by the number of A

While the second step we elected C, B

So we finally selected a collection of AB, AC

Another example is the second step we choose an empty set, C

So we finally elected a collection of A, AC

So we need to meet all the requirements of the situation is

I was pigeon verify the correctness of the. . . . . .

Then we have to do is find solutions

Set f [i] for the current set of at least the selected number of embodiments i Number

M is set ni (i.e., the number of remaining)

Ni for the remaining number may constitute 2 ^ (ni) sets

For each of these sets may be selected or not selected (i.e., 2 ^ (2 ^ (ni))) case

Not not chosen (for the empty set is also an option)

then

    for(ll i=0;i<=n;i++)    f[i]=C(n,i)*(2^(2^(n-i))-1);

Obviously we count more, then we need to inclusion and exclusion out of the inclusion-exclusion factor, first of all we found in all f [k + 1], we were overcharged C (k + 1, k) times

For example, we fixed the number four ABCD

We are overcharged once in each of the k + 1

ABC    D==ACD     B==ABD     C==BCD      A

Obtained for f [k + 1]: - C (k + 1, k)

  同理f[k+2]:+C(k+2,k+1)*C(k+1,k)==C(k+2,k)

And so on

get   

for(ll i=k;i<=n;i++)
    ans=(C(i,k)*f[i]%p*((i-k)&1?-1:1)+ans)%p;

 

In fact it is similar to a number of inclusion-exclusion

And this problem because the N <= 1000000

If the count C is now considered not going to work, taking into account a number of times to play table (factorial and factorial inverse), but for the inverse band log is still no response

So also linear inverse yuan

 

    ni [n] = meng (sabot [n], sinners 2 );
    for (i = ll scatter 1 ; i> = 1 ; i -) ni [i] = ni [i + 1 ] * (i + 1 )% p;

 

Understand what is actually inverse 1 /?

And 1 / (n + 1)! * (N + 1) is actually 1 / (n)! To seek out the inverse

 

Note two points

:for(ll i=1;i<=maxn;i++) ermi[i]=2*ermi[i-1]%(p-1);//2^ermi[i]%p!=2^ermi[i]%p调两节课

:for(ll i=0;i<=n;i++)又一节半课,实在是菜

: Ermi [0] and twenty minutes

In addition to the first one is a little bit wrong thinking, wrong other are low. Real should not

首先关于为什么%(p-1)而不是%(p)

To launch this fact, we must first know that 2 ^ (2 ^ i% p

How do set (2 ^ i) == (kφ (p) + t), on the original formula is (2 ^ k (φ (p)) * 2 ^ t)% p

According to Euler's theorem

2 ^ φ (p)% p I on the same 1, (2 ^ i) == (kφ (p) + t) t is equal to 2 ^ i% φ (p), and [Phi] (prime number) == p-1

Therefore% (p-1) instead of% p

The following is still also with my ugly debugging code

 1 #include<bits/stdc++.h>
 2 #define ll long long
 3 #define A 1100000
 4 #define maxn 1000010
 5 #define p 1000000007
 6 using namespace std;
 7 ll m,n,k,f[A],jie[A],ermi[A],ans,ni[A];
 8 inline ll read()
 9 {
10     ll f=1,x=0;char c=getchar();
11     while(!isdigit(c)){if(c=='-') f=-1;c=getchar();}
12     while(isdigit(c)){x=(x<<1)+(x<<3)+c-'0';c=getchar();}
13     return f*x;
14 }
15 ll meng(ll x,ll k)
16 {
17     ll ans=1;
18     for(;k;k>>=1,x=x*x%p)
19         if(k&1)
20             ans=x%p*ans%p;
21     return ans;
22 }
23 ll C(ll n,ll m)
24 {
25     if(m==0) return 1;
26     if(m>n) return 0;
27     else return (jie[n]*ni[m]%p*ni[n-m])%p;
28 }
29 void init()
30 {
31     n=read(),k=read();
32     m=n-k;
33     jie[0]=1;ni[0]=1;ermi[0]=1;
34     for(ll i=1;i<=maxn;i++)    ermi[i]=2*ermi[i-1]%(p-1);//2^ermi[i]%p!=2^ermi[i]%p
35     for(ll i=1;i<=n;i++)    jie[i]=jie[i-1]*i%p;
36     ni[n]=meng(jie[n],p-2);
37     for(ll i=n-1;i>=1;i--)ni[i]=ni[i+1]*(i+1)%p;
38     for(ll i=0;i<=n;i++)    f[i]=C(n,i)%p*(meng(2,ermi[n-i])%p-1)%p;
39 }
40 int main()
41 {
42 //    freopen("test.in","r",stdin);freopen("vio.out","w",stdout);
43     ans=0;
44     init();
45     for(ll i=k;i<=n;i++)
46     ans=(C(i,k)*f[i]%p*((i-k)&1?-1:1)+ans)%p;
47     cout<<(ans%p+p)%p<<endl;
48 }
View Code

And to take

 1 #include<bits/stdc++.h>
 2 int main() 
 3 {
 4     while(true) 
 5     {
 6         system("./mkd"),puts("mkd runs out");
 7         system("./std"),puts("std runs out");
 8         system("./vio"),puts("vio runs out");
 9         if(system("diff std.out vio.out")) while(true);
10         puts("");
11     }
12     return 0;
13 }
View Code

 

Ah, finished

Guess you like

Origin www.cnblogs.com/znsbc-13/p/11131680.html