Codeforces Round #724 (Div. 2) E. Omkar and Forest(思维)

题意:

给出一个 n ⋅ m n \cdot m nm的方格,由 0 0 0 和 # 组成。#可以被填入非负整数,但要满足以下要求:

1. 1. 1. 相邻单元格之间的数值之差不能超过 1 1 1

2. 2. 2. 假如某个单元格上的数字为正整数,那么必须至少存在一个相邻格子上的数值比其小。

求有多少种填数方案。

题解:

结论题。结论:#的数量为 c n t cnt cnt个,则方案数为 2 c n t 2^{cnt} 2cnt ,若初始状态全为#,则方案数要减一。

证明:

假设有 x x x 个#的值为0,那么此时0的数量已经固定了,又因为条件1,所有接下来只能填1,那么1填在哪呢?根据条件2可以知道,1只能填在0的上下左右,填完1后,所有的0就会被包围,再接着就是填2,同理,2也只能填在1的上下左右,以此类推,可以发现,其实就是从0的位置开始 b f s bfs bfs的过程。当0确定后,其他数都能确定。

所以枚举#中0的个数,方案数就是 C ( c n t , 0 ) + C ( c n t , 1 ) + C ( c n t , 2 ) + . . . . . . + C ( c n t , c n t ) = 2 c n t C(cnt,0)+C(cnt,1)+C(cnt,2)+......+C(cnt,cnt)=2^{cnt} C(cnt,0)+C(cnt,1)+C(cnt,2)+......+C(cnt,cnt)=2cnt

当初始状态全为#时, C ( c n t , 0 ) C(cnt,0) C(cnt,0) 不满足条件,因为没有0就无法往下填数,所有要减1。

代码:

#pragma GCC diagnostic error "-std=c++11"
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
#include<stack>
#include<set>
#include<ctime>
#define iss ios::sync_with_stdio(false)
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int,int> pii;
const int mod=1e9+7;
const int MAXN=2e5+5;
const int inf=0x3f3f3f3f;
ll qpow(ll a,ll b)
{
    ll res=1;
    while(b){
        if(b&1) res=res*a%mod;
        a=a*a%mod;
        b>>=1;
    }
    return res;
}
int main()
{
    int t;
    cin>>t;    
    while(t--){
        int n,m;
        cin>>n>>m;
        int cnt=0;
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                char s;
                cin>>s;
                if(s=='#') cnt++;
            }
        }
        ll ans=qpow(2,cnt);
        if(cnt==n*m) ans=(ans-1+mod)%mod;
        printf("%lld\n",ans);
    }
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值