UOJ # 422. [2018] Team work is the gift Small Z

# 422. [2018] Team work small Z gifts

min-max inclusion and exclusion

Each set of the first to be transformed into stained expected time

If there are x choice can be dyed, the desired time is ((n-1) * m + (m-1) * n)) / x

But x becomes, the answer will be very troublesome halfway statistics

So the x recorded in the state!

Contour DP

f [i] [j] [s] [x] to the (i, j), the contour lines is selected where s, x of all programs can be selected stained (-1) ^ (| T | +1) and

Enum (i, j) is not selected from the group selected from growth can be calculated directly with the x and s (i, j) position.

Each T is considered equivalent to exactly one position x on.

#include<bits/stdc++.h>
#define reg register int
#define il inline
#define fi first
#define se second
#define mk(a,b) make_pair(a,b)
#define numb (ch^'0')
#define pb push_back
#define solid const auto &
#define enter cout<<endl
#define pii pair<int,int>
using namespace std;
typedef long long ll;
template<class T>il void rd(T &x){
    char ch;x=0;bool fl=false;while(!isdigit(ch=getchar()))(ch=='-')&&(fl=true);
    for(x=numb;isdigit(ch=getchar());x=x*10+numb);(fl==true)&&(x=-x);}
template<class T>il void output(T x){if(x/10)output(x/10);putchar(x%10+'0');}
template<class T>il void ot(T x){if(x<0) putchar('-'),x=-x;output(x);putchar(' ');}
template<class T>il void prt(T a[],int st,int nd){for(reg i=st;i<=nd;++i) ot(a[i]);putchar('\n');}
namespace Modulo{
const int mod=998244353;
int ad(int x,int y){return (x+y)>=mod?x+y-mod:x+y;}
void inc(int &x,int y){x=ad(x,y);}
int mul(int x,int y){return (ll)x*y%mod;}
void inc2(int &x,int y){x=mul(x,y);}
int qm(int x,int y=mod-2){int ret=1;while(y){if(y&1) ret=mul(x,ret);x=mul(x,x);y>>=1;}return ret;}
template<class ...Args>il int ad(const int a,const int b,const Args &...args) {return ad(ad(a,b),args...);}
template<class ...Args>il int mul(const int a,const int b,const Args &...args) {return mul(mul(a,b),args...);}
}
using namespace Modulo;
namespace Miracle{
const int N=7;
const int M=103;
int f[M][N][1<<6][1201];
char mp[M][N],tc[N][M];
int n,m;
int val(int i,int j,int c,int s){
    int ret=0;
    if(c==0){
        if(i!=1&&((s>>(j-1))&1)) ++ret;
        if(j!=1&&((s>>(j-2))&1)) ++ret;
        return ret;
    }else{
        if(i!=1) ++ret;
        if(j!=1) ++ret;
        return ret;
    }
}
int main(){
    rd(n);rd(m);
    for(reg i=1;i<=n;++i){
        scanf("%s",tc[i]+1);
    }
    swap(n,m);
    for(reg i=1;i<=n;++i){
        for(reg j=1;j<=m;++j){
            mp[i][j]=tc[m-j+1][i];
            // cout<<mp[i][j];
        }
        // cout<<endl;
    }
    f[0][m][0][0]=mod-1;
    int sum=0;
    for(reg i=1;i<=n;++i){
        for(reg j=1;j<=m;++j){
            // cout<<" now "<<"("<<i<<","<<j<<")"<<endl;
            for(reg s=0;s<(1<<m);++s){
                for(reg x=0;x<=sum;++x){
                    //no choose
                    int li,lj;
                    if(j==1) li=i-1,lj=m;
                    else li=i,lj=j-1;

                    if(!f[li][lj][s][x]) continue;
                    int v=f[li][lj][s][x];

                    int ns=s;
                    if((s>>(j-1))&1) ns^=(1<<(j-1));
                    inc(f[i][j][ns][x+val(i,j,0,s)],v);

                    if(mp[i][j]=='*'){
                        //choose
                        ns|=(1<<(j-1));
                        inc(f[i][j][ns][x+val(i,j,1,s)],mod-v);
                    }
                }
            }
            int ct=0;
            if(i!=1) ++ct;
            if(j!=1) ++ct;
            sum+=ct;
            
            // cout<<" sum "<<sum<<endl;
            // for(reg s=0;s<(1<<m);++s){
            //     for(reg x=0;x<=sum+10;++x){
            //         cout<<" s "<<s<<" x "<<x<<" : "<<f[i][j][s][x]<<endl;
            //     }
            // }
        }
    }
    ll ans=0;
    // cout<<sum<<endl;
    for(reg s=0;s<(1<<m);++s){
        for(reg x=1;x<=sum;++x){
            ans=ad(ans,mul(f[n][m][s][x],mul(sum,qm(x))));
        }
    }
    ot(ans);
    return 0;
}

}
signed main(){
    Miracle::main();
    return 0;
}

/*
   Author: *Miracle*
*/

X recorded in the state must pay attention to what

Guess you like

Origin www.cnblogs.com/Miracevin/p/10994884.html