Snake Rana Gym - 101350G

题目:

Old Macdonald wants to build a new hen house for his hens. He buys a new rectangular area of size N by M. The night before he builds the hen house, snake Rana devises an evil plan to plant bombs in K distinct cells in the area to kill the hens and eat them for dinner later.

The morning of, Old Macdonald notices that each of the K cells, where snake Rana planted a bomb, have a marking on them. That won’t stop him though, all he must do is build the hen house in an area with no bombs.

Assume that rows are numbered from top to bottom, and columns are numbered from left to right. Old Macdonald now wants to know the number of ways he can choose sub-rectangles of top left coordinates (x1, y1) and bottom right coordinates (x2, y2) (x1 ≤ x2) (y1 ≤ y2) such that there are no bombs in the sub rectangle.

有一个大的矩形区域,其中一些地方中有炸弹,现在问你有多少中矩阵的取法,使得任何一个矩阵里面都不包含炸弹。

思路:这个题是一个计数问题,我们可以使用容斥的方法去做,首先求出能构成矩阵的所有方案,相当于是在(n+1)条边中取两条边,在(m+1)条边中取两条边,得到总的方案数(n+1)*n/2*(m+1)*m/2,然后在减去包含奇数个炸弹的方案数,这时会多减掉一些部分,所以我们要在加上包含偶数个炸弹的方案数,想求包含几个炸弹,由于总炸弹的数量比较少,所以我们使用二进制的方式来判断包含的炸弹数。

#include <bits/stdc++.h>

using namespace  std;
const int maxn=1e4;
const long long inf=1e9;
struct Node
{
    long long x,y;
} node[maxn];
int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        long long n,m,k;
        cin>>n>>m>>k;
        for(int i=0; i<k; i++)
        {
            cin>>node[i].x>>node[i].y;
        }
        long long ans=(long long)(n+1)*n/2*(m+1)*m/2;
//        cout<<ans<<endl;
        for(int i=1; i<(1<<k); i++)
        {
            int cnt=0;
            long long minx=inf,miny=inf;
            long long maxx=-inf,maxy=-inf;
            for(int j=0; j<k; j++)
            {
                if(i&(1<<j))
                {
                    minx=min(minx,node[j].x);
                    miny=min(miny,node[j].y);
                    maxx=max(maxx,node[j].x);
                    maxy=max(maxy,node[j].y);
                    cnt++;
                }

            }
            long long res=minx*miny*(n-maxx+1)*(m-maxy+1);///相当于求出包含目前cnt个炸弹的所有矩阵数
            if(cnt&1) ans-=res;
            else ans+=res;
        }
        cout<<ans<<endl;
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值