HDU - 5155 Harry And Magic Box

解决Harry面对的一个数学问题,即在一个n行m列的网格中,每行每列至少有一个发光宝石的不同分布方案数。使用容斥原理和组合数学方法进行解答。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


Harry And Magic Box

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.

There are several test cases.
For each test case,there are two integers n and m indicating the size of the box. 0n,m500≤n,m≤50.

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





    题意:  n 行  m 列的表格,每一行,每一列不能全为0  ,求方案数?


   思路:   容斥 + 组合数。直接假定  每一行都有  1 ,考虑每一列的情况,枚举有多少列全部是0 ,奇数减偶数加。




#pragma comment(linker, "/STACK:1024000000,1024000000")
//#include <bits/stdc++.h>
#include<string>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#include<algorithm>
#define maxn 57
#define INF 0x3f3f3f3f
#define eps 1e-8
#define mod 1000000007
#define ll long long
using namespace std;


ll Pow(ll a,ll b)
{
    a=a%mod;
    ll ans=1;
    while(b)
    {
        if(b&1)  ans=ans*a%mod;
        a=a*a%mod;
        b>>=1;
    }
    return ans%mod;
}
ll c[maxn][maxn];
void init()
{
    for(int i=1;i<maxn;i++)
    {
        c[i][0]=c[i][i]=1;
        for(int j=1;j<i;j++)
            c[i][j]=(c[i-1][j]+c[i-1][j-1])%mod;
    }
}
int main()
{
    ll n,m;
    init();
    while(scanf("%lld%lld",&n,&m)!=EOF)
    {
        ll ans=0;
        for(int i=0;i<=m;i++)
        {
            if(i&1)
            {
                ans=((ans-Pow((1ll<<(m-i))-1,n)*c[m][i]%mod)%mod+mod)%mod;
            }
            else
                ans=((ans+Pow((1ll<<(m-i))-1,n)*c[m][i]%mod)%mod+mod)%mod;
        }
        printf("%lld\n",ans);
    }
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值