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

https://codeforces.com/contest/1536/problem/E

Imagine picking some subset of ‘#’ and making them 0. Then there is exactly one way to make all the remaining ‘#’ positive integers.
——官方题解

这句话非常重要。
就是,让若干的#为0,每次这样选都会有一种符合解(因为多源BFS)。所以我们只要看有多少种不同的让若干#为0即可。
计#个数为n,则选取方式如下:

C n 0 + C n 1 + . . . + C n n C^0_n+C^1_n+...+C^n_n Cn0+Cn1+...+Cnn
通过高中知识,这个结果是
2 n 2^n 2n
(但是其实我也是看大佬的qwq)
https://blog.csdn.net/qq_41286356/article/details/105788449

https://blog.csdn.net/litble/article/details/75913032?depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-2&utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-2

1.情景假设法(假设boshi从枣树选枣子的方案。。。)
2.隔板法(boshi把枣子放成一排,通过在枣子间添加隔板来分组。。。)
3.通向公式法
4.递推公式法

这里相当于n个中选0~n个的所有情况,所有情况即每个有选与不选两种情况。
或者: ( 1 + 1 ) n = C n 0 + C n 1 + . . . + C n n (1+1)^n=C^0_n+C^1_n+...+C^n_n (1+1)n=Cn0+Cn1+...+Cnn是显然的

但是如果整个图全是#,没有0,没有BFS源,要除掉-1即可,因为没有0只有一种情况,就是整个图全是#时选0个#取0。

AC代码:

#include<bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define MOD 1000000007
#define int long long
char a[2005];
inline int qp(int a,int n,int mod){
	int ans=1;
	while(n){
		if(n&1)ans=ans*a%mod;
		a=a*a%mod;
		n>>=1;
	}
	return ans;
}
signed main(){
	int t;
	scanf("%lld",&t);
	int n,m;
	while(t--){
		scanf("%lld%lld",&n,&m);
		int cnt=0;
		for(int i=0;i<n;i++){
			scanf("%s",a);
			for(int j=0;j<m;j++){
				if(a[j]=='#')cnt++;
			}
		}
		int te=qp(2,cnt,MOD);
		te-=cnt==m*n;
		printf("%lld\n",te);
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值