Set count: inclusion and exclusion

zkt big problem with the solution to a problem of God:   https://www.cnblogs.com/hzoi-DeepinC/articles/11102979.html

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

Line two integers N, K

Output

A behavioral answer.

Sample Input

3 2

Sample Output

6

Sample Explanation

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

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

Hint

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

Write problem solution not just to write the solution to a problem, talk focuses on ideas, I want to look directly at the Xiongtai positive solutions on their own jump to read.

Sorry my mind a little stupid, really did not think about the positive solution, a first idea is the n- 2 violence

70% of the algorithm

Although it is violence, but in fact I think the idea is not very good, because at first glance it is not on the right track, so the final amount of thinking is almost bigger than std

However, only 70 points, no one will sympathize with you how stupid QAQ

If the original problem in the meaning of the function F (n, k) represents that k is the number of n-element intersection programs

We can think of this two-step process: f (n, k) = C (n, k) * f (nk, 0)

The first step is to select the k C (n, k) from the n elements, and secondly to choose any set so that their intersection exactly k elements

Then we can determine, the collection was finally elected in each set contains the k elements, and the intersection of the remaining elements of the empty set: f (nk, 0)

Such as: {a, b, c} three elements, k = 1 when if you have selected a, then the set of possible are: {a}, {a, b}, {a, c}, {a , b, c}

You remove selected elements a, the rest is Ø, {b}, {c}, {b, c}, in these you optionally set, so that they are empty intersection

One possible method is selected from {a}, {a, b}, {a, b, c}, after removing a remaining Ø, {b}, {b, c}, their intersection is empty do

It looks "remaining elements of the intersection is empty," some of this issue would be better solution, that is, f (s, 0).

If that is the case, then the question becomes simplified f (n, k) = C (n, k) * f (nk, 0)

So that all problems can be dependent on f (nk, 0) be solved. We just requires a f (x, 0) in this series

I've been trying to find this law series, so I Shoumo a lot of points, got the form f (n, k) of

The law of the form looks like a lot, but f (x, 0) is really what the law: 2,10,218,64196 ...

I did not find. . . But I found the last line: All election law is 2 2 ^ the n- -1, because a total of 2 the n- sets, each set can be selected or not selected

But it can not not chosen, so to -1.

