Hdu -- 5155 Harry And Magic Box (combination number + rejection principle)
Harry And Magic Box
Time Limit:1000 MS
Memory Limit:32768KB
64bit IO Format:% I64d & % I64u Submit Status Appoint description: System Crawler)
Description
One day, Harry got a magical box. the box is made of n * m grids. there are sparking jewel in some grids. but the top and bottom of the box is locked by amazing magic, so Harry can't see the inside from the top or bottom. however, four sides of the box are transparent, so Harry can see the inside from the four sides. seeing from the left of the box, Harry finds each row is shining (it means each row has at least one jewel ). and seeing from the front of the box, each column is shining (it means each column has at least one jewel ). harry wants to know how many kinds of jewel's distribution are there in the box. and the answer may be too large, you shoshould output the answer mod 1000000007.
Input
There are several test cases.
For each test case, there are two integers n and m indicating the size of the box. $0 \ leq n, m \ leq 50 $.
Output
For each test case, just output one line that contains an integer indicating the answer.
Sample Input
1 12 22 3
Sample Output
1725
Hint
There are 7 possible arrangements for the second test case. They are: 11 11 11 10 11 01 10 11 01 11 01 10 10 01 Assume that a grids is '1' when it contains a jewel otherwise not.
The question is about diamond in each row and column in the n * m matrix. What is the type of diamond distribution?
In the n * m matrix, assume that each row exists. If each row is set to I with no diamond in the I column, a total of C (m, I) arrays exist.
Other m-I columns can be placed or not, but you need to exclude all of them. A 2 ^ (m-I)-1 type can be obtained, add n rows to get (2 ^ (m-I)-1) ^ n
F (I) = C (m, I) * (2 ^ (m-I)-1) ^ n;
Exclusion principles exclude excess f (0)-f (1) + f (2)... f (n );
#include
#include
#include using namespace std ;#define MOD 1000000007#define LL long longLL c[60][60] , num[60][60] , k[60] ;int main(){ int i , j , n , m ; memset(num,0,sizeof(num)) ; for(i = 0 ; i <= 50 ; i++) num[i][0] = num[0][i] = 1 ; k[0] = 1 ; for(i = 0 ; i <= 50 ; i++) c[i][0] = 1 ; for(i = 1 ; i <= 50 ; i++) { k[i] = k[i-1] * 2 ; k[i] %= MOD ; } for(i = 1 ; i <= 50 ; i++) { for(j = 1 ; j < i ; j++) { c[i][j] = c[i-1][j-1] + c[i-1][j] ; c[i][j] %= MOD ; } c[i][i] = 1 ; } while(scanf("%d %d", &n, &m) != EOF ) { if( num[n][m] == 0 ) { int temp = 1 ; LL ans = 0 , s ; for(i = 0 ; i <= m ; i++) { s = c[m][i] ; for(j = 1 ; j <= n ; j++) { s *= ( k[m-i]-1 ) ; s %= MOD ; } ans += temp * s ; ans %= MOD ; temp = -temp ; } if( ans < 0 ) ans += MOD ; num[n][m] = num[m][n] = ans ; } printf("%lld\n", num[n][m]) ; } return 0;}