uoj#422. 【集训队作业2018】小Z的礼物

uoj#422. 【集训队作业2018】小Z的礼物

题目描述

Solution

所有礼物全部取到的方案数并不好求,因此我们考虑 m i n − m a x min-max minmax容斥,转化为第一次取到集合中某一个的期望时间。

p = n ∗ ( m − 1 ) + m ∗ ( n − 1 ) p=n*(m-1)+m*(n-1) p=n(m1)+m(n1)表示有多少个 1 ∗ 2 1*2 12矩阵的选取方案。
倘若此时有 x x x 1 ∗ 2 1*2 12的矩阵与枚举的集合 T T T有交,不难证明,期望的步数即为 p x \frac{p}{x} xp

所以我们直接枚举集合 T T T,计算 x x x,就可以以 O ( 2 n u m n u m ) O(2^{num}num) O(2numnum)的时间求出答案( n u m num num表示有多少个 ∗ * 号)。

这样显然会超时。

我们发现 n ≤ 6 n\leq 6 n6,因此考虑状压 d p dp dp,从第一列开始,一列一列转移,记录轮廓线上的信息,令 f [ S ] [ j ] f[S][j] f[S][j]表示轮廓线的状态为 S S S,目前有 j j j 1 ∗ 2 1*2 12矩阵与 T T T有交的方案数,并且将容斥系数一起带入其中计算。

时间复杂度 O ( 2 n n m p ) O(2^nnmp) O(2nnmp)

#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <cassert>
#include <string.h>
//#include <unordered_set>
//#include <unordered_map>
//#include <bits/stdc++.h>

#define MP(A,B) make_pair(A,B)
#define PB(A) push_back(A)
#define SIZE(A) ((int)A.size())
#define LEN(A) ((int)A.length())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define fi first
#define se second

using namespace std;

template<typename T>inline bool upmin(T &x,T y) { return y<x?x=y,1:0; }
template<typename T>inline bool upmax(T &x,T y) { return x<y?x=y,1:0; }

typedef long long ll;
typedef unsigned long long ull;
typedef long double lod;
typedef pair<int,int> PR;
typedef vector<int> VI;

const lod eps=1e-11;
const lod pi=acos(-1);
const int oo=1<<30;
const ll loo=1ll<<62;
const int mods=998244353;
const int MAXN=600005;
const int INF=0x3f3f3f3f;//1061109567
/*--------------------------------------------------------------------*/
inline int read()
{
	int f=1,x=0; char c=getchar();
	while (c<'0'||c>'9') { if (c=='-') f=-1; c=getchar(); }
	while (c>='0'&&c<='9') { x=(x<<3)+(x<<1)+(c^48); c=getchar(); }
	return x*f;
}
char st[10][105];
int f[2][105][1205],inv[1205];
inline int upd(int x,int y){ return x+y>=mods?x+y-mods:x+y; }
int main()
{
	int n=read(),m=read(),p=n*(m-1)+m*(n-1);
	for (int i=1;i<=n;i++) scanf("%s",st[i]+1);
	inv[0]=inv[1]=1;
	for (int i=2;i<=1200;i++) inv[i]=1ll*inv[mods%i]*(mods-mods/i)%mods;
	
	int pre=1,now=0;
	f[now][0][0]=mods-1;
	for (int i=1;i<=m;i++)
		for (int j=1;j<=n;j++)
		{
			now^=1,pre^=1;
			memset(f[now],0,sizeof f[now]);
			for (int S=0;S<1<<n;S++)
				for (int k=0;k<=p;k++)
				if (f[pre][S][k])
				{
					if (st[j][i]=='*')
					{
						int _S=S|(1<<(j-1)),_k=k+(j<n)+(i<m);
						_k+=(!((S>>(j-2))&1)&&j>1);
						_k+=(!((S>>(j-1))&1)&&i>1);
						f[now][_S][_k]=upd(f[now][_S][_k],mods-f[pre][S][k]);
					}
					int _S=S&(((1<<n)-1)^(1<<(j-1)));
					f[now][_S][k]=upd(f[now][_S][k],f[pre][S][k]);
				}
		}
	int ans=0;
	for (int i=0;i<1<<n;i++)
		for (int j=1;j<=p;j++) 
			ans=upd(ans,1ll*f[now][i][j]*p%mods*inv[j]%mods);
	printf("%d\n",ans); 
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值