Devu and flowers (inclusion and exclusion)

topic:

Devu there are N boxes, there is the i-th boxAi ramiflorous.

One and the same color in a flower box, flowers of different colors in different boxes.

Devu from these boxes selected M branch with flowers bouquet, seeking a total of how many kinds of programs.

If the number of flowers bouquet of two of each color are the same, I think the two flowers are the same program.

The results need to 10 . 9 + 7 only after the output of modulo.

Input Format

The first line contains two integers N and M.

The second line contains N space-separated integers representing A . 1 , A 2 , ... , A N .

Output Format

Output an integer representing the number of programs 10 . 9 + 7 result of modulo 109 + 7.

data range

1N201≤N≤20,
0M10140≤M≤1014,
0Ai10120≤Ai≤1012

Sample input:

3 5
1 3 2

Sample output:

3

Report problem solving:
did not start finding out how the inclusion-exclusion process, a bit later reference Gangster blog, the purpose of this question was to understand the true meaning!

First scenario if all the fi are no restrictions.
According So the answer is C (s, n + s- 1) an infinite number of combinations of multiple sets of 
circumstances we need to subtract the answer from unlawful in.
Not lawful is the case xi> fixi> fi's.
Is set AiAi xi> fixi> fi solution.
| Ai | = C (s- ( fi + 1), n + s-1- (fi + 1))
corresponds to our previously put it fi + 1fi + 1 elements.
Similarly | Ai⋂Aj | = C (s- ( fi + 1) - (fj + 1), n + s-1- (fi + 1) - (fj + 1))
according to the inclusion and exclusion and the answer is
C (s, n + s-1 ) -Σ1≤i≤n | Ai | + Σ1≤i <j≤n | Ai⋂Aj | -... ( omitted)

We can compress represent each state by state.
Pretreatment in advance of each state needs to put several elements.
The last time scan statistics answer.
The time complexity of O (2nn)

 

ac Code:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 
 5 const int mod=1e9+7;
 6 int n,lim;
 7 ll s,ans;
 8 ll f[30],inv[30],sum[1100000];
 9 int num[1100000];
10 ll comb(ll n,ll m)
11 {//求解C(n,m)   
12     if(m<0)
13         return 0;
14     ll ret=1;
15     for(ll i=m+1;i<=n;i++)
16         ret=ret*(i%mod)%mod;//n!/m!
17     for(int i=1;i<=n-m;i++)
18         ret=ret*inv[i]%mod;//(n-m)!
19     return ret;
20 }
21 
22 int main()
23 {
24     cin>>n>>s;//n个盒子,挑选s朵花 
25     for(intI = 0 ; I <n; I ++ )
 26 is          CIN >> F [I]; // number of input flowers of n in a box 
27      INV [ . 1 ] = . 1 ;
 28      for ( int I = 2 ; I <= n + . 1 ; I ++ )
 29          INV [I] = (INV [I MOD%] * (MOD-MOD / I)) MOD%; // magical play table solving inverse element 
30      Lim = ( . 1 << n-) - . 1 ; / / binary enumeration, ease-repellent capacity 
31 is      for ( int I = 0 ; I <= Lim; I ++ )
 32      {
 33 is          for( Int J = 0 ; J <n-; J ++ )
 34 is          {
 35              IF (I & ( . 1 << J))
 36              {
 37 [                  SUM [I] = F + [J] + . 1 ; // advance put fi + 1 th element 
38 is                  NUM [I] ++; // tag at the number of occurrences 
39              }
 40          }
 41      } // has been handling all states, from 1,2, -> n- 
42 is      for ( int I = 0 ; I <= Lim; I ++ )
 43 is      {
 44 is          IF(num[i]%2)
45             ans=(ans-comb(s+n-1-sum[i],s-sum[i])+mod)%mod;
46         else
47             ans=(ans+comb(s+n-1-sum[i],s-sum[i]))%mod;
48     }
49     cout<<ans<<endl;
50 } 

 



Guess you like

Origin www.cnblogs.com/Spring-Onion/p/11310470.html