天天看点

UVALive - 8144 Sacred Scarecrows DP + FMT 未解决

简略题意: R∗C 的庄稼地,有些地方已经种了庄稼了。现在需要放置一些稻草人,使得满足以下两个条件:

1 . 所有行都包含稻草人。

2. 相邻的两列至少包含两个稻草人。

这题目前还没有 AC ,但是值得记录。

注意到行很少,因此可以状压进行 DP 。

令 dp[i][0/1][j] 代表,前i列的稻草人存放了那些行,当前列是否有稻草人。

用 ∗∗ 代表集合并卷积,那么转移有如下:

1.dp[i][0]=dp[i−1][1]∗∗g[0]

2.dp[i][1]=dp[i−1][0]∗∗g/g[0]+dp[i−1][0]∗∗g/g[0]=(dp[i−1][0]+dp[i−1][1])∗∗g/g[0]

障碍物的处理直接把对应卷上的 g 给置零即可,所以可以用FMT来优化到 R∗(2R)∗C 。

经过用 assert 得出测试数据至少有 20 组…上述算法无法通过。

qls给出了如下解法:

对行容斥,可以知道每列有多少空地,然后 O(c) 的 dp .

容斥用 dfs 枚举集合可以 2r ,可以做到 O(2R∗C) .

暂时还没太理解,先想一想。

#define others
#ifdef poj
#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
#include <set>
#endif // poj
#ifdef others
#include <bits/stdc++.h>
#endif // others
//#define file
#define all(x) x.begin(), x.end()
using namespace std;
#define eps 1e-8
const double pi = acos(-);

typedef long long LL;
typedef unsigned long long ULL;
void umax(int &a, int b) {
    a = max(a, b);
}
void umin(int &a, int b) {
    a = min(a, b);
}
int dcmp(double x) {
    return fabs(x) <= eps?:(x > ?:-);
}
void file() {
    freopen("data_in.txt", "r", stdin);
    freopen("data_out.txt", "w", stdout);
}

const LL mod = +;

int r, c, lim;
LL dp[][][], f[], g[], h[];
char G[][];

LL add(LL a, LL b) {
    a += b;
    if(a >= mod) a -= mod;
    return a;
}

LL mul(LL a, LL b) {
    LL sum = l * a * b;
    if(sum >= mod) a %= mod;
    return sum;
}

LL sub(LL a, LL b) {
    a -= b;
    if(a < ) a += mod;
    return a;
}

void FMT(int col, int on) {
    for(int i = ; i < lim; i++) g[i] = ;
    if(on) {
        for(int i = ; i < lim; i++) {
            bool isok = ;
            for(int j = ; j < r; j++) {
                if((i & ( << j)) && G[j][col] == 'v') isok = ;
            }
            if(isok) g[i] = ;
        }
    } else {
        g[] = ;
    }
    for(int i = ; i < r; i++)
        for(int j = ; j < lim; j++)
            if((j >> i) & )
                f[j] = add(f[j], f[j^(<<i)]);
    for(int i = ; i < r; i++)
        for(int j = ; j < lim; j++)
            if((j >> i) & )
                g[j] = add(g[j], g[j^(<<i)]);
    for(int i = ; i < lim; i++)
        h[i] = mul(f[i], g[i]);
    for(int i = ; i < r; i++)
        for(int j = ; j < lim; j++)
            if((j>>i)&)
                h[j] = sub(h[j], h[j^(<<i)]);
}

void cpy(LL x[], LL y[]) {
    for(int i = ; i < lim; i++)
        x[i] = y[i];
}

int main() {
//    file();
int ff = ;
    while(~scanf("%d%d", &r, &c)) {
        ff++;
//    assert(ff <= 30);
        lim =  << r;
        for(int i = ; i < r; i++)
            for(int j = ; j <= c; j++)
                scanf(" %c", &G[i][j]);
        for(int i = ; i < ; i++)
            for(int j = ; j < ; j++)
                for(int k = ; k < lim; k++) dp[i][j][k] = ;
        LL ans = ;
        dp[][][] = ;
        for(int i = ; i <= c; i++) {
            int now = i & , pre = now ^ ;
            for(int j = ; j < lim; j++) dp[now][][j] = dp[now][][j] = ;
            for(int j = ; j < lim; j++)
                dp[now][][j] = dp[pre][][j], dp[pre][][j] = add(dp[pre][][j], dp[pre][][j]);

            cpy(f, dp[pre][]);
            FMT(i, );
            cpy(dp[now][], h);
        }
        cout<<(dp[c&][][(<<r)-] + dp[c&][][(<<r)-])%mod<<'\n';
    }
    return ;
}