博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ P3254 Corn fields 【状压dp】
阅读量:5154 次
发布时间:2019-06-13

本文共 3044 字,大约阅读时间需要 10 分钟。

Corn Fields
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 16909 Accepted: 8939

Description

Farmer John has purchased a lush new rectangular pasture composed of M by N (1 ≤ M ≤ 12; 1 ≤ N ≤ 12) square parcels. He wants to grow some yummy corn for the cows on a number of squares. Regrettably, some of the squares are infertile and can't be planted. Canny FJ knows that the cows dislike eating close to each other, so when choosing which squares to plant, he avoids choosing squares that are adjacent; no two chosen squares share an edge. He has not yet made the final choice as to which squares to plant.

Being a very open-minded man, Farmer John wants to consider all possible options for how to choose the squares for planting. He is so open-minded that he considers choosing no squares as a valid option! Please help Farmer John determine the number of ways he can choose the squares to plant.

Input

Line 1: Two space-separated integers:
M
and
N
Lines 2..
M
+1: Line
i
+1 describes row
i
of the pasture with
N
space-separated integers indicating whether a square is fertile (1 for fertile, 0 for infertile)

Output

Line 1: One integer: the number of ways that FJ can choose the squares modulo 100,000,000.

Sample Input

2 31 1 10 1 0

Sample Output

9

Hint

Number the squares as follows:
1 2 3  4  
There are four ways to plant only on one squares (1, 2, 3, or 4), three ways to plant on two squares (13, 14, or 34), 1 way to plant on three squares (134), and one way to plant on no squares. 4+3+1+1=9.

Source

[]   [Go Back]   []   []

设f[i][s]表示第i行s状态下的方案数,则对于所有i - 1行与s不冲突的方案都可以转移
#include
#include
#include
#include
#define LL long long int#define REP(i,n) for (int i = 1; i <= (n); i++)#define fo(i,x,y) for (int i = (x); i <= (y); i++)#define Redge(u) for (int k = head[u]; k != -1; k = edge[k].next)using namespace std;const int maxn = 20,maxm = 1 << 12,INF = 1000000000,P = 100000000;inline int read(){ int out = 0,flag = 1;char c = getchar(); while (c < 48 || c > 57) {if (c == '-') flag = -1; c = getchar();} while (c >= 48 && c <= 57) {out = out * 10 + c - 48; c = getchar();} return out * flag;}int f[maxn][maxm],N,M,le[maxm];bool ill[maxm];int main(){ N = read(); M = read(); int maxv = (1 << M) - 1; for (int i = 1; i <= N; i++){ for (int j = 1; j <= M; j++) le[i] = (le[i] << 1) + read(); } for (int s = 0; s <= maxv; s++){ bool sig = false; int t = s; while (t){ if ((t & 1)&& sig) {ill[s] = true; break;} else if (t & 1) sig = true; else sig = false; t >>= 1; } } for (int s = 0; s <= maxv; s++){ if (!ill[s] && (s | le[1]) == le[1]) f[1][s] = 1; } for (int i = 2; i <= N; i++) for (int e = 0; e <= maxv; e++){ if (ill[e] || (e | le[i]) != le[i]) continue; for(int s = 0; s <= maxv; s++){ if (e & s) continue; f[i][e] = (f[i][e] + f[i - 1][s]) % P; } } int ans = 0; for (int i = 0; i <= maxv; i++) ans = (ans + f[N][i]) % P; cout<
<

转载于:https://www.cnblogs.com/Mychael/p/8282845.html

你可能感兴趣的文章
I/O的一些简单操作
查看>>
Handbook之012:函数类别构型
查看>>
php取整函数ceil,floor,round,intval的区别
查看>>
局部富文本
查看>>
例题6-7 树的层次遍历
查看>>
2019-2-15 日记
查看>>
那些年我们跳过的 IE坑
查看>>
产生式模型和判别式模型
查看>>
2015.10.13课堂
查看>>
国内最火5款Java微服务开源项目
查看>>
[国嵌攻略][038][时钟初始化]
查看>>
C#格式化字符串
查看>>
剑指offer——二叉搜索树的后序遍历序列
查看>>
2016集训测试赛(二十四)Problem C: 棋盘控制
查看>>
稳定土厂拌设备控制系统-基本介绍(图)
查看>>
测试计划
查看>>
JAVA中properties基本用法
查看>>
大道至简第四章读后感
查看>>
对象的动态特性
查看>>
析构函数 p157
查看>>