【玉米田】

题目

代码

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;

const int mod = 1e8;
const int M = 1 << 12;
LL f[13][M];
int g[13];
vector<int> state;
vector<int> p[M];
int n, m;
bool check(int x)
{
    return !(x & x << 1);
}
int main()
{
    cin >> m >> n;
    for(int i = 1; i <= m; i++)
    {
        for(int j = 0; j < n; j++)
        {
            int t;
            cin >> t;
            g[i] |= !t << j;
        }
    }
    
    for(int i = 0; i < 1 << n; i++)
    {
        if(check(i)) state.push_back(i);
    }
    
    for(auto a : state)
        for(auto b : state)
        {
            if((a & b) == 0) p[a].push_back(b);
        }
    
    f[0][0] = 1;
    for(int i = 1; i <= m+1; i++)
    {
        for(auto a : state)
        {
            f[i&1][a] = 0;
            if(a & g[i]) continue;
            for(auto b : p[a])
            {
                f[i&1][a] = (f[i&1][a] + f[(i-1)&1][b]) % mod;
            }
        }
    }
    
    cout << f[(m+1)&1][0];
    return 0;
}

注意

采用滚动数组时,一定要注意恢复,而且不仅是更新之前恢复,对于那些不更新的值也要恢复,因为不更新不代表继承上一次的值,而是采用默认值,因此必须恢复,下面的这个代码就闹了这个笑话

cpp 复制代码
    f[0][0] = 1;
    for(int i = 1; i <= m+1; i++)
    {
        for(auto a : state)
        {
            f[i&1][a] = 0;
            if(a & g[i]) continue;
            //f[i&1][a] = 0;
            for(auto b : p[a])
            {
                f[i&1][a] = (f[i&1][a] + f[(i-1)&1][b]) % mod;
            }
        }
    }
相关推荐
Ayanami_Reii18 分钟前
基础数学算法-开关问题
数学·算法·高斯消元
稚辉君.MCA_P8_Java28 分钟前
通义 Go 语言实现的插入排序(Insertion Sort)
数据结构·后端·算法·架构·golang
稚辉君.MCA_P8_Java1 小时前
Gemini永久会员 Go 实现动态规划
数据结构·后端·算法·golang·动态规划
快手技术1 小时前
快手 & 南大发布代码智能“指南针”,重新定义 AI 编程能力评估体系
算法
Murphy_lx2 小时前
C++ std_stringstream
开发语言·c++·算法
CoovallyAIHub2 小时前
超越YOLOv8/v11!自研RKM-YOLO为输电线路巡检精度、速度双提升
深度学习·算法·计算机视觉
哭泣方源炼蛊2 小时前
HAUE 新生周赛(七)题解
数据结构·c++·算法
q***64973 小时前
SpringMVC 请求参数接收
前端·javascript·算法
Lwcah3 小时前
Python | LGBM+SHAP可解释性分析回归预测及可视化算法
python·算法·回归
小此方3 小时前
从零开始手搓堆:核心操作实现 + 堆排序 + TopK 算法+ 向上调整 vs 向下调整建堆的时间复杂度严密证明!
开发语言·数据结构·算法