Now we can get f (x, 0), and if we know all the f (1,0), f (2,0 ) .... f (x-1,0), with the total number of minus Σ = l- I> X F (X, I) i.e. [Sigma I = l-> X C (X, XI) * F (XI, 0)

This is n- 2 , and the procedure of finding f (n, k), we can obtain all of the f (m, s) values (m <= n)

But most f values ​​are not used, they will be able to save memory burst, then run directly over the line, retaining f (x, 0) to

 1 #include<cstdio>
 2 #define int long long
 3 const long long mod=1000000007;
 4 long long fac[1000005],inv[1000005],invv[1000005],x,y,n,k,f0[1000005],f[1000005];
 5 long long pow(long long b,long long t,long long modd,long long ans=1){
 6     for(;t;t>>=1 , b = b * b% mode) f (t & 1 ) ans = ans * b% mode;
7      return ans;
8  }
 9  Signed main () {
 10      scanf ( " %% LLD LLD " , & n & k);
11      fac [ 0 ] = inv [ 0 ] = invv [ 0 ] = f0 [ 0 ] = invv [ 1 ] = inv [ 1 ] = fac [ 1 ] = 1 ; f0 [ 1 ] = 2 ;
12      for ( int i = 2 ; to <= n; ++ i) fac [i] = fac [to-1 ] * as% v, invv [i] = (- v / i * invv [v% i]) v%, INV [i] = inv [i- 1 ] * invv [i]% v;
13      for ( int i = 1 ; i <= n; ++ i) {
 14          f0 [i] = (pow ( 2 , pow ( 2 , i, received 1 ), v) - 1 + v)% v;
15          for ( int j = 1 ; j <= i; j ++) F [j] = fac [i] * inv [j] *% v inv [ij]% f0 * v [ij]% v, f0 [ i] = (f 0 [i] -f [j] + v)% v;
16          f [ 0 ] = f0 [i];
17      }
 18      printf ( "%lld\n",(f[k]+mod)%mod);
19 }
Stupid TLE70 Code

100% algorithm

There are too many redundant computation in the process of seeking f (n, k), and certainly not a positive solution, dead ~

I do not know if I have the great God that I consider to optimize the algorithm can not be saved, I abandon it

Then we take a look at the title bar: is the intersection is a subset of what can think of?

Uh. . The suspense a bit boring, because I had already said in the title. . Well inclusion and exclusion. . .

Why not think of the beginning ah ah ah ah (stupid, do not explain)

Every time I like to give this example: venn diagram four rings

This is the second time I make a rewarding map

Now consider n = 4, k = 2

Each ring are called {a, b, c, d}, which contains part of the intersection represented are those containing the element selected from the law

Then the public part of two circles, says their intersection contains two elements

Think about what you want to fill in each figure, it is not convenient to my scratch paper photographed. .

If only one ring contains the lightest portion of the part is called Level 1, common to both rings of the other ring without being called stage comprising two parts, so

Finally can be found on Level 1 parts are written on a level 218,2 10,3 2,4 grade level is 1.

p stage is the f (np, 0). . . Ugh. . . Seemingly no progress, still will not find ah

But we then look for other rules:

Redefining the series, level 1 represents a moiety containing specific elements (which may contain only not exactly), three stages are complete circle portion in FIG. 1, the entire area is a red stage 1

For each stage part 4, 1

For each stage portion 3, 2 + 1 = 3

For each stage part 2, 10 + 2 + 1 + 2 = 15

1 part for each stage is 10 + 1 + 2 + 2 + 2 + 10 + 10 + 218 = 255

For each stage portion s is 2 n-s- -1

We are asking f (4,2), try these parts to it that out of it

Level 1 of our seemingly less than

Obtained using 2-stage portion, a total of six stages section 2, is C (4,2), put them together, ans = 6 * 15 = 90

However, when level 2 plus parts we mistakenly added some class 3 section, subtract them

There are a few do? Each stage 2 comprises two parts are part of Level 3, then a total of 12, ans = ans-12 * 3 = 54

Yet again, we subtract the excess part of the four, plus the back how much?

When stage 2 plus part, each stage section comprising a 2-level section 4, a total of six stages added 2 part four portions 6

Save at three portions, each portion comprising a three-stage portion 4, a total of 12 part level 3 level 4 Save the part 12

Therefore, a total of six stages should be added back portion 4, ans = ans + 6 × 1 = 60 right!

Now there are only two questions: how demand 2 2 ^ the n- how to get a k-class section should be plus or minus a few times??

For the first question, obviously can not directly index modulo 1e9 + 7, the answer is wrong, then how should we do?

Fermat's Little Theorem: A the p-1- Ξp (the p-MOD) is the p-prime number

So index is modulo p-1, i.e. 1e9 + 6

For the second question is not so really want to, at least I think super trouble

Request f (n, k): Obviously k level needs to be added to section C (n, k) times, in order to study (I'm too lazy to play), to reduce the number of all so many times, and finally back to take easy

k Level: 1

k + level. 1: -C (NK,. 1) = -1 (NK)

k + Level 2: -C (nk, 2) + C (nk-1,1) * C (nk, 1) = - (nk) (nk-1) / 2 + (nk) (nk-1) = ( 1/2 ) * (NK) (NK-. 1)

k+3级:-C(n-k,3)+C(n-k-1,2)*C(n-k,1)+C(n-k-2,1)*(-C(n-k,2)+C(n-k-1,1)*C(n-k,1))=(-1/6+1/2-1/2)(n-k)(n-k-1)(n-k-2)=(-1/6)(n-k)(n-k-1)(n-k-2)

k + Level 4: -C (nk, 4) + ... = (- 1/24 + 1 / 6-1 / 4 + 1/6) (nk) (nk-1) (nk-2) (nk -3) = ( 1/24 ) (NK) (NK-. 1) (NK-2) (NK-. 3)

Coefficients in the red part is alternating positive and negative, and its absolute value is the reciprocal of the factorial, with the inverse solution

While the back part is obviously some even by using inverse factorial and factorial solutions. Code implements its own processing.

 1 #include<cstdio>
 2 const long long mod=1000000007;
 3 long long fac[1000005],inv[1000005],invv[1000005],x,y,n,k,ans,res;
 4 inline long long pow(long long b,long long t,long long modd,long long ans=1){
 5     for(;t;t>>=1,b=b*b%modd)if(t&1)ans=ans*b%modd;
 6     return ans;
 7 }
 8 inline long long c(long long b,long long t){return fac[b]*inv[t]%mod*inv[b-t]%mod;}
 9 signed main(){
10     scanf("%lld%lld",&n,&k);
11     fac[0]=inv[0]=invv[0]=invv[1]=inv[1]=fac[1]=1;
12     for(inti = 2 ; i <= n; ++ i) fac [i] = fac [i- 1 ] * as% v, invv [i] = (- v / i * invv [v% i]) v%, inv [i] = inv [i- 1 ] * invv [i]% v; res = c (n, k);
13      for ( int i = k; i <= n; ++ i)
 14          ans = (ANS + (((Ki) & 1 )? - 1 : 1 ) * res * inv [k]% v * fac [NK] % v * inv [Ni] v% * (pow ( 2 , pow ( 2 , Ni, MOD 1 ), v) - 1 ))% v;
15      printf ( " % lld \ n " , (ans + v)% v);
16 }
A small amount of code ^ _ ^

Guess you like

Origin www.cnblogs.com/hzoi-DeepinC/p/11102929.html