hdu--5155Harry And Magic Box(组合数+容斥原理)

Harry And Magic Box
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
Appoint description: 

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 should 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 1 2 2 2 3
 

Sample Output

     
     
1 7 25

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.

题目大意在n*m的矩阵内每一行每一列都有钻石,问钻石分布的种类?

在n*m的矩阵中,假设每一行都存在,对于每一行设i为有i列不存在钻石,那么共有C(m,i)种排列。

对于其他的m-i列中可以放也可以不放,但是要排除全都不放的情况,得到2^(m-i) - 1种,再加上n行得到(2^(m-i)-1)^n

得到f(i) = C(m,i) * (2^(m-i)-1)^n ;

容斥原理排除多余的  f(0) - f(1) + f(2)....f(n) ;


#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std ;
#define MOD 1000000007
#define LL long long
LL 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;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值