UVALive - 8144 Sacred Scarecrows DP + FMT 未解决

3 篇文章 0 订阅
1 篇文章 0 订阅

简略题意: RC 的庄稼地,有些地方已经种了庄稼了。现在需要放置一些稻草人,使得满足以下两个条件:
1 . 所有行都包含稻草人。
2. 相邻的两列至少包含两个稻草人。

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

注意到行很少,因此可以状压进行 DP
dp[i][0/1][j] 代表,前i列的稻草人存放了那些行,当前列是否有稻草人。

代表集合并卷积,那么转移有如下:
1.dp[i][0]=dp[i1][1]g[0]
2.dp[i][1]=dp[i1][0]g/g[0]+dp[i1][0]g/g[0]=(dp[i1][0]+dp[i1][1])g/g[0]
障碍物的处理直接把对应卷上的 g 给置零即可,所以可以用FMT来优化到 R(2R)C
经过用 assert 得出测试数据至少有 20 组…上述算法无法通过。

qls给出了如下解法:
对行容斥,可以知道每列有多少空地,然后 O(c) dp .
容斥用 dfs 枚举集合可以 2r ,可以做到 O(2RC) .
暂时还没太理解,先想一想。

#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(-1.0);

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?0:(x > 0?1:-1);
}
void file() {
    freopen("data_in.txt", "r", stdin);
    freopen("data_out.txt", "w", stdout);
}

const LL mod = 1e9+7;

int r, c, lim;
LL dp[2][2][20000], f[20000], g[20000], h[20000];
char G[16][1100];

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

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

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

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

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

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

            cpy(f, dp[pre][1]);
            FMT(i, 1);
            cpy(dp[now][1], h);
        }
        cout<<(dp[c&1][0][(1<<r)-1] + dp[c&1][1][(1<<r)-1])%mod<<'\n';
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